Morrison's 1617T2 Classes









Complex.java

Note Make sure you read Complex.html, which has complete specifications for this project. At the left, there is shell code you can download. Do not alter the file IComplex.java! Do not change any of the public method names or argument lists.

You are, however, free to create private helper methods in the class to do work behind the scenes.

Here is a brief text on complex numbers. It has all you need to know to do this project. I will give a lesson in class that discusses them de novo.

You will implement this interface using complex number operations.

Put a main in your class and place appropriate test code in it.


public interface IComplex 
{
    //adds the complex number that to this complex number
    public Complex add(Complex that);
    //adds the real number that to this complex number
    public Complex add(double that);
    public Complex subtract(Complex that);
    public Complex subtract(double that);
    public Complex multiply(Complex that);
    public Complex multiply(double that);
    public Complex divide(Complex that);
    public Complex divide(double that);
    public Complex conjugate();
    public double abs();
    public Complex pow(int n);
    public double re();
    public double im();
    //finds the angle beteeen the positive x-axis and this complex number.
    public double Arg();
    //you need to implement these
    public boolean equals(Object o);
    public boolean closeEnough(Complex that);
    public String toString();
}

For testing purposes, these bad boys are useful.

   public static final double EPSILON = 1e-6;
    public static boolean close(Complex z, Complex w)
    {
        return z.subtract(w).magnitude() < EPSILON;
    }
    public static boolean close(double z, double w)
    {
        return Math.abs(z - w) < EPSILON;
    }

You will have these three static constants.

jshell> Complex.ZERO
$6 ==> 0.0 + 0.0i

jshell> Complex.ONE
$7 ==> 1.0 + 0.0i

jshell> Complex.I
$8 ==> 0.0 + 1.0i

Here are addition, subtraction, and multiplication

jshell> /open Complex.java

jshell> Complex a = new Complex(3,4);
a ==> 3.0 + 4.0i

jshell> Complex b = new Complex(3, -4);
b ==> 3.0 - 4.0i

jshell> a.multiply(b)
$9 ==> 25.0 + 0.0i

Here we have the getters.

jshell> a.re()
$10 ==> 3.0 

jshell> a.re()
$11 ==> 4.0 

The method Arg returns the angle in (-π, π] made by this Complex with the positive x-axis.

jshell> Complex c = new Complex(1,1);
c ==> 1.0 + 1.0i

jshell> c.Arg()
$12 ==> 0.7853981633974484

jshell> Complex d = new Complex(-1,1);
d ==> -1.0 + 1.0i

jshell> d.Arg()
$13 ==> 2.356194490192345

jshell> d.Arg()/Math.PI
$14 ==> 0.75

Here we computer some powers of 1 + i.

jshell> z.pow(5)
$28 ==> -4.0 - 4.0i

jshell> z.pow(4);
$29 ==> -4.0 + 0.0i

jshell> z.pow(8)
$30 ==> 16.0 + 0.0i