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.
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()
.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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)