-
Notifications
You must be signed in to change notification settings - Fork 0
/
Time2
244 lines (194 loc) · 7.62 KB
/
Time2
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//<Time2> add a tick method
//CSIS312 - <Assignment1-2>
//Citations if necessary>--
public class Time2 {
private int totalSeconds; //0-86,400
// Time2 no-argument constructor:
// initializes each instance variable to zero
public Time2()
{
this(0); // invoke constructor with three arguments
}
// Time2 constructor: hour supplied, minute and second defaulted to 0
public Time2(int hour)
{
this(hour, 0, 0); // invoke constructor with three arguments
hour = totalSeconds / 60 / 60;
}
// Time2 constructor: hour and minute supplied, second defaulted to 0
public Time2(int hour, int minute)
{
this(hour, minute, 0); // invoke constructor with three arguments
minute = (hour - 12) /60;
}
// Time2 constructor: hour, minute and second supplied
public Time2(int hour, int minute, int second)
{
// new Time2(12, 25, 42)
if (hour < 0 || hour >= 24)
throw new IllegalArgumentException("hour must be 0-23");
if (minute < 0 || minute >= 60)
throw new IllegalArgumentException("minute must be 0-59");
if (second < 0 || second >= 60)
throw new IllegalArgumentException("second must be 0-59");
setHour(hour);
setMinute(minute);
setSecond(second);
// this.totalSeconds += totalSeconds;
}
// Time2 constructor: another Time2 object supplied
public Time2(Time2 time)
{
// invoke constructor with three arguments
this(time.getHour(), time.getMinute(), time.getSecond());
}
// Set Methods
// set a new time value using universal time;
// validate the data
public void setHour(int hour, int minute, int second)
{
if (hour < 0 || hour >= 24)
throw new IllegalArgumentException("hour must be 0-23");
if (minute < 0 || minute >= 60)
throw new IllegalArgumentException("minute must be 0-59");
if (second < 0 || second >= 60)
throw new IllegalArgumentException("second must be 0-59");
/*setHour(hour);
setMinute(minute);
setSecond(second);
*/
}
// validate and set hour
public void setHour(int hour)
{
if (hour < 0 || hour >= 24)
throw new IllegalArgumentException("hour must be 0-23");
// totalSeconds += totalSeconds - (getHour() * 60 * 60);
totalSeconds += totalSeconds + (hour * 60 * 60);
//setHour(hour);
}
// validate and set minute
public void setMinute(int minute)
{
if (minute < 0 || minute >= 60)
throw new IllegalArgumentException("minute must be 0-59");
// totalSeconds = totalSeconds -(getMinute() * 60 * 60);
totalSeconds = totalSeconds +(minute * 60);
// setMinute(minute);
}
// validate and set second
public void setSecond(int second)
{
if (second < 0 || second >= 60)
throw new IllegalArgumentException("second must be 0-59");
// totalSeconds = totalSeconds - (getSecond() * 60 * 60);
totalSeconds += second;
//setSecond(second);
}
// Get Methods
// get hour value
public int getHour()
{
int hour = (totalSeconds/60)/60;
return hour;
}
// get minute value
public int getMinute()
{
return totalSeconds/60%60;
}
// get second value
public int getSecond()
{
return totalSeconds % 60;
}
//adding a second
public int tick(){
totalSeconds = (totalSeconds + 1);
int tick = totalSeconds;
return tick;
//string tick = displayTime(getSecond +1);
}
//adding a minute
public int incrementMinute(){
int incrementMinute = totalSeconds + 60;
totalSeconds = incrementMinute;
return incrementMinute;
}
//adding an hour
public int incrementHour(){
int incrementHour = totalSeconds + (60 * 60);
totalSeconds = incrementHour;
return incrementHour;
}
// convert to String in universal-time format (HH:MM:SS)
public String toUniversalString()
{
return String.format(
"%02d:%02d:%02d", getHour(), getMinute(), getSecond());
}
// convert to String in standard-time format (H:MM:SS AM or PM)
public String toString()
{
return String.format("%d:%02d:%02d %s",
((getHour() == 0 || getHour() == 12) ? 12 : getHour() % 12),
getMinute(), getSecond(), (getHour() < 12 ? "AM" : "PM"));
}
} // end class Time2
-----------------------------------------------
public class Time2Test {
public static void main(String[] args)
{
System.out.println("Lisa Tidball - Assignment 1-2");
Time2 t1 = new Time2(); // 00:00:00
Time2 t2 = new Time2(2); // 02:00:00
Time2 t3 = new Time2(21, 34); // 21:34:00
Time2 t4 = new Time2(12, 25, 42); // 12:25:42
Time2 t5 = new Time2(t4); // 12:25:42
System.out.println("Constructed with:");
displayTime("t1: all default arguments", t1);
displayTime("t2: hour specified; default minute and second", t2);
displayTime("t3: hour and minute specified; default second", t3);
displayTime("t4: hour, minute and second specified", t4);
displayTime("t5: Time2 object t4 specified", t5);
displayTime("t4: hour, minute, and second specified", t4); //shows the time before calling tick
t4.tick(); //call tick to add a second to t4's time
displayTime("t4: hour, minute, and second specified", t4);
displayTime("t4: hour, minute and second specified", t4); //Shows the time before calling incrementMinute
t4.incrementMinute(); //call incrementMinute to add 60 seconds to t4’s time
displayTime("t4: hour, minute and second specified", t4); //Shows the time after calling incrementMinute
displayTime("t4: hour, minute and second specified", t4); //Shows the time before calling incrementHour
t4.incrementHour(); //call incrementHour to add 3600 seconds to t4’s time
displayTime("t4: hour, minute and second specified", t4); //Shows the time after calling incrementHour
// attempt to initialize t6 with invalid values
try
{
Time2 t6 = new Time2(27, 74, 99); // invalid values
}
catch (IllegalArgumentException e)
{
System.out.printf("%nException while initializing t6: %s%n",
e.getMessage());
}
}
// displays a Time2 object in 24-hour and 12-hour formats
private static void displayTime(String header, Time2 t)
{
System.out.printf("%s%n %s%n %s%n",
header, t.toUniversalString(), t.toString());
}
} // end class Time2Test
/**************************************************************************
* (C) Copyright 1992-2014 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
*************************************************************************/