Lab 8: First stab at classes

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.

I. Complex numbers

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

Copy the following file from the course public directory.

cp /homeemp/tvandrun/pub/235/ComplexDriver.java .

3. Writing a class

In a separate file, Complex.java, write a class to represent complex numbers. The process of writing a class involves three steps:

  1. Determine what instance variables it needs.
  2. Write a constructor (in this case, you can see from the driver program that the constructor needs to take two doubles as parameters, the real and imaginary parts.
  3. Write operations in the form of methods. In this case, your class should support

Compile and test this using ComplexDriver.java.

II. A stopwatch

1. Introduction

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.

2. Setup

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.

3. Running the stopwatch program

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.

4. Finishing the StopWatch class

The SW program expects four things from the StopWatch class (this is an example of thinking in terms of programming by contract):

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.)

III. A game clock

1. Introduction

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.

2. Setup

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

3. Finishing the game clock program

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:

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.

IV. Turn in

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.


Thomas VanDrunen
Last modified: Mon Feb 26 15:06:25 CST 2007