-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClock.java
More file actions
102 lines (99 loc) · 2.53 KB
/
Clock.java
File metadata and controls
102 lines (99 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package unit_04.Examples.Example_12;
public class Clock {
private int hours, minutes, seconds;
public Clock(){
setTime(0, 0, 0);
}
public Clock(int hours, int minutes, int seconds){
setTime(hours, minutes, seconds);
}
public void setTime(int hours, int minutes, int seconds){
if(hours >= 0 && hours < 24){
this.hours = hours;
}else{
this.hours = 0;
}
if(minutes >= 0 && minutes < 60){
this.minutes = minutes;
}else{
this.minutes = 0;
}
if (seconds >= 0 && seconds < 60) {
this.seconds = seconds;
}else{
this.seconds = 0;
}
}
public void setHours(int hours){
if(hours >= 0 && hours < 24){
this.hours = hours;
}else{
this.hours = 0;
}
}
public void setMinutes(int minutes){
if(minutes >= 0 && minutes < 60){
this.minutes = minutes;
}else {
this.minutes = 0;
}
}
public void setSeconds(int seconds){
if(seconds >= 0 && seconds < 60){
this.seconds = seconds;
}else {
this.seconds = 0;
}
}
public int getHours(){return hours;}
public int getMinutes(){return minutes;}
public int getSeconds(){return seconds;}
public void incrementSeconds(){
seconds++;
if(seconds > 59){
seconds = 0;
incrementMinutes();
}
}
public void incrementMinutes(){
minutes++;
if(minutes > 59){
minutes = 0;
incrementHours();
}
}
public void incrementHours(){
hours++;
if(hours > 23){
hours = 0;
}
}
public boolean equals(Clock clock){
return hours == clock.hours && minutes == clock.minutes && seconds == clock.seconds;
}
public String toString(){
String time = "";
if(hours < 10){
time = "0";
}
time = time + hours + ":";
if(minutes < 10){
time = time + "0";
}
time = time + minutes + ":";
if(seconds < 10){
time = time + "0";
}
time = time + seconds;
return time;
}
public void makeCopy(Clock otherClock){
hours = otherClock.hours;
minutes = otherClock.minutes;
seconds = otherClock.seconds;
}
public Clock getCopy(){
Clock temp = new Clock(hours, minutes, seconds);
return temp;
}
}