OBJECT-ORIENTED PROGRAMMING
CSC 111
The Clock Example
2
1. The Clock Example
Outline
3
1. The Clock Example
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); }
123456789
101112131415
Variables and Constructors
Calls a setter method
Calls a setter method
4
1. The Clock Example
// 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
161718192021222324252627282930313233
Setters (1)
Called method
5
1. The Clock Example
// 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
343536373839404142434445464748495051
Setters (2)
Calls another setter method
Calls another setter method
6
1. The Clock ExampleSetters (3)
public void incrementHours () { hr++; if (hr > 23) hr = 0; } // end incrementHours} // end of Clock
525354555657
7
1. The Clock Example
// 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
585960616263646566676869
Accessors
8
1. The Clock Example
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
70717273747576777879808182
Display
9
1. The Clock ExampleOther Instance Methods
public boolean equals (Clock otherClock) { //compare two times return (hr == otherClock.hr && min == otherClock.min && sec == otherClock.sec) } //end equalspublic 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
838485868788898081828384858687888990
10
1. The Clock EXAMPLE
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
1. The Clock ExampleUsing 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
123456789
10111213141516171819
12
1. The Clock ExampleUsing 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 ID2arrivalTime[ID2].makeCopy(arrivalTime[ID1]);//Get a copy of the departure information of employee ID2Clock tempInfo = new Clock();tempInfo = getCopy(departTime[ID2]);// specify the employees that deserve an overtime//create a parallel arrayboolean[] overTime = new boolean[SIZE];overTimedEmployees(departTime, overTime);} // end main
20
2122232425262728293031323334353637
13
1. The Clock ExampleUsing 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
38394041424344454647484950515253545556
14
1. The Clock ExampleUsing 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
575859606162636465666768
It is recommended that you try the previous example on the computer
Best wishes
Top Related