-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTime1.java
More file actions
163 lines (145 loc) · 4.67 KB
/
Time1.java
File metadata and controls
163 lines (145 loc) · 4.67 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
/**
* Represents time - hours:minutes. Coordinates cannot be negative.
*/
public class Time1 {
private int _hours, _minutes;
private static final int MINIMUM_MINUTE_RANGE = 0;
private static final int MAXIMUM_MINUTE_RANGE = 59;
private static final int MINIMUM_HOUR_RANGE = 0;
private static final int MAXIMUM_HOUR_RANGE = 23;
private static final int MINUTES_IN_HOUR = 60;
/**
* Constructs a Time1 object. Construct a new time instance with the specified hour and minute.
* hour should be between 0-23, otherwise it should be set to 0.
* minute should be between 0-59, otherwise it should be set to 0.
* @param h - the hour of the time
* @param m - the minute of the time
*/
public Time1(int h, int m) {
if(!isTimeValid(h,m)){
_minutes = MINIMUM_MINUTE_RANGE;
_hours = MINIMUM_HOUR_RANGE;
} else {
_minutes = m;
_hours = h;
}
}
/**
* Copy constructor for Time1. Construct a time with the same instance variables as another time.
* @param other - The time object from which to construct the new time
*/
public Time1(Time1 other) {
_minutes = other._minutes;
_hours = other._hours;
}
/**
* validate if time is valid
* @return boolean
*/
private boolean isTimeValid(int hours, int minutes) {
return (isHourValid(hours) && isMinuteValid(minutes));
}
/**
* check if hour is valid
* @return boolean
*/
private boolean isHourValid(int h) {
return (h >= MINIMUM_HOUR_RANGE && h <= MAXIMUM_HOUR_RANGE);
}
/**
* check if minute is valid
* @return boolean
*/
private boolean isMinuteValid(int m) {
return (m >= MINIMUM_MINUTE_RANGE && m <= MAXIMUM_MINUTE_RANGE);
}
/**
* Returns the hour of the time.
* @return The hour of the time
*/
public int getHour(){
return _hours;
}
/**
* Returns the minute of the time.
* @return The minute of the time
*/
public int getMinute(){
return _minutes;
}
/**
* Changes the hour of the time. If an illegal number is received hour will be unchanged.
* @param num - The new hour
*/
public void setHour(int num){
_hours = (isHourValid(num)) ? num : _hours;
}
/**
* Changes the minute of the time. If an illegal number is received minute will be unchanged.
* @param num - The new minute
*/
public void setMinute(int num){
_minutes = (isMinuteValid(num)) ? num : _minutes;
}
/**
* [formatInt - fix num to 2 int like: 7 to 07]
* @param num [hour/minute] Int
* @return String
*/
public String formatInt(int num){
if(num == 0){
return "00";
} else if(num < 10){
return ("0" + num);
} else {
return "" + num;
}
}
/**
* Return a string representation of this time (hh:mm).
* @Overrides toString in class java.lang.Object
* @return String representation of this time (hh:mm).
*/
public String toString() {
return formatInt(_hours) + ":" + formatInt(_minutes);
}
/**
* Return the amount of minutes since midnight.
* @return amount of minutes since midnight.
*/
public int minFromMidnight(){
return _hours * MINUTES_IN_HOUR + _minutes;
}
/**
* Check if the received time is equal to this time.
* @param other - The time to be compared with this time
* @return True if the received time is equal to this time
*/
public boolean equals(Time1 other){
return (_hours == other._hours && _minutes == other._minutes);
}
/**
* Check if this time is before a received time.
* @param other - The time to check if this point is before
* @return True if this time is before other time
*/
public boolean before(Time1 other){
return (_hours * MINUTES_IN_HOUR + _minutes) < (other._hours * MINUTES_IN_HOUR + other._minutes);
}
/**
* Check if this time is after a received time.
* @param other - The time to check if this point is after
* @return True if this time is after other time
*/
public boolean after(Time1 other){
return !before(other);
}
/**
* Calculates the difference (in minutes) between two times. Assumption: this time is after other time.
* @param other - The time to check the difference to
* @return int difference in minutes
*/
public int difference(Time1 other) {
return (Math.abs(_hours - other._hours) * MINUTES_IN_HOUR) + Math.abs(_minutes - other._minutes);
}
}