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.javaand 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.)
setenv JAVA_HOME /usr/local/jdk1.2.2 setenv CLASSPATH $JAVA_HOME/lib:/cs/cs60/java/:.
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) convertas 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.
A web-based reference on polya and the Polylist class is available from this site.
[1.0, ["meter"], []]
S expressions are delimited by parentheses and do not use
commas to separate items. Whitespace is used instead, e.g.
(1.0 (meter) ())
Double D = (Double)(P.first());
assigns the Double object that is the first element of P to
the variable D. From there
double d = D.doubleValue();
will assign the value of the Double D to the double
d. Then, you can multiply or divide d by other
doubles. However, in order to put the resulting double value (call it
d again) into a Polylist, you will need to make a
Double object (called a "wrapper") out of it, e.g.,
Polylist P = Polylist.list(new Double(d));
As another example, the second element of P (as long as
P is a Quantity List) is another Polylist -- namely, the
Polylist of all of the units in the numerator. To get at this
numerator list, use
Polylist N = (Polylist)(P.second());
A similar command will get the list of denominator units.
(Individual elements of these numerator and denominator lists
will have to be cast to Strings.)
(1 (meter) ())
will cause problems if the "1" is to get cast to a Double. Typing
(1.0 (meter) ())
will work.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.
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... .
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).
You should submit your Unicalc.java source file in the usual way, i.e., by running
% cs60submit Unicalc.javaYou will be asked to input the assignment number (4).