The goal of this lab is to try out the basic syntax for writing classes
Complex numbers are numbers that have a real part
and an imaginary part (factor of
Some programming languages, like Fortran, have a primitive complex number type. Java does not. We will write a class for complex numbers (including some operations) in this lab.
As, usual, move into your cs241
directory,
make a directory for this assignment, and change into it.
mkdir cs235 cd cs235 mkdir lab8 cd lab8
Then copy the file
ComplexDriver.java
from the class directory.
cp /homeemp/tvandrun/pub/235/ComplexDriver.java . cp /homeemp/tvandrun/pub/235/ComplexDriver2.java . cp /homeemp/tvandrun/pub/235/ComplexDriver3.java .
In a separate file, Complex.java
,
write a class containing the necessary data for a complex number.
Remember, we are assuming that the real part is, well, a real
number; also, assume the coefficient to i is also real.
Document this class properly.
Compile it (javac Complex.java
).
Then uncomment the relevant section in ComplexDriver.java
,
compile, and run.
It should not display any output yet.
The driver calls methods setReal()
and
setImaginary()
, which each take a complex number
and a double.
Implement these methods (that is, fill in the bodies for the method
stubs you'll find at the end of the file ComplexDriver.java
).
Remember that you are not creating a new complex number with these;
rather, each method should modify a complex number it is given.
Document these methods; uncomment the relevant parts of the driver;
compile and test.
Unlike the program in class yesterday, the driver does not
refer to the instance variables when displaying the complex number
(after all, when I wrote the driver, I could not have predicted what
you would call them).
Instead, it calls a method to make a String
that represents the complex number.
Implement the method complexToString()
.
Make the numbers look nice, as exemplified at the beginning of this
lab description.
Document, uncomment, compile, and test.
Now implement the methods add()
,
mul()
, and div()
.
Make sure you remember (or derive, or at least look up)
how complex arithmetic works before implementing it.
Remember that these methods create a new complex number
that is the answer, and returns it.
They do not modify the complex numbers they are given.
Document, uncomment, compile, and test.
Now open ComplexDriver2.java
.
You will not make any modification to this file; you will
only modify Complex.java
to work with it.
Write methods in the class Complex.java
equivalent to the methods in ComplexDriver.java
.
Make setReal()
, setImaginary()
,
and complexToString()
(renamed toString()
--also,
this one needs to be public)
to be instance methods;
and add()
, mul()
, and div()
to be static.
Pay careful attention to how ComplexDriver2
uses these.
Compile and test.
Remember that you can write a special method called a constructor
which will initialize the instance variables.
You will now add two constructors (like other methods, constructors can
be overloaded), which will
be used by ComplexDriver3
.
Copy and paste the following as the first constructor (I'll explain later why you need
to do this):
Complex() {}
Now write a second constructor that receives to values and initializes the
real and imaginary instance variables to those values.
Look at how ComplexDriver3
instantiates Complex
.
Rewrite your toString()
method so that
it represents the complex number in polar coordinates.
Display your Complex.java
and ComplexDriver.java
,
but compile and run all three versions of the driver.
The older versions of the driver should still work, even though you have
made changes to Complex.java
.
> a2ps -P sp (the name of the script file)
(This will print "two up", meaning two pages shown next to each other
on one pice of paper. If you use a2ps
on a Java file, it
will format it nicely like in the handouts I've given in class.
The command lpr
works similarly except it does no formatting
and doesn't print two up by default.)
Then turn in the hard copy to me.