Computer Science 262



Computer Science 221

Lab Exercises

Week 5

Objective: Gain experience writing class definitions.

Preparation: Review Chapter 4 of Wu.

Demonstrate your solution to the instructor or tutor by next Thursday.

Write a Java class Clock with three data members – the time in hours, minutes, and seconds.

Your class must have the following features:

• Three instance variables for the hours, minutes, and seconds

• Three constructors – default (initialize the time to 0), a constructor, which is passed the value of time in seconds (it should be converted to the correct value in hours, minutes, and seconds), and a constructor, which is passed three values – for hours, minutes, and seconds.

• Instance methods tick() – increments a Clock object by one second, accessor methods getHours(), getMinutes(), getSeconds(), mutator methods setHours(), setMinutes(), setSeconds(), and a mutator method setClock() with one argument – seconds to be assigned to a clock object.

• Instance method addClock() method to make the sum of two clock objects (it should have one argument – clock object)

• Instance method tickDown() method which decrements the clock object.

• Write a main() method in the Clock class to test the functionality of your objects.

• Write a separate Tester class with a main() method. The program should instantiate a Clock object using three integers (hours, minutes, seconds) read from standard input. Then tick the clock ten times, printing the time after each tick.

Some boundary values for the clock object:

(0:0:0), (0:0:59), (0:1:0), (0:59:59), (1:0:0), (23:59:59). Note: (24:0:0) is not a valid Clock value.

To complete the lab:

1. Demonstrate your finished class to your instructor, lab assistant, or tutor by using the interactive features of BlueJ to create several Clock objects and send messages to them.

2. Run the main method in your Tester class and hand in a hard copy.

Challenges:

• Add an instance method toString() (which takes zero parameters) to your class. toString() must return a String object of the form: "(3:22:34)".

• Add an instance method subtract() that takes one Clock parameter and returns the difference between the Clock receiving the message and the Clock parameter, measured in seconds.

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download