-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTime1.java
More file actions
231 lines (183 loc) · 6.4 KB
/
Time1.java
File metadata and controls
231 lines (183 loc) · 6.4 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
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
/**
* Represents time - hours:minutes.
* @author Michal Danishevsky
* @vertion 20.04.2020
*/
public class Time1{
private int _hour;
private int _minute;
final int MAXIMUM_MINUTES=60;
final int MINIMUM_MINUTES=0;
final int MAXIMUM_HOURS=24;
final int MINIMUM_HOURS=0;
/**
* Constructs a Time1 object
* @param h Time's hour
* @param m Time's minute
*/
public Time1(int h,int m){
/* _hour get the given hour if given hour in the
* range of hour and get the value 0 if not
*/
if(h < MAXIMUM_HOURS && h > MINIMUM_HOURS)
_hour=h;
else
_hour=0;
/* _minute get the given minute if given minute in the
* range of minute and get 0 if not
*/
if(m < MAXIMUM_MINUTES && m > MINIMUM_MINUTES)
_minute=m;
else
_minute=0;
}//end of Time1
/**
* Copy the hour and minutes from other time to this time
* @param other The time object from which to construct the new time
*/
public Time1(Time1 other){
_hour=other._hour;
_minute=other._minute;
}//end of Time1
/**
* Return the time's hour
* @return Time's hour
*/
public int getHour(){
return _hour;
}//end of getHour
/**
* Return the time's minute
* @return Time's minute
*/
public int getMinute(){
return _minute;
}//end of getMinute
/**
* Sets the time's hour to new given hour if the new given hour
* in the range of hour if not the hour stay the same hour
* @param num New hour
*/
public void setHour(int num){
if(num < MAXIMUM_HOURS && num > MINIMUM_HOURS)
_hour = num;
}//end of setHour
/**
* Sets the time's minute to new given minute if the new given minute
* in the range of minute if not the minute stay the same minute
* @param num New minute
*/
public void setMinute(int num){
if(num < MAXIMUM_MINUTES && num > MINIMUM_MINUTES)
_minute = num;
}//end of setMinute
/**
* Returns a string representation of this time ("hh:mm")
* @return This time in pattern hh:mm
*/
public String toString(){
if(_hour < 10 && _minute < 10)
return "0" + _hour + ":" + 0 + _minute;
//the minute and the hour are smaller than ten
else if(_hour < 10)
return "0" + _hour + ":" + _minute;
//only the minute is bigger than ten
else if(_minute < 10)
return _hour + ":" + 0 + _minute;
//only the hour is bigger than ten
else
return _hour + ":" + _minute;
//the minute and the hour are bigger than ten
}//end of toString
/**
* Return how much minutes passed from the midnight (00:00)
* @return The minutes from midnight
*/
public int minFromMidnight(){
return (_hour * MAXIMUM_MINUTES + _minute);
}//end of minFromMidnight
/**
* Checks if the received time is equal to this time
* @param other The time to be compared with this time
* @return True if they are the same time and
* false if they are differents times
*/
public boolean equals(Time1 other){
if(_minute == other._minute && _hour == other._hour)
return true;//there is the same time
else
return false;//they are differents times
}//end of equals
/**
* Check if this time before the other time
* @param other The time to be compared with this time
* @return True if this time before the other time else it is return false
*/
public boolean before(Time1 other){
if(_hour == other._hour)
if(_minute < other._minute)
return true;//this time before the other time
if(_hour < other._hour)
return true;//this time before the other time
else
return false;/* they are the same time or
the other time before this time */
}//end of before
/**
* Check if this time after the other time
* @param other The time to be compared with this time
* @return True if this time before the other time else it is return false
*/
public boolean after(Time1 other){
if(other.before(this))
return true;/*other time is before this time so
this time after the other time*/
else
return false;/*the times are equals or other
time is after this time*/
}//end of after
/**
* Calculates the difference (in minutes) between two times.
* Assumption: this time is after other time
* @param other The time to check the difference
* @return The difference in minute
*/
public int difference(Time1 other){
int totalMinuteDifference;
int minuteDifference = _minute - other._minute;//minutes difference
int hourDifference = _hour - other._hour;//hour difference
/*minutes can't to be negetive so if it
happend need convert hour to minutes*/
if(minuteDifference < MINIMUM_MINUTES){
hourDifference--;
minuteDifference = minuteDifference + MAXIMUM_MINUTES;
}//end of if
//total the difference in minutes
totalMinuteDifference = minuteDifference + MAXIMUM_MINUTES * hourDifference;
return totalMinuteDifference;
}//end of difference
/**
* Add minutes to this time to make new time
* @param num Minutes to add
* @return The new time
*/
public Time1 addMinutes(int num){
Time1 newTime = new Time1(this);//make new time
newTime._minute += num;//add the minutes
num = newTime._minute / MAXIMUM_MINUTES;//only hours stay to add
newTime._minute %= MAXIMUM_MINUTES;//they are only 60 minuts in hour
newTime._hour += num;//add the hours
newTime._hour %= MAXIMUM_HOURS;//they are only 24 hours in day
/*minutes can't to be negetive so if it
happend need convert hour to minutes*/
if(newTime._minute < MINIMUM_MINUTES){
newTime._hour--;
newTime._minute += MAXIMUM_MINUTES;
}//end of if
/*hours can't to be negetive so if it
happend need convert day to hours*/
if(newTime._hour < MINIMUM_HOURS)
newTime._hour += MAXIMUM_HOURS;
return new Time1(newTime);
}//end of addMinutes
}//end of class