Download presentation
Presentation is loading. Please wait.
Published byIsaac Perry Modified over 9 years ago
1
The Clock Example
2
1. The Clock Example 2
3
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); } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Variables and Constructors Calls a setter method
4
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 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 Setters (1) Called method
5
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 Setters (2) Calls another setter method
6
6 Setters (3) public void incrementHours () { hr++; if (hr > 23) hr = 0; } // end incrementHours } // end of Clock 52 53 54 55 56 57
7
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 58 59 60 61 62 63 64 65 66 67 68 69 Accessors
8
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 70 71 72 73 74 75 76 77 78 79 80 81 82 Display
9
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 83 84 85 86 87 88 89 80 81 82 83 84 85 86 87 88 89 90
10
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
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 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
12
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 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
13
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
14
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 57 58 59 60 61 62 63 64 65 66 67 68 It is recommended that you try the previous example on the computer
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.