CS 172
PA 9 - Recursion
Submit: Methods9.java and TestPA9.java on Canvas.
Programs that do not compile will receive a grade of zero.
You must have the three components, in order, in each recursive method.
1) e
or case - If there is no e
or case, you must have a comment saying so.
2) base case - You must have at least one base case (there may be more than one).
3) recursive step - You must have at least one recursive step (there may be more than one).
Also, you must have comments in each method that clearly label the e
or case, base case(s), and
ecursive step(s).
In Methods9.java:
Note: Methods9.java does not have a main method.
1. Write a recursive method called printLettersForward that will accept a character parameter. The
method should print all of the letters of the alphabet up to (including) the parameter value. For
example, if the parameter passed to the method is 'f', the method should print:
abcdef
The sequence of characters printed should be the same case as the parameter. If the parameter
value is 'D' (uppercase), then the method should print:
ABCD
If the parameter value is not a letter of the alphabet, do not print anything. Do not return anything.
public static void printLettersForward ( char c ) {
You cannot have any loop in this method.
You cannot declare any additional variables inside this method.
You cannot use ASCII codes in this method. Write characters enclosed in apostrophes.
2. Write a recursive method that will count (and return) how many positive factors a number has.
The method header is
public static int countFactors( int number, int testFactor )
You cannot have any loop in this method.
The method cannot work (e
or case) if number is not positive. Your method should return 0 for the
e
or case.
For testing, you should use the same value for both parameters in your test call.
Examples
countFactors( 10, 10 ) should return 4 because 10 has 4 positive factors: 1, 2, 5, and 10
countFactors( 1, 1 ) should return 1 because 1 has 1 positive factor: 1
countFactors( -3, -3 ) should return 0
In TestPA9.java:
1. Write a main method.
a. Write at least 3 test calls to printLettersForward. Display meaningful messages with your
output.
. Write at least 3 test calls to countFactors. Display meaningful messages with your output.