What is an API? It is an application programming interface. Essentially, it is a new data type along with the documentation needed to use it properly. We will use finality to make these objects immutible.
What are Complex Numbers? Open complex.pdf. It explains all. I will do some discussion in class, too.
This is your chance to create an API You will create a
class for doing computation with complex numbers. It will have all features
listed here. Use the ideas from the BigFraction
case study.
Your final product should have a similar appearance to the product in the
textbook.
Tip Build this class as I build the
BigFraction
API. As I implement features in
BigFraction
, you should implement those features
in Complex.java
; it will help you to follow the
discussion. Also, you will have a good model as to how
to proceed. This project will help you to understand the whole process
of creating an API.
Tip Compile early and often!
Also you may insert a main
method. Test your
code as you go along. Even better, write test code for each
method before writing the method.
Static Constants Our class will have three static constants
as shown here. Initialize them in a static
block.
Complex.ONE
This is 1 + 0i.Complex.ZERO
This is 0 + 0i.Complex.I
This is 0 + i.In mathematical parlance, we use \(|z|\) for the magnitude of a complex number. Notice how this is an extension of the definition of absolute value for a real number.
For a complex number \(z = a + ib\), its argument is defined as \(\arcsin(b/|z|)\). This it the angle it makes, as a vector, with the positive real (\(x\)-axis) in the complex plane.
State Your class wlll have
private state variables re
and
im
that are doubles
.
Methods Your public interface will look like this. You may add any private or other methods as you see fit.
Complex(double re, double im)
This
constructor creates re + i*im
Complex(double re)
This
constructor creates re + i*0
Complex()
This
constructor creates 0 + i*0
String toString()
returns a string representation that
looks like a + ib
or a - ib
.boolean equals(Object o)
returns true
if o
is a Complex
and if its real and imaginary
parts agree with those of this complex number.a + ib
or a - ib
.
Complex add(Complex that)
Returns this + that
Complex subtract(Complex that)
returns this - that
Complex multiply(Complex that)
returns this * that
Complex divide(Complex that)
returns this / that
Complex conjugate()
returns the conjugate of this
Complex magnitude()
returns |this|
Complex pow(int n)
returns thisn
double re()
returns the real part of this
double im()
returns the imaginary part of this
double Arg()
returns the argument of this
Javadoc You will also javadoc this class so it is usable by someone who does not see your code. This is an important part of the project.
Shell Code Download the shell code on the left; there are two files Complex.java and IComplex.java. Place them in the same folder and open them with DrJava. They will compile right out of the box, since all methods bear an appropriate stub.
Do not change the contents of IComplex.java
at all. Compile
it and keep it in the same directory as Complex.java
Do not
change any of the method names or signaures in Complex.java
. You
may add methods to this class if you wish. Of course, you may also add private
methods to do tasks behind the scenes.
Turning This In All you need turn in is the
file Complex.java
; I can generate anything else that
is needed by compiling.