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

From Racket to Java...

You've already written quite a bit of Java code, but we'll dive into some of the real functionality of Java this week. This week you'll use or build several Java classes, i.e., custom data structures.


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., #partnerlogin in the pair-programming textbox -- this will help the graders give the same grade to both.

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

Here are the starter files:


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 files:

Complex.java     and     ComplexTest.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!    We have provided a small number of tests in ComplexTest.java. We recommend that you add additional test cases, but you will not be graded on these test cases in this assignment. Your ComplexTest.java file won't compile (it will have errors) until you add the public methods described below to your file Complex.java.

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.



Problem 2: List

This problem may be done individually or in a pair. If you work in a pair, be sure to follow the CS department's pair programming practices throughout.

60 points

You will be writing the following methods, which are described in the start file List.java and can be tested using ListTest.java.


Problem 3: 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 and BSTNodeTest class:

BSTNode.java and BSTNodeTest.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!

However, in this class, we're doing something unsual for Java. We have based the class BSTNode off of Racket trees. Recall that in Racket we never changed any lists, we only made new lists. So in this BSTNode class we will do the same thing - and NEVER change any BST!

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