HW3 Due: Tuesday, Feb 12, 11:59pm
HW4 Due: Thursday, Feb 14, 11:59pm
Be sure to POST the GUI on your websites. It is required. Do not post source code. You send me a gist link to your source code.
Clarification on the equals method (2/12/19):
Because doubles cannot be accurately checked for equality, you need to do something else to define an equality method.
The class has a distance method. It calculates the distance between two points.
x.abs() is a method that calculates the magnitude of the complex number x.
So say you have two complex numbers x and y. They are equal if:
max(x.abs(), y.abs())) < 1E-6
Review the program on Rational Numbers. This code should serve as a guide to writing the Complex class.
Use Command Line Complex Driver, as the driver class to test your implementation of the Complex number class whose design is described below. The optional string constructor need not be implemented at this time. Email a gist of your Complex class. I will post my solution so you can continue with the next step. No submissions will be accepted after I post my solution.
HW4 will involve creating a GUI that will use this Complex Number class.
Use this website to check your complex number calculations:
Complex Class Implementation
Java provides several types of numbers, but it does not provide support for complex numbers. Your task is to implement this support.
The following table lists the kind of data and the kind of operations you will need.
|Components||Two doubles, known as the real part and the imaginary part.|
|Conjugate||Complex conjugate of
|Equality||Equality of floating point numbers is not well defined–but you can test if two numbers are approximately equal|
|Greater Than||Can be defined as having the greater distance from
|Less Than||Can be defined as having the lesser distance from
The Complex class should have 4 constructors:
- One that takes two
doubleparameters, a real part and an imaginary part
- One that takes one
doubleparameter, the real part (the imaginary part is assumed to be zero)
- One that takes no parameters, the real and imaginary parts are zero.
- (As a bonus implement this constructor): One that takes a
"99i"(allow blanks in the string)
public Complex add(Complex c)
public Complex subtract(Complex c)
public Complex multiply(Complex c)
public Complex divide(Complex c)
public double abs()
public Complex negate()
public Complex conjugate()
public double distance(Complex c)
public boolean equals(Complex c)
public boolean greaterThan(Complex c)
public boolean lessThan(Complex c)
public String toString()
All numbers should be immutable, that is, there should be no way to change their components after the numbers have been created. Most of your methods simply return a new number.
To determine whether two complex numbers are approximately equal: Notice that, in some sense,
1234600 are “more equal” than
2.0, because the percentage difference is much greater in the second case. We will therefore define two complex numbers to be “approximately equal” if the distance between the numbers divided by the abs(larger) distance is less than 1E-6.
Good comments and good indentation and formatting are required, as always.
Happy Coding …