The Clock Example
1. The Clock Example 2
3 public class Clock { // instance variables private int hr; private int min; private int sec; // constructors: same name as class name. No type. public Clock () { //default constructor setTime (0, 0, 0); } public Clock (int hours, int minutes, int seconds) { setTime (hours, minutes, seconds); } Variables and Constructors Calls a setter method
4 // instance methods public void setTime (int hours, int minutes, int seconds) { if (0 <= hours && hours < 24) //check if hours exceeds 24 hr = hours; else hr = 0; if (0 <= minutes && minutes < 60) //check if minutes exceeds 60 min = minutes; else min = 0; if (0 <= seconds && seconds < 60) //check if seconds exceeds 60 sec = seconds; else sec = 0; } //end of setTime Setters (1) Called method
5 // mutator methods public void incrementSeconds () { sec++; if (sec > 59) { sec = 0; incrementMinutes(); } // end if } // end incrementSeconds public void incrementMinutes () { min++; if (min > 59) { min = 0; incrementHours(); } // end if } // end incrementMinutes Setters (2) Calls another setter method
6 Setters (3) public void incrementHours () { hr++; if (hr > 23) hr = 0; } // end incrementHours } // end of Clock
7 // accessor methods public int getHours () { return hr; } //end of getHours public int getMinutes () { return min; } //end of getMinutes public int getSeconds () { return sec; } //end of getSeconds Accessors
8 public void printTime() { //prints time in the form hh:mm:ss if (hr < 10) System.out.print (“0”); System.out.print (hr + “:”); if (min < 10) System.out.print (“0”); System.out.print (min + “:”); if (sec < 10) System.out.print (“0”); System.out.print (sec); } // end printTime Display
9 Other Instance Methods public boolean equals (Clock otherClock) { //compare two times return (hr == otherClock.hr && min == otherClock.min && sec == otherClock.sec) } //end equals public void makeCopy (Clock otherClock) { //object1 = object2 hr = otherClock.hr; min = otherClock.min; sec = otherClock.sec; } public Clock getCopy() { //get a copy of a given object Clock temp = new Clock(); temp.hr = hr; temp.min = min; temp.sec = sec; return temp; } } //end class
10 Clock -hr: int -min: int -sec: int + Clock() + Clock(int, int, int) + setTime (int, int, int):void + incrementSeconds(): void + incrementMinutes(): void + incrementHours(): void + getHours(): int + getMinutes(): int + getSeconds(): int + printTime(): void + equals(Clock):boolean + makeCopy(Clock):void + getCopy():Clock UML Diagram
11 Using in the Application Class import java.util.*; public class clockApplication { static Scanner read = new Scanner (System.in); static final int SIZE = 100; public static void main (String[] args) { int ndx; //instantiate arrays of objects Clock[] arrivalTime = new Clock(SIZE); Clock[] departTime = new Clock(SIZE); //instantiate the object elements of arrays for (ndx = 0; ndx < arrivalTime.length; ndx++) { //invoke constructor with hr = 8, min = 30 and sec = 0 arrivalTime[ndx] = new Clock(8, 30, 0); // invoke constructor with hr = 15, min 30 and sec = 0 departTime[ndx] = new Clock(15, 30, 0); } //end for // update the arrival times of late employees lateEmployees(arrivalTime);//process the whole array
12 Using in the Application Class // check if two employees leave at the same time to take the same bus for example int ID1= -1, ID2 = -1; boolean sameTime = false; System.out.println (“Enter ID of employee1”); ID1 = read.nextInt(); System.out.println (“Enter ID of employee2”); ID2 = read.nextInt(); sameTime = departTime[ID1].equals(departTime[ID2]); //Copy the arrival information of empLoyee ID1 to employee ID2 arrivalTime[ID2].makeCopy(arrivalTime[ID1]); //Get a copy of the departure information of employee ID2 Clock tempInfo = new Clock(); tempInfo = getCopy(departTime[ID2]); // specify the employees that deserve an overtime //create a parallel array boolean[] overTime = new boolean[SIZE]; overTimedEmployees(departTime, overTime); } // end main
13 Using in the Application Class public static void lateEmployees(Clock[] arrival) { int employeeID = -1, arrivalHour = 0, arrivalMinute = 0, arrivalSecond = 0; do { // Assume ndx is used as employee ID System.out.println (“Enter employee ID or -1 to exit”); employeeID = read.nextInt(); if (employeeID == -1) break; System.out.println (“Enter employee arrival hour”); arrivalHour = read.nextInt(); System.out.println (“Enter employee arrival minute”); arrivalMinute = read.nextInt(); System.out.println (“Enter employee arrival seconds”); arrivalSecond = read.nextInt(); // Update employee’s data in the array. Assume ID = ndx arrival[employeeID].setTime(arrivalHour, arrivalMinute, arrivalSecond); } while (employeeID != -1); }//end lateEmployees
14 Using in the Application Class public static void overTimedEmployees(Clock[] depart, boolean[] bonus) { int ndx; Clock template = new Clock(15, 30, 0); for (ndx = 0; ndx < depart.length; ndx++) if (depart.hr > template.hr) { bonus[ndx] = true; System.out.printf (“%d deserves a bonus %n”, ndx); } //end if }//end overTimedEmployees }//end class It is recommended that you try the previous example on the computer