Lab 8: First stab at classes

The goal of this lab is to try out the basic syntax for writing classes

1. Introduction

Complex numbers are numbers that have a real part and an imaginary part (factor of -1 or i). Here are some examples of complex numbers:

5.3
29.14 + 4.3 i
8.53 i
3.2 - 2.1 i

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.

2. Setup

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 .

3. Part 1: Writing a class

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.

4. Part 2: Methods to set the instance variables.

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.

5. Part 3: Displaying complex numbers

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.

6. Part 4: Mathematical operations

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.

7. Part 5: Making instance methods

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.

8. Part 6: Making a constructor

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.

9. Extra credit

Rewrite your toString() method so that it represents the complex number in polar coordinates.

10. Turn in

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.


Thomas VanDrunen
Last modified: Thu Oct 19 15:09:06 CDT 2006