Lab 11: Recursive types

This goal of this project is to practice writing recursive types an operations performed on them.

As usual, make a new directory for this project and cd into it.

1. Introduction

Peano numbers are a way to represent the natural numbers, specified by the following axioms:

Intuitively, a natural number n's sucessor is n + 1. Recursively, we can define a natural number as

Copy the following files from the course directory and look at them.

> cp /homeemp/tvandrun/pub/cs241/NatNumber.java .
> cp /homeemp/tvandrun/pub/cs241/PeanoDriver.java .

In the interest of time, you may skip method and instance variable documentation in this lab. You need only write file-opening documentation in the files you create and for the methods intToNatNumber() and natNumberToInt().

2. Classes and instance variables

You already have an interface, NatNumber, which you would like to implement. Open two new files, Zero.java and OnePlus.java. Write appropriate classes. What should the class hierarchy be? What instance variables should they have? Compile two new files to make sure they make sense.

3. Constructors

Look at how the driver instantiates the classes. Write appropriate constructors for Zero.java and OnePlus.java. Un-comment the first section in the driver, compile and run.

4. toString

Uncomment toString() in NatNumber.java. Write methods in your two classes that will produce a string representation that use a parenthesized system like we did with the pizza example in class. For example, 3 should be OnePlus(OnePlus(OnePlus(Zero))). Uncomment the next section of the driver. Compile and run.

All of the methods you write in Zero.java and OnePlus.java need be only one line long.

5. Successor

Uncomment succ() in NatNumber.java. Write methods in your two classes that will compute the successor to a given natural number. Uncomment the next section of the driver. Compile and run.

6. Predecessor

Uncomment pred() in NatNumber.java. Write methods in your two classes that will compute the predecessor (number that comes before) a given natural number. The problem is, what is zero's predecessor? Technically it doesn't have one. For our purposes, assume zero's predecessor is itself (a very suboptimal solution, but the best we can do). Uncomment the next section of the driver. Compile and run.

7. Addition

Uncomment plus() in NatNumber.java. Write methods in your two classes that will compute the sum of two natural numbers--- that is, the sum of the receiver and a parameter. For example, if we have

NatNumber a = ...
NatNumber b = ...

a.plus(b)

should be equivalent to a + b. Hint: write the method for zero first; it's the base case. Then think about what to do for other numbers. Uncomment the next section of the driver. Compile and run.

8. Comparisons

Uncomment isLessThanOrEqTo() in NatNumber.java. Write methods in your two classes that will compute whether one natural number (the receiver) is less than or equal to another (the parameter). Again, do zero first. This will take a little mathematical reasoning. Don't forget we're dealing only with natural numbers (not real numbers). Uncomment the next section of the driver. Compile and run.

9. As integers

Uncomment asInt() in NatNumber.java. Write methods in your two classes that will compute the int value of a natural number. Uncomment the next section of the driver. Compile and run.

10. Iterative conversion methods

Ok, now that you are used to thinking recursively, try regressing back to iterative mode. Write static methods intToNatNumber() and natNumberToInt() in the driver which convert from an int to a NatNumber and vice versa. These methods must be iterative; that means they must use loops. Do not make them recursive, and do not call the asInt() method that you just wrote. Document these in the normal way. Uncomment the next section of the driver. Compile and run.

11. Turn in

Create the script file as before (cat--only files you've written or changed, rm, compile, and run)

 > a2ps -P sp (the name of the script file)

Thomas VanDrunen
Last modified: Wed Mar 21 10:42:23 CDT 2007