Harvey Mudd College
Computer Science 60
Assignment 6, Due Monday, October 26, by 11:59pm

From Racket to Java...

Assuming you took CS5, at this point in CS60 youve worked with (at least) four programming languages: Python, HMMM, Racket, and now Prolog. This week - and in the coming weeks - we will add Java to that list (if it's not already there).

In addition, over the next two weeks you'll build your own task-specific language that will be able to handle Unicalc-like conversions -- this time using Java, instead of Racket.

This week you'll use or build several Java classes, i.e., custom data structures. The largest one, named OpenList will be the backbone for a Java implementation of unicalc, which you'll complete next week. With the OpenList class you'll create a Racket-like linked list that can be naturally handled recursively. Thus, OpenList "shows off" Java's data-structuring capabilities without sacrificing the generality and power of Racket and Scheme's functional style.


Submitting your code

Problems 1 should be completed individually; the other problems may be completed individually or in pairs. If you do work in a pair, please be sure both partners submit the file to their own submissions account; each should include a hash, e.g., #partnername in the pair-programming textbox -- this will help the graders give the same grade to both.

Overall, you will submit 3 files for this week's three problems:


Problem 1: Complex

This problem should be completed individually.

20 points

Java has eight built-in (primitive) types -- the most common are such as int, char, boolean, and double. It has thousands of class types in its libraries.

Yet one class type that Java does not have is Complex, representing the complex numbers commonly used by mathematicians, scientists, engineers, physicists, and Math 35 students. This problem asks you to implement a complex-number data structure (class), Complex.java. You should start with the provided file:

Complex.java.

The Point class example we developed in class will be a good reference. After all, 2d points and Complex numbers are not so different! The complete Point class is available here in Point.java.

Testing your code!    Just as in Scheme, you should test each of your methods (functions) as you write it. To do this in Java, uncomment the lines in the provided Complex class's main method that test each of your methods. For this assignment, we will check visually that the answers are what they should be, so feel free to do the same.

It's much easier to make small syntactic errors in Java than in Scheme - there is simply more code where errors can appear! To help with this, the starter file provides lots of tests in a comment in main. As you implement your Java methods, uncomment the appropriate tests and try them out!

Complex number notes     Recall that a complex number is of the form a + bi where a and b are real numbers and i is an imaginary number. Addition of two complex numbers is defined by (a + bi) + (c + di) = (a+c) + (b+d)i. Similarly, to multiply two complex numbers, we simply compute (a + bi) * (c + di) = ac + adi + bci + bdii = (ac - bd) + (ad + bc) i since ii (i times i) is -1 by definition.

In your implementation, the numbers a and b in the complex number a + bi should be represented by doubles: these will be the two data members of your class. Please use the names real and imag (as already appear in the starter file) for these data members -- this will be far more readable/memorable than a and b!

Below are the public methods (functions) which your Complex class should contain. Be sure to use exactly these names and the specified kinds of arguments and return values. You may use any additional private methods of your own design, though such helper functions are not required.


Be sure to test out your Complex class with the tests provided in main before submitting your Complex.java file.


Problem 2: BSTNode

Pair or individual

20 points

At this point you've written code for Binary Search Trees (BSTs) in two different languages: Racket & Prolog. Here, we provide a binary-search-tree data structure (class) in Java named BSTNode. For this problem, you should start with this almost-complete BSTNode class:

BSTNode.java

BSTNode has much of its basic functionality already provided. This provides both an additional example of how Java creates data structures and it's to allow you to focus on the most algorithmically interesting facets of binary search trees!

In this problem you will add the methods insert and delete for the class BSTNode.