The goal of this lab is to gain familiarity with writing simple classes.
Change into the appropriate directory, and then clone the repository for this week's lab:
There will be two subdirectories in youhg clone /cslab/class/csci235/lab7
lab7
directory, one
for each part of this lab.
Complex numbers are numbers that have a real part and an imaginary part (factor of squart root of -1 or i). Here are some examples of complex numbers:
Some programming languages, such as Fortran, have a primitive complex number type. Java does not. We will write a class for complex numbers (including some operations) in this lab.
Change into the complex
directory in your working copy.
You can look at the file ComplexDriver.java
, which reads a
couple of complex numbers from the keyboard and then prints the result of
several arithmetic operations performed on them.
You will not need to modify this file.
Now look at Complex.java
, This provides you with the
skeleton for a complex number class: the instance variables and
constructor have been provided, but you will need to write the rest of the
methods.
Glance down ComplexDriver
and get a basic feel for what it does.
We input two complex numbers from the user, print them out,
and calculate their sum, product, and quotient.
There are methods to do these operations, but not all of them are
finished.
A. First, implement the method toString()
to make a string representation of a complex number.
Currently it returns an "not implemented"
.
If your number has a 3.1 real part and 5.7 imaginary part,
it should produce a string like 3.1+5.7i
.
You should now be able to test your program, verifying that it prints correctly the numbers that were input. Take some care to do reasonable things when either (or both!) of the real or imaginary part is zero.
(You may want to commit this to you repository when you are satisfied that it works correctly.)
B. Now take a look at the plus()
method, and find
the place in ComplexDriver.java
where it is invoked:
answer = comp1.plus(comp2);
This call involves two instances of class Complex
. The
method called belongs to the one on the left of the dot; we often call
this one the receiver of the call. You can think of this call as
sending a message to comp1
asking it, "give me the result of
adding comp2
to yourself".
You need to fill in the body of plus()
. Note that you
refer to the instance variables of the receiver with unqualified names,
while you refer to the instance variables of the parameter
other
using qualified names, such as other.real
.
C.In similar fashion, complete the methods minus()
,
times()
and dividedBy()
.
Compile and test. (And, probably, commit.)
The second example in the lab is a very different kind of object, one that models a game clock, such as a sporting event. This will give you a feel for the range of things that objects can be used to model or simulate.
Change to its directory by moving up one directory, and then down:
cd .. cd gameclock
First try compiling and running the game clock program.
javac GameClock.java java -cp .:gc.jar GC &
(Ask me after class if you want to know what
the -cp .:gc.jar
stuff means.)
A window will pop up (yes, this is your first program with a graphical user interface, or GUI) representing the clock. You'll notice that the clock has a current time left, buttons for stopping and starting the clock, for adding seconds, ten seconds, and minutes, and for setting the clock to a specific time.
Try the buttons out. You'll see that although you can add time to the clock, the button to start it running doesn't work, and neither does the button to set the clock to a specific time. You will implement these.
GameClock
classOpen the file GameClock.java
.
You will notice the class has three instance variables:
The time left on the clock (timeLeft
),
the time-of-day when we updated
the time left (timeMark
),
and whether or not the clock is currently running
(running
).
Internally we keep time by milliseconds (even though
the clock will display only in seconds).
We also use the long
type instead of
int
, since that's what the Java
method System.currentTimeMillis()
uses.
Look also at the instance methods. You'll notice that the methods correspond to the buttons on the window. I've written the GUI component to this program so that the appropriate methods will be invoke on the object whenever the user clicks on the buttons.
The methods addSecond()
,
addTenSeconds()
,
and addMinute()
are written for you.
Figure out how they work.
The method getTime()
looks like
it's written, but it's actually incomplete.
Here is how the clock should work:
When the clock is started, we record the time-of-day
(we call System.currentTimeMillis()
)
and set the clock's state to be "running."
Whenever we are asked for the current time on the clock
(and we will be asked continually, several times a second),
then we find the current time and subtract the
recorded time from it.
This gives us how much time has elapsed since
the last time we checked the time-of-day,
and how much we should decrement the time left on the game clock.
In light of this,
startStop()
so that it toggles the running
boolean variable
and, when appropriate, records the time.
getTime()
so that, if the clock
is currently running, it will update the time left
on the clock before returning the string representation.
setTime()
method.
Write setTime()
so that it interprets this
string and sets the time left on the clock appropriately.
Assume this text is an integer and interpret it as the
number of minutes (extra credit: assume nothing; interpret it as minutes
if it is just an integer, but if it is an integer, colon, another integer
(for example, "5:15"),
interpret them as minutes and seconds; if it is neither of these, ignore
the request).
Compile GameClock.java
, and run the program
as indicated above.
From your lab7
directory, make a typescript as you change
into the complex
directory and run the program. (Don't
forget to end the script.) It doesn't make sense to make a script of the
gameclock program.
From the lab7
directory, print your typescript and source
files with the command (all on one line):
a2ps --file-align=fill typescript complex/Complex.java gameclock/GameClock.java
Turn in the same three files electronically as lab7
.