Harvey Mudd College
Computer Science 60
Assignment 4, Due Thursday, Sep. 30, by midnight

Unicalc in Java !

This assignment is 50 points of your overall grade.

This assignment consists of porting the Unicalc project to Java. It will (or should) show that many of the programming techniques which rex makes possible can be translated to an imperative language. In addition, it is meant to refresh your memory for Java syntax.

For this assignment, you should create a file named Unicalc.java which contains a single class, "Unicalc," and several static member functions which implement the unit conversion computations. You do not need to write this file from scratch. In the directory /cs/cs60/assignments/a4 you will find a file named Unicalc.java -- this file has a "working" skeleton for the Java unicalc program. Copy that file to your directory and use it as the start of your program. As is, it should compile and run (though it will not pass any tests).

To compile the Unicalc Java program, use the command

javac Unicalc.java
and to run a successfully compiled program, type
java Unicalc
(You may also have included the aliases jc, je, and jce in your .cshrc file in order to save typing.)

Lines to add to your .cshrc file !

In order that Java can find the Polylist class, you will need to include these two lines in your .cshrc file:
setenv JAVA_HOME /usr/local/jdk1.2.2
setenv CLASSPATH $JAVA_HOME/lib:/cs/cs60/java/:.

Functions to implement

The same functions which were implemented in Assignment 3 need to be implemented here. convert, however, has largely been written for you, in order that the formatting of the output text remain consistent. (This will simplify the script-based grading of the assignment.) Please use the commands in the example convert function. These commands need to be enabled once your other unicalc functions are functioning.

The headers for the required functions (methods),

(1)  simplify
(2)  multiply
(3)  divide
(4)  conv_unit
(5)  norm_unit
(6)  norm
(7)  convert
as well as a main method for testing them, are provided in the Unicalc.java file mentioned above. Please do not change the signatures of those functions. For each, write code implementing the function using the Polylists from the polya package.

Polya reference

A web-based reference on polya and the Polylist class is available from this site.

Important Notes

Testing your code

The testing code provided makes it easy to manually type in Quantity Lists or strings, as appropriate, to test your functions. In addition, there is a script named UnicalcTest (in /cs/cs60/bin, which should be in your path). It requires that you run it from the directory in which you have successfully compiled your Unicalc.java code, i.e., your development directory. It takes a single argument representing the problem you would like to test (p1,p2,p3,p4,p5,p6,p7,or pAll).

    % UnicalcTest p1
    
for example, tests the simplify function. If your simplify function works correctly (and the testing script is free of bugs), the output should look like
    Running test cases in /cs/cs60/assignments/a4/p1.in
 
    The test is complete. If no errors were noted, all tests were passed.
       See the file p1.out for results.
    If there were errors, the lines prefaced with < indicate computed answers,
       while lines prefaced with > indicate correct answers.
    
If there are errors, they will look similar to
    11c11
    < (7.6 (meter second) (kg))
    ---
    > (3.0 (meter second) (kg))
    23c23
    < (7.6 () (second))
    ---
    > (3.14 () (second))
    
As explained by the script, the lines prefaced by < are those computed by the code being tested. The lines prefaced with > are the correct answers.

Don't change the testing code. We will use it to check your program.

Another suggestion

It is strongly suggested that you not try to write all of the code at once and then start to debug. DTest and debug small parts as you go. In fact, this is a good idea for almost all coding... .

Reading

You will want to have read to at least Chapter 5 of the text for this assignment. In addition, you may need to consult your Java reference (be it online or a book).

Submission

You should submit your Unicalc.java source file in the usual way, i.e., by running

% cs60submit Unicalc.java
You will be asked to input the assignment number (4).