next up previous
Next: What to turn in Up: Assignment 5: Recursion Previous: Recurse

Recurse again

1.
Read Sections 7.1 and 7.2 of the book.

2.
Add the following methods to the Recurse project.

    // first: returns the first character of the given String
    public static char first (String s) {
        return s.charAt (0);
    }

    // last: returns a new String that contains all but the
    // first letter of the given String
    public static String rest (String s) {
        return s.substring (1, s.length());
    }

    // length: returns the length of the given String
    public static int length (String s) {
        return s.length();
    }

3.
Write some code in main that tests each of these methods. Make sure they work, and make sure you understand what they do.

4.
Write a method called printString that takes a String as a parameter and that prints the letters of the String, one on each line. It should be a void method.

5.
Write a method called printBackward that does the same thing as printString but that prints the String backwards (one character per line).

6.
Write a method called reverseString that takes a String as a parameter and that returns a new String as a return value. The new String should contain the same letters as the parameter, but in reverse order. For example, the output of the following code

	String backwards = reverseString ("Allen Downey");
	System.out.println (backwards);
should be

yenwoD nellA



Allen B. Downey
1999-10-06