You will build an applications prograamming interface for complex numbers. Here is pro tip. As I build the BigFraction classes, you should build this class as you see me do various things (make constructor, define arithmetic, etc. It will help you to understand what is happening.
What is IComplex.java
You will not need to
modify this file in any way. It is the design contract for your Complex class.
Place it in the same folder as Complex.java
and compile both
files. Notice it specifies the headers of all of the required methods.
The static method IComplex.closeEnough()
can be used to check
if floating point quantities are suitably close. Use it in lieu of checking
floating-point numbers for equality.
public interface IComplex
{
public Complex add(Complex that);
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();
public double Arg();
public boolean equals(Object o);
public String toString();
public static boolean closeEnough(Complex z, Complex w)
{
return z.subtract(w).abs() < 1e-6;
}
}
Complex.java This is the file you will code in. It is written in such a manner that everything will compile.
Let's make our complex numbers immutable by declaring all state to be
final
. This prevents state from being reassigned, and since the
state is primitive, state cannot be changed.
As you know, Python has a builtin complex number type. Use it to check against as you code.
public class Complex implements IComplex
{
public static final Complex ZERO;
public static final Complex ONE;
public static final Complex I;
static
{
ZERO = null;
ONE = null;
I = null;
}
public Complex(double re, double im)
{
}
public Complex(double re)
{
}
public Complex()
{
}
public Complex add(Complex that)
{
return null;
}
public Complex add(double that)
{
return null;
}
public Complex subtract(Complex that)
{
return null;
}
public Complex subtract(double that)
{
return null;
}
public Complex multiply(Complex that)
{
return null;
}
public Complex multiply(double that)
{
return null;
}
public Complex divide(Complex that)
{
return null;
}
public Complex divide(double that)
{
return null;
}
public Complex conjugate()
{
return null;
}
public double abs()
{
return 0;
}
public Complex pow(int n)
{
return null;
}
public double re()
{
return 0;
}
public double im()
{
return 0;
}
public double Arg()
{
return 0;
}
public boolean equals(Object o)
{
return false;
}
public String toString()
{
return "";
}
}