The goal of this lab is to try out the basic syntax for writing classes. You will write three classes-- one value class and two classes having state.
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.
Copy the following file from the course public directory.
cp /homeemp/tvandrun/pub/235/ComplexDriver.java .
In a separate file, Complex.java
,
write a class to represent complex numbers.
The process of writing a class involves three steps:
plus()
which takes another complex number and
returns a complex number, the sum.
times()
, similarly, but returning a product
dividedBy()
, similarly, but return the
quotient; you may have to derive (or look up on the Internet) a formula
for dividing complex numbers.
toString()
which returns a String representation
of a complex number.
Compile and test this using ComplexDriver.java
.
In this portion of the lab, you will write a class for an object that models a stopwatch. It will have operations to start running, to stop running, and to reset the time back to zero. It also will have a method that will return a String representation of the current time.
When a new stopwatch is created, its time is initially zero.
If the startStop()
method (compare with the "start/stop"
button
on a real stopwatch) is called, the time stats ticking.
If the startStop()
method is called 5 seconds later,
then its current time stands at 5 seconds.
If it is pressed yet again, time starts ticking from 5 seconds,
and after being pressed a
fourh time after 8 seconds,
the time stands at 13 second.
If the reset()
method is called, the time reverts to zero.
For this part of the lab (as also the third part and the current project) I am providing a graphical user interface (GUI) to use with your program. In this lab, the GUI will serve to illustrate the concept of sending messages to an object via methods. The window will stand for the object, and the buttons on the window will stand for the methods which may be called.
Copy the following files from the course public directory:
cp /homeemp/tvandrun/pub/235/sw.jar . cp /homeemp/tvandrun/pub/235/StopWatch.java .
The file sw.jar
is a Java archive file (hence the
extension "jar
"), which contains several classes.
I have set this up this way because the GUI requires a collection of small
classes (some of which have strange names because they are automatically
generated by the compiler), and by giving them to you in an archive file,
I've hidden the details that are irrelevant to our current purpose.
The file StopWatch.java
contains a skeleton of the class
modelling the stopwatch, which you must complete.
One of the classes archives in the jar file is called SW
,
and it is the driver or client program that tests out
StopWatch
and will make a window appear.
You can run this (after you compile StopWatch.java
)
with
java -cp .:sw.jar SW
The -cp .:sw.jar
stuff tells the Java virtual machine to
look in the current directory (".") and in the file sw.jar
for definitions of classes.
By default, it looks only in the current directory.
This will make a stopwatch window pop up. You can click on the buttons, but they won't do anything.
StopWatch
classThe SW
program expects four things from the
StopWatch
class (this is an example of thinking
in terms of programming by contract):
startStop()
that stops or starts the
stopwatch, depending on whether it is currently running or not
(respectively)
reset()
that sets the time back to zero
(but only if the watch is not running; it should be ignored
if the watch is running)
getTime()
which returns the current time
as a String; more specifically, it should be formatted
m:ss:t
,
that is, the number of minutes (even if zero), colon, the
number of seconds (always two digis), colon, the number of tenths of
a second.
The SW
program is set up so that when the program
starts, the StopWatch
class is instantiated;
whenever the "Start/Stop" button is pressed,
the startStop()
method is invoked;
whenever the "Reset" button is pressed,
the reset()
methos is invoked (important:
the method will be invoked even if the watch is running--
it's up to your class to ignore the message);
the method getTime()
is being called "constantly"
(actually every 25 milliseconds or so) and its result printed
to the screen of the window.
Write instance variables and method bodies for StopWatch
.
Think carefully about what information you need to store in order
to respond to the messages appropriately and how the methods
can use this information.
(Once again, this is just the definiing of type.)
In this final portion of the lab, you will complete a problem very similar to the previous, just a little more sophisticated. This time, instead of a stop watch, you will write a class for an object that models a game clock, such as for a sporting event. This differs because when it is running, it counts down, and also the user can add time to the clock or set it to a specific time.
Copy the following files from the course public directory.
cp /homeemp/tvandrun/pub/235/gc.jar . cp /homeemp/tvandrun/pub/235/GameClock.java .
Checkout the appearance of the game clock program by compiling
GameClock.java
and running
java -cp .:gc.jar GC
By inspecting the skeleton code of GameClock.java
and the program window, you can infer most of what you need to do:
Write an object that counts down time, but responds to
requests to get the current time, start or stop running, add
a second, add ten seconds, add a minute, or change the time
to a specificed time.
In addition, assume or do the following things:
set()
method.
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, color, another integer, interpret them
as minutes and seconds; if it is neither of these, ignore the request).
The best way to go about this problem is to get the start/stop method/button working first. Then implement each of the other button/methods one at a time, and test them as you go.
Turn in your Complex.java
class as usual (you do not need
to display the ComplexDriver.java
file, since you did not
modify it.
Email your StopWatch.java
and GameClock.java
files to Joe.R.Michalka@wheaton.edu
.