Harvey Mudd College

Computer Science 60

Spring 1998

Assignment 3

Due Thur. 19 February 1998

Unicalc Kernel in Java

This assignment is 40 points of your overall grade.

In the previous assignment, you constructed various functions for the unit conversion in the unicalc kernel. Familiarity with that assignment is assumed in this write-up. In this assignment, you will "port" (transcribe) those functions to Java. This assignment does not intend to make use of the object-oriented features of Java. You will need to define a few classes and constructors, but most methods can be static.

You may use your solutions to assignment 2 or mine as a basis. Please compare the two before deciding.

1. [15 points] Since Java is a strongly-typed language, you will need to define some classes representing those types. In my solution, the following classes are sufficient:

StringList

A list of elements of type String, representing a list of units. This type is defined for you in /cs/cs60/ht/examples/StringList.java and you may copy this code in its entirety. It is similar to the list of integers, IntList, discussed in class.

Quantity

A triple of values representing a compound unit, consisting of a double, a StringList numerator, and a StringList denominator.

Eqn

represents an equation. It is has a LHS which is a String and a RHS which is a Quantity.

EqnList

represents a list of Eqn's, e.g. the unicalc database.

QuantityList

represents a list of Quantity.

It is possible to do without QuantityList, depending on how you code. I realize that you could choose other names, but it would help us help you if you didn't. Another approach is to use polymorphism, but since we haven't discussed that yet, it might be better not to.

This problem is to define each of these classes including their constructors. For EqnList and QuantityList, you can essentially transcribe the code of StringList, using your text editor and a query-replace mechanism.

For Quantity, I found it helpful to have a several constructors, to reduce the messiness of code in other functions. For example, I have a constructor which makes a Quantity from a single double, one which makes one from a single unit (String), etc.

2. [10 points] Code the functions simplify, multiply, and divide from assignment 2, along with any auxiliary functions you need. These should be static methods of class Quantity.

3. [10 points] Code the functions norm and norm_unit, along with any auxiliary functions you need. These should also be static methods of class Quantity. If you used higher-order functions such as map and reduce in assignment 2, consider replacing them with versions in which the function argument is explicitly embedded in their code, so that you don't have to deal with higher-order functions in Java.

4. [5 points] Provide a user interface function convert such that convert(A, B) will perform the conversion and output the units if a direct conversion is possible, or which explains the deficit if not, just as in the previous assignment.

You will be provided with a database definition, resembling the following, but more comprehensive:

static EqnList DB =
EqnList.cons(new Eqn("foot", new Quantity(12.0, "inch")),
EqnList.cons(new Eqn("mile", new Quantity(5280.0, "foot")),
EqnList.cons(new Eqn("hour", new Quantity(60, "minute")),
EqnList.cons(new Eqn("minute", new Quantity(60, "second")),
EqnList.cons(new Eqn("mph", new Quantity(1.0, "mile", "hour")),
EqnList.nil)))));

Notice that when a function is used outside its class, you need to prefix it with the class name, such as EqnList.cons above.

You will also be provided with a main program, resembling the following, but possibly with more conversions:

static public void main(String[] arg)
{
Quantity inch = new Quantity("inch");
Quantity foot = new Quantity("foot");
Quantity mile = new Quantity("mile");
System.out.println(convert(foot, inch));
System.out.println(convert(mile, inch);
System.out.println(convert(inch, mile));
System.out.println(convert(new Quantity(1.0, "foot", "second"),
                           new Quantity("mph")));
}

Both definitions will be in the file /cs/cs60/a/a3.java, within class Unicalc.