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.
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 two files for this week's problems:
Here are the starter files:
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. You should not modify any of the provided test cases.
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 methods are not required.
public Complex( double real_in, double imag_in )
public Complex( )
// method: toString
// in: no inputs
// out: the String version of this Complex object
public String toString()
{
String sign = "+"; // default sign is plus
if (this.imag < 0.0) {
sign = "-"; // if imag is negative, make it a minus
}
return "" + this.real + " " + sign + " " + Math.abs( this.imag ) + "i";
}
// method: equals
// in: any object o
// out: true if o is Complex and its contents
// match this's contents
public boolean equals(Object o)
{
if (o instanceof Complex)
{
Complex c = (Complex)o; // cast o to a Complex
// now we can access c's imaginary and real parts...
return c.imag == this.imag && c.real == this.real;
}
else
{
return false;
}
}
public Complex add( Complex c )
public Complex negate( )
public Complex conjugate( )
public Complex multiply( Complex c )
To divide one complex number by another, first multiply both the numerator and denominator by the conjugate of the denominator. That ensures that the denominator is a real value! Then, you can simply divide both the real and imaginary components of the numerator by that value.
Here is the method signature:
public Complex divide( Complex c )
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.