Java Reference
In-Depth Information
15.1.6
Self-review exercises
SR1. Define the term ancestor recursively â€”the way noun phrase was defined
in Sec. 15.1.1.
SR2.
Define the syntax of decimal integers, like 5432 and 041, recursively.
The rest of these exercises ask you to write small recursive methods, which are
quite similar to those developed in this Sec. 15.1. For each, we give a specifica-
tion and method header. Write each one and then test it. It does no good to write
a function that you think is right but is wrong. When testing, first write calls that
test the base case; then slowly work your way up to calls with larger arguments.
SR3. /** Set each array element b[i] of b[h..n] to i */
public static void setToI( int [] b, int h, int n)
SR4. /** Print the squares of the integers in the range h..n . */
public static void printSquares( int h, int n)
SR5. /** Print n if it is not divisible by an integer in the range h..k */
public static void printIfNot( int n, int h, int k)
SR6. /** Print x if it is one of the values b[h..] */
public static void printX( int [] b, int x, int h)
SR7. /** Print n if it is divisible by an integer in the range h..k */
public static void printIf( int n, int h, int k)
SR8. /** Print the square of the integers in the range 0..n
in descending order of values in the range */
public static void printSquares( int n)
SR9. /** Set every element of b[h..] with even index to zero */
public static void zeroEven( int [] b, int h)
SR10. /** Print 5 if it is in b[ h..k] */
public static void printIf5( int[] b, int h, int k)
SR11. /** = s with each char duplicated, e.g. dup("bcd") is "bbccdd" */
public static String dup(String s)
SR12. /** = sum of the characters in s (each character is a digit) */
public static int sumChars(String s)
SR13. /** = s with each character lower-cased */
public static String lowerCase(String s)
SR14. /** = s with every adjacent pair of characters swapped
public static int swapPairs(String s)

Search WWH ::

Custom Search