This assignment shows you a possible way to implement rex-like functionality in Java. We recommend using the package known as Polya, as will be explained in class. Polya provides most of the list processing functionality of rex, except that Java has no pattern matching, so methods such as first(), rest(), and isEmpty() have to be used to get the same effect.
Chapter 5 of the text describes many of the basic implementation ideas, although Polya provides a little more flexibility, in that list elements can be arbitrary Java Objects. You may also wish to refer to the Polya web page:
http://www.cs.hmc.edu/~keller/polya/
Do not hesitate to ask if you have questions about any of this.
Your code should be in a file named Quantity.java. You should run
cs60submit Quantity.java
As always, commenting your functions and your file are important. You may wish to insert your rex definitions into Java comments to act as a coding guide.
The skeletal file
/cs/cs60/assign/4/Quantity.java
contains a main program that will provide a set of test cases. Some skeletal definitions are also provided for you. Peruse the file to see what you are given. Your program's output should be very close to the output indicated in comments at the end of that file. Note that there are two other files in the same directory:
Multiply2.java
NormalizeUnit.java
which are usable with map and reduce, if you need them. There is also a sub-directory:
polya
which contains the Polya package. The entire contents of
/cs/cs60/assign/4
should be copied to a directory into a dedicated sub-directory of yours on turing, by connecting to the sub-directory, such as:
cd ~/cs60
mkdir a4
cd a4
cp -r /cs/cs60/assign/4 .
where -r means a recursive copy (copies the sub-directories, etc.) and the . means the directory to which you are connected.
Your end objective is to implement the static method
convert(A, B, DB)
Where A and B are Quantities and DB is a database, just as in assignment 3, only this time in Java. As before, you will no doubt want to use auxiliary methods. The database and Quantity constructors are already coded for you.
You should proceed by transcribing your key rex definitions to Java, using the list abstractions in the Polya library, as explained in class. If you are not confident that your definitions are correct, then use the ones provided in the sample solution to Assignment 3 (which will be posted Thursday after midnight).
To get set up for java, make sure you have the following lines in your file .cshrc:
setenv JAVA_HOME /usr/local/jdk1.3.1
setenv CLASSPATH $JAVA_HOME/lib:/cs/cs60/java/:.
To compile the program use the command
javac Quantity.java
To execute a correctly-compiled program, use the command
java Quantity
Note that the skeletal program Quantity.java will compile and execute. However, it will not give the correct answers until you put in your functions.
You should plan on 50-100 lines of Java code to fill in your solutions in the skeleton. It is suggested that you add other test cases to unit test each of your functions as you are transcribing them from rex. Make it clear in your output where your test cases end and ours start.