-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClock24.java
124 lines (118 loc) · 2.12 KB
/
Clock24.java
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
* Created by Noah Shaw
*/
public class Clock24 {
//Attributes
private int hours;
private int minutes;
private boolean isAM;
//Constructors
public Clock24()
{
this.hours = 0;
this.minutes = 0;
this.isAM = true;
}
public Clock24(int aHours, int aMinutes)
{
this.setHours(aHours);
this.setMinutes(aMinutes);
}
//Accessors
public int getHours()
{
return this.hours;
}
public int getMinutes()
{
return this.minutes;
}
public boolean getIsAM()
{
return this.isAM;
}
//Mutators
public void setHours(int aHours)
{
if(aHours >= 0 && aHours <= 23)
{
this.hours = aHours;
}
}
public void setMinutes(int aMinutes)
{
if(aMinutes >= 0 && aMinutes <= 59)
{
this.minutes = aMinutes;
}
}
public void setIsAM(boolean aIsAM)
{
this.isAM = aIsAM;
}
//Methods
public void setTime(int aHours, int aMinutes)
throws TimeFormatException
{
if(aHours < 0 || aMinutes > 59)
{
throw new TimeFormatException();
}
if(aHours == 12)
{
aHours = 12;
this.setIsAM(true);
}
else if(aHours > 12)
{
aHours = aHours - 12;
this.setIsAM(false);
}
else if(aHours < 12)
{
this.setIsAM(true);
}
this.setHours(aHours);
this.setMinutes(aMinutes);
}
public void setTime(String aString)
throws TimeFormatException
{
try
{
int aHours = Integer.parseInt(aString.substring(0, aString.indexOf(":")));
int aMinutes = Integer.parseInt(aString.substring(aString.indexOf(":") + 1, aString.length()));
this.setTime(aHours, aMinutes);
}
catch(TimeFormatException e)
{
System.out.println("EXCEPTION: Incorrect time!");
}
catch(Exception e)
{
System.out.println("EXCEPTION: Wrong format!");
}
}
public void printTime()
{
String amPm = "";
if(this.isAM == true)
{
amPm = "AM";
}
else if(this.isAM == false)
{
amPm = "PM";
}
String minutes = "";
if(this.minutes == 0)
{
minutes = "00";
}
else
{
minutes = Integer.toString(this.minutes);
}
System.out.println(this.hours+":"+minutes+" "+amPm);
}
}