Lab #6: Recursion
Fall 2000
You must submit this lab by 11:59pm Wednesday, November 1.
The objective of this lab is to become familiar with the use of recursion to solve problems.
Note: In each of the following questions, you will be asked to write a main() method as part of your class to demonstrate the methods you have written. It is important that this main() method does not expect user input. It should just contain a number of test cases that you design. You will not need Stdin.java for this lab and may not use it in the programs you submit.
printTriangle(5,'*');
prints
* ** *** **** *****
You may write other methods to be called by printTriangle(), but none of your methods may contain any loops. Do not add any additional output in your method. If n < 1 your method should not print anything. Make your method part of a class named Ptriangle. Write a main() method for Ptriangle to demonstrate the use of printTriangle().
printOpenTriangle(5,'*');
prints
* ** * * * * * *
Include it in a class named POtriangle, and include a main() method that demonstrates the use of printOpenTriangle().
public static int sumDigits(int n)
that sums the digits of n and returns the sum. For example,
sumDigits(12345);
would return 15 (= 1 + 2 + 3 + 4 + 5). You may assume n > 0. Include your method in a class named Digits. Write a main() method for this class to demonstrate the use of your method.
public static void printReverseDigits(int n, int base)
that prints the digits of n in reverse order using base base numbers. For example,
printReverseDigits(12345,10);
would print
54321
and
printReverseDigits(6,2);
would print
011
You may assume n >= 0 and 16 >= base >= 2. If base > 10 you are to use the letter 'A' to represent 10, 'B' to represent 11, 'C' to represent 12, ..., and 'F' to represent 15. Your method need not worry about advancing to a new line after the digits are printed. Include your method in a class named Pdigits, and include a main() method that demonstrates the use of your method.
Note: Since some people have expressed concern about ``number bases'', here is some basic information.
Every integer x can be expressed in terms of a base b via the following
relation:
can be evaluated efficiently using Horner's rule. This rule avoids explicit computation of the xi terms as follows:
Complete the definition of the recursive method horner with header
public static double horner(double [] a, double x, int i)
where the first parameter is an array of coefficients of the polynomial, the second is the value of x at which the polynomial is to be evaluated, and the third is an index that you should use to keep track of your recursion. You may assume that, when first called, the value of i is zero. Include this method in a class named Poly, and write a main() method to demonstrate its use.
You should submit the following files: Ptriangle.java, POtriangle.java, Digits.java, Pdigits.java and Poly.java. Do not submit Stdin.java, since none of your main() methods will require it.