Harvey Mudd College
Computer Science 60
Assignment 4, Due Sunday, September 28, by midnight

From Scheme to Java...

This homework introduces Java, a language designed for creating and using data structures. The first part of the assignment is to implement an OpenList data structure, which is a list of the type Scheme uses for programs and data. The OpenList "shows off" Java's data-structuring capabilities without sacrificing the generality of Scheme.

The second part of this assignment is a Scheme "unit-calculator" application. This will be followed up (next week) with a Java version of that calculator... .

Submitting your code

You will want to submit your two files

in the usual way, under homework #4.

Getting started with Java

There are many, many environments in which you might write Java code. Because CS 60 seeks to get at the fundamentals of computation (and not focus on a particular toolset like Eclipse, JCreator, Visual Studio, NetBeans, etc.) our default setup will use Java from the Dr Java environment.

However, if you have a different favorite environment, you're welcome to use it, but please be sure that you are submitting only your plain-text .java source files so that we will be able to compile and run them!

Setting up Java on the Mac    Type javac -version at the command line in the Terminal application; it should already be built-in. The lab Macs are using version 1.5.0_13, though pretty much any version will be fine. We will be concentrating on the core of the language, not the small differences among versions 1.4, 1.5, and 1.6, for example.

Setting up Java on the PC (Windows)    You might have java, but might _not_ have java development kit (JDK) which includes the compiler. You will need the compiler to run your code! To find out if you have it, look for the folder C:\Program Files\Java\jdk1.5.0_11\ - or some other version, perhaps jdk1.6.0_07 - the key is that the jdk letters are at the start of the folder name. The jre folder, which stands for java runtime environment can run software, but can not compile it. Thus, you'll need the JDK. If you don't have the JDK and are on a Windows machine, download it from Sun's Java standard edition downloads page.

Working with Java

Integrated development environments (IDEs) automate some of this process for you. There are many Java IDEs, some are described at this excellent documentation page. I would recommend one of the following three choices for working with Java:

The Problems

Problem 1: OpenList

70 points

This problem is meant to introduce the implementation of data structures in Java. In addition, it is a chance to either introduce yourself (or review) what Java looks like and how java code is structured. If you haven't used Java before, you may need to review a bit about the language. In particular, there is a starting point for your OpenList.java file on the top-level assignments page. Also, there are on-line references and tutorials available from the references page.)

Write a java class named OpenList that implements an (open) linked list data structure. Since the goal of this problem is to transition from Scheme to Java, you will be implementing lists in the same way Scheme does. In fact, this problem is, essentially, the first step in writing the Scheme programming language in Java. Open lists are not modified in the computer's memory (so that there is no worry about side effects) -- instead, each function returns a newly created list (or whatever data is appropriate).

Write your class in a file called OpenList.java. You should start with the OpenList.java file located at the top-level assignments page.

The main method of this OpenList.java contains a couple of tests. You might want to try out the file to begin with and read over the code in order to get a sense of what Java is doing (and how it's organized).

Testing your code!    Just as with your Scheme programming, you should test each of your methods as you write it. To do this, add tests to your OpenList class's main method that exercise each of your methods. It's much easier to make small syntactic errors in Java than in Scheme (all those semicolons!) Be sure to try out base cases, as well as recursive ones!

Reminder    Again, the command

     % javac OpenList.java
     
will create a file named OpenList.class in your directory. After that, running
     % java OpenList
     
will execute the code in the main method of the OpenList class.

Writing the OpenList class

First, in your OpenList class, you will want to indicate the data that constitute a list. There are two nonstatic data members (first and rest) and one static data member (emptyList) in an OpenList:

          private Object first;
          private OpenList rest;

          public static OpenList emptyList = new OpenList(null, null);
     
These are the only data members you will need in your list implementation. You may use others, if you would like -- in fact, there is considerable leeway in how you want to implement OpenList. However, it has to have all of the methods indicated below, and they need to behave as specified!

Notice that because the data member named first is of type Object, this OpenList implementation will be able to create lists of any Java Objects. Everything in Java is an Object except the types int, double, float, boolean, byte, char, and long. We will use only Strings and OpenLists for this assignment. The next problem, on the other hand, will put your OpenList class to work... .

In Java and other object-oriented languages, functions that are members of a class are often called methods.

So, in your OpenList class, implement the following methods:







Problem 2: Unicalc, a unit-calculator in Scheme

30 Points

This problem asks you to write a Scheme function, convert, which implements a "unit-calculator" application. A complete application will be able to convert any unit quantity to any compatible quantity by using an association list of known conversions. Incompatible conversions will result in a set of extra units "left over."

For example, the following call is asking "how would you convert from 1 mph (mile per hour) to 1 meter per second?" The third input argument to convert, named db is a "database" of unit conversions. This "database" is simply an association list.

> (convert  '(1 ("mile") ("hour")) '(1 ("meter") ("second")) db)
(0.447032 () ())  
This result is indicating that 1 mile-per-hour == 0.447032 * 1 meter-per-second.

Here is an example with some units left over, because it's not possible to convert miles-per-hour to feet:
> (convert  '(1 ("mile") ("hour")) '(1 ("foot") ()) db)
(1.466666 () ("second"))
This time the result is showing that 1 mile-per-hour == 1.46666 feet-per-second.

Since there were no time units in the denominator and there should have been, the result shows the basic (irreduceable) time units there. Basic units are simply those which do not appear on the left-hand side of the association list. In both of these examples, only the first few significant figures have been shown.

As these examples suggest, your convert function needs only to handle multiplicative conversions - it will not handle more complicated conversions, such as the one from Fahrenheit to Celsius.

Here are the key representations used in Unicalc:

Individual functions   You are welcome to decompose this unit-calculator into smaller functions on your own. If you do so, we will simply test your convert function. However, you are also welcome to write the functions described on this page, which will lead you to a full implementation of the unit-calculator. If you use this decomposition, we will grade each function separately.