To Home Page

CSC 212 -- Java Programming II

 

Lab Exercise Ex2

  1. Clock Class

    1. Write code that defines a Clock class according to these UML specs.

      The hr instance variable records time on a 24 hour clock: 0 is midnight, 6 is 6am, 12 is noon and 23 is 11pm.

      The constructor should create object with instance variables initialized to the time and date 0:00:00 January 1, 1 A.D.

      The setTimeDate method should initialize the values of the instance variables. You choose the order of the parameters. You also decide what to do if any values are out of range.

      When the tick method is executed, add one to sec. If the resulting value of sec is 60, set is back to 0 and update the other instance variables as necessary.

    2. Write a TestClock class that tests all of the methods in your Clock class.

      At a minimum, include these dates when testing the tick method: January 31, April 30, February 28 for leap and non-leap years, February 29 for leap and non-leap years, December 31.

    3. Add code to your TestClock class that creates a Clock object initialized to the date and time 12 noon, January 24, 2008. Compute the dates 1 million and 10 million seconds in the future by repeatedly calling the tick method with for loops.

Sample Code for the Clock Class

Disclaimer: this code has not been fully tested; watch out for typos.