CSCI 201: Intro to Programming (Java)
Lab 3: Working with class Clock
Lab Task I
Write a Java class Clock for dealing with
the day time represented by hours, minutes, and seconds.
This class should NOT have method main().
public class Clock
{
Rest of your code here
}
|
Your class must have the following features:
- Three instance variables for the hours (range 0 - 23), minutes
(range 0 - 59), and seconds (range 0 - 59).
private int hours, minutes, seconds;
|
- Three constructors:
- default (with no parameters passed; is should initialize the
represented time to 12:0:0)
public Clock()
{
hours = 12; // initialize class variables
minutes = seconds = 0;
}
|
- a constructor with three parameters: hours, minutes, and seconds.
public Clock(int h, int m, int s)
{
(fill this) // initialize class variables
}
|
- a constructor with one parameter: the value of time in seconds
since midnight
(it should be converted into the time value in hours, minutes,
and seconds)
public Clock(int seconds)
{
hours = seconds / 3600;
minutes = ... (fill this)
seconds = ... (fill this)
}
|
- Instance methods:
- a set-method method setClock() with one parameter
seconds since midnight (to be converted into the time value in
hours, minutes, and seconds as above).
public void setClock(int seconds)
{
hours = seconds / 3600;
minutes = ... (fill this)
seconds = ... (fill this)
}
|
- get-methods getHours(), getMinutes(),
getSeconds() with no parameters that return the corresponding
values.
public int getHours()
{
return hours; // public interface to class variable
}
|
- set-methods setHours(), setMinutes(),
setSeconds() with one parameter each that set up the
corresponding instance variables.
public void setHours(int hours)
{
this.hours = hours; // keyword "this" is used to access the class variable
}
|
- method tick() with no parameters that increments the time
stored in a Clock object by one second.
public void tick()
{
seconds++;
if (seconds == 60) // validate seconds
{
seconds = 0; // reset seconds
minutes++; // increment minutes
if (minutes == 60) // validate minutes
Rest of your code here // make sure to validate hours
}
}
|
- method addClock() accepting an object of type Clock as a
parameter. The method should add the time represented by the parameter class
to the time represented in the current class.
public void addClock(Clock c)
{
seconds += c.seconds; // first add seconds
if (seconds >= 60) // check seconds for validity
{
seconds = seconds % 60; // validate seconds (they must be below 60)
minutes++; // update minutes
}
minutes += c.minutes; // next, add minutes
if (minutes >= 60)
Rest of your code here
}
|
Compile your code and fix possible errors.
Lab Task II
Add another class TestClock to the project with a
main() method. For this right-click on the package containing class
Clock in NetBeans IDE and use the New menu option to create a new class with
main method.
public class TestClock
{
public static void main(String[] args)
{
Rest of your code here
}
}
|
The program should:
- instantiate a Clock object using one integer seconds since
midnight obtained from the keyboard.
int seconds = instance of class Scanner.nextInt();
Clock c = new Clock(seconds);
|
- tick the clock ten times by applying its tick() method and
print out the time after each tick.
c.tick();
System.out.println(c.getHours() + ":" + (fill this for minutes and seconds));
Repeat the last 2 lines 9 times
|
Compile and run your code.
Lab Task III
Extend your code by appending to it instructions instantiating a Clock
object by using three integers
(hours, minutes, seconds) read from the keyboard.
int hours = instance of class Scanner.nextInt();
int minutes = instance of class Scanner.nextInt();
int seconds = instance of class Scanner.nextInt();
Clock c2 = new Clock(hours, minutes, seconds);
|
Then tick the clock ten
times, printing the time after each tick.
Use the following values for the Clock object in your tests:
(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.
Lab Task IV (if you wish a challenge)
- Add an instance method toString()
with no parameters to your class.
toString() must return a String representation of the Clock object
in the form "(hh:mm:ss)", for example "(03:02:34)".
- Add an instance method tickDown() which decrements the
time stored in a Clock object by one second.
- Add an instance method subtractClock() that takes one Clock
parameter and returns the difference between the time represented in the
current Clock object and the one represented by the Clock parameter,
measured in seconds.