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

Math.abs(x.distance(y)/Math.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:

https://www.hackmath.net/en/calculator/complex-number

## 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.

Complex numbers | |
---|---|

Components | Two doubles, known as the real part and the imaginary part. |

How written | `a+bi` (or` a-bi` , if `b` is negative), where `a` is the real part and `b` is the imaginary part; the “i” is a symbol denoting “imaginary” |

Addition | `(a+bi) + (c+di)` is `(a+c)+(b+d)i` |

Subtraction | `(a+bi) - (c+di)` is `(a-c)+(b-d)i` |

Multiplication | `(a+bi) * (c+di)` is `(ac-bd)+(ad+bc)i` |

Division | `(a+bi) / (c+di)` is `(ac+bd)/(c` |

Absolute Value | `sqrt(a` ) (Same as distance from `0+0i` ) |

Negation | `-(a+bi)` is `-a-bi` |

Conjugate | Complex conjugate of `a+bi` is `a-bi` |

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 `0+0i` |

Less Than | Can be defined as having the lesser distance from `0+0i` |

Distance | Distance between `a+bi` and `c+di` is `sqrt((a-c)` |

Zero | Zero is `0+0i` |

The **Complex** class should have 4 constructors:

- One that takes two
`double`

parameters, a real part and an imaginary part - One that takes one
`double`

parameter, 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
`String`

such as`"3+5i"`

, or`"17.5-21.3i"`

, or`"3.1416"`

, or`"99i"`

(allow blanks in the string)

And these `public`

methods:

`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, `1234500`

and `1234600`

are “more equal” than `0.5`

and `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 …