ICS 211 Homework 8

Recursive methods

In this assignment, you implement three recursive methods. The methods are unrelated, and you should put them in different classes.

  1. a recursive method to convert a parameter of type long to a string representing that many U.S. cents. The string must begin with a "$" sign, and should have a comma every 3 digits, as well as a dot between the dollars and the cents. For example, the value 1 should be returned as the string "$0.01", the value 123 should be returned as the string "$1.23", the value 1051200 should be returned as the string "$10,512.00", and the value 1356162303089179 should be returned as the string "$13,561,623,030,891.79".

    You should also have a driver program to allow the user to test printing different numbers, and printing the resulting string.

  2. a recursive method to compute the Ackermann function of two numbers, a and b, defined as follows:

    This function grows very rapidly with its input values. You should test with small values such as ack(0, 0) computing 1, ack(1, 3) computing 5, ack(2, 3) computing 9.

    You must also provide a driver program for this method that allows the user to test calling with different sets of inputs.

  3. A recursive method to duplicate every element in a linked list, that is equal to a certain value. For example, if the linked list contains "foo", "bar", "foo", "baz", and the element to duplicate is "foo", the new list should contain "foo", "foo", "bar", "foo", "foo", "baz".

    This method should be embedded in the LinkedListRec.java code.

    The main method of the LinkedListRec.java class must be changed to create a linked list containing all the arguments on the command line except for the first argument.

    After creating the linked list, the main method should run this duplicate method with the first argument and the linked list, and print the result.

    Given the command line would be "foo hello foo world bar foo baz", the first "foo" is the value to duplicate, and the other arguments are the list that main must create before calling the duplicate method. So the duplicate method is called with two arguments, the string "foo", and the list "hello", "foo", "world", "bar", "foo", "baz". The result should be the list "hello", "foo", "foo", "world", "bar", "foo", "foo", "baz".

All these methods must use recursion to accomplish the stated goal. If your recursive method uses any iterative loops (for or while) or if in any way it fails to use recursion, you will get no credit for this part of the assignment. If this is not clear, please consult the TA or the instructor.

Note: you are allowed to use loops in the driver method. For example, for the last problem, you are allowed to use a loop to create the linked list to be duplicated. But your solution to the problem must use recursion.

Turning in the Assignment

Email all the java source code for your assignment to the TA following the instructions posted here. Your source code must include all the parts of each class.