Copyright RIT 2007
$Id: writeup.xml,v 1.17 2009/09/25 13:38:54 vcss233 Exp $
The primary purpose of this lab is to give you some experience writing programs that utilize recursion.
Read chapter 3.5 in the Goodrich and Tamassia book.
Review your class notes.
Read some facts about the Tower of Hanoi from http://www.lhs.berkeley.edu/java/tower/towerhistory.html
See an example of the Tower of Hanoi http://www.lhs.berkeley.edu/java/tower/tower.html
Each lab you will do is separated into a set of activities. Each activity has specific deliverables you must submit for credit and each activity is graded separately.
In this activity you will write a recursive program that calculates m to the nth power. Instead of directly using the Math.pow() function or iteratively multiplying m n times, you will compute the result recursively by performing successive multiplication operations. Your program must obtain the numbers m and n from the command line. If the program invocation has the wrong number of command line arguments, it shall print the following error message on standard error and terminate:
|
The name of the class you will write to implement this program
is MToTheNth.
The main method of this class processes the command line arguments
and then calls a method,
mToTheNth(), that uses recursion to compute the power.
The declaration for the method
mToTheNth() is given below:
|
Before calling the method mToTheNth(),
the program first checks to see if m
is a positive integer, and if n is non-negative.
The base, m, must be greater than 0, and the exponent
must be greater than -1.
If n is 0, the result is 1 and the method returns.
Otherwise, if n is not 0, compute the result
by multiplying m by the result of calling mToTheNth
with m and n-1.
Before calling the recursive method, print the math expression the recursion will solve. For example, the program would print
|
mToTheNth( 2, 3 ).
You will not receive credit if you do not use recursion for both computing the result and printing the multiplied numbers.
The output of your program must be formatted as shown in the sample runs below:
|
NOTE: The error message in the second example above is correct. The "For" is capitalized. That part of the error message comes from the exception produced by Integer.parseInt(); your program must add the prefix text.
When you are convinced that your program is correct, submit your work using the following command:
|
The "power set" is a set containing all its possible subsets. For example, the power set of { x, y } is { {}, {x}, {y}, {x, y} }, using brace notation for the sets.
In this activity you will write a program that takes a set of strings from the command line and prints the power set of those strings. The PowerSet program will use string notation for the power set members. The empty string, "", will represent the null set, and the string "a" represents the set containing the letter a. The concatenation of strings will represent other subsets. For example, the subset composed of sets "a" and "b" would be "ab".
Fetch the required materials from http://www.cs.rit.edu/~vcss233/pub/lab04/Binaries/lab4.jar and unpack the jar.
Below are sample runs of the program showing the required output of
the PowerSet program:
|
To ensure a consistent order of output, this lab includes a PowerComparator to use with the collection of subsets. The comparator is in the lab jar file.
Your solution must use recursion. You may use the collections in java.util.
One way to generate the power set is outlined below:
Concrete examples are in the example outputs earlier. Here are some more illustrations of the process of generating power sets.
The input set { b } generates the power set { "", b }.
If { a, b } is the input set, generate the power set as follows:
|
The class that you will write to implement this program will be named
PowerSet.
The PowerSet program will also be structured recursively;
the main method processes the command line arguments and then
calls a method to compute the power set.
You may also find it useful to have some helper methods.
After you have tested your program, and are sure that it works, submit it using the following command:
|
Grade Breakdown: