package test2; /** * A class that represents the hour on a 24-hour clock. * *
* A 24-hour clock (sometimes called military time) uses the numbers 0-23 to * represent the hour of the day starting at midnight. The mapping between a * 24-hour clock and a 12-hour clock is shown in the table below: * *
24-hour clock hour | *12-hour clock hour | *
---|---|
0 | *12AM | *
1 | *1AM | *
2 | *2AM | *
3 | *3AM | *
4 | *4AM | *
5 | *5AM | *
6 | *6AM | *
7 | *7AM | *
8 | *8AM | *
9 | *9AM | *
10 | *10AM | *
11 | *11AM | *
12 | *12PM | *
13 | *1PM | *
14 | *2PM | *
15 | *3PM | *
16 | *4PM | *
17 | *5PM | *
18 | *6PM | *
19 | *7PM | *
20 | *8PM | *
21 | *9PM | *
22 | *10PM | *
23 | *11PM | *
true
if hour
is a valid hour on a
* 24-hour clock, and false
otherwise.
*
* @param hour
* an hour on a 24-hour clock
* @return true
if hour
is a valid hour on a
* 24-hour clock, and false
otherwise.
*/
public static boolean isValid(int hour) {
return hour >= 0 && hour <= 23;
}
/**
* Returns true
if hour
is a valid hour on a
* 12-hour clock and ampm
is equal to one of
* Hour.AM
or Hour.PM
, and false
* otherwise.
*
* @param hour
* an hour on a 12-hour clock
* @param ampm
* the string Hour.AM
or Hour.PM
* @return true
if hour
is a valid hour on a
* 12-hour clock and ampm
is equal to one of
* Hour.AM
or Hour.PM
, and
* false
otherwise
*/
public static boolean isValid(int hour, String ampm) {
return hour >= 1 && hour <= 12 && (ampm.equals(Hour.AM) || ampm.equals(Hour.PM));
}
/**
* Checks if an hour and String pair form a valid hour on a 12-hour clock.
*
* @param hour
* the hour
* @param ampm
* the string Hour.AM
or Hour.PM
* @throws IllegalArgumentException
* if hour is less than 1 or greater than 12
* @throws IllegalArgumentException
* if ampm is not one of Hour.AM
or
* Hour.PM
*/
public static void check12(int hour, String ampm) {
if (!Hour.isValid(hour, ampm)) {
throw new IllegalArgumentException("");
}
}
/**
* Checks if an hour is a valid hour on a 24-hour clock.
*
* @param hour
* the hour
*
* @throws IllegalArgumentException
* if hour is less than 0 or greater than 23
*/
public static void check24(int hour) {
if (!Hour.isValid(hour)) {
throw new IllegalArgumentException("hour must be between 0 and 23 on a 24-hour clock");
}
}
/**
* Converts an hour defined on a 12-hour clock to the equivalent hour on a
* 24-hour clock.
*
* @param hour
* hour the hour on a 12-hour clock
* @param ampm
* the string Hour.AM or Hour.PM
* @return the hour on a 24-hour clock equivalent to the given hour defined
* on a 12-hour clock
* @throws IllegalArgumentException
* if hour is less than 1 or greater than 12
* @throws IllegalArgumentException
* if ampm is not one of Hour.AM or Hour.PM
*
*/
public static int to24HourClock(int hour, String ampm) {
Hour.check12(hour, ampm);
if (hour == 12 && ampm.equals(Hour.AM)) {
return 0;
} else if (hour == 12 && ampm.equals(Hour.PM)) {
return 12;
} else if (ampm.equals(Hour.AM)) {
return hour;
}
return hour + 12;
}
/**
* Converts an hour defined on a 24-hour clock to the equivalent hour on a
* 12-hour clock.
*
* @param hour
* the hour on a 24-hour clock
* @return the hour on a 12-hour clock equivalent to the given hour defined
* on a 24-hour clock
* @throws IllegalArgumentException
* if hour is less than 0 or greater than 23
*/
public static int to12HourClock(int hour) {
Hour.check24(hour);
if (hour == 0 || hour == 12) {
return 12;
} else if (hour < 12) {
return hour;
}
return 23 - hour;
}
/**
* Initializes the hour given the hour
on a 24-hour clock.
*
* @param hour
* the hour on a 24-hour clock
* @throws IllegalArgumentException
* if hour is less than 0 or greater than 23
*/
public Hour(int hour) {
Hour.check24(hour);
this.hour = hour;
}
/**
* Initializes the hour given the hour
on a 12-hour clock and a
* string indicating AM or PM.
*
* @param hour
* the hour on a 12-hour clock
* @param ampm
* the string Hour.AM
or Hour.PM
* @throws IllegalArgumentException
* if hour is less than 1 or greater than 12
* @throws IllegalArgumentException
* if ampm is not one of Hour.AM or Hour.PM
*/
public Hour(int hour, String ampm) {
Hour.check12(hour, ampm);
this.hour = Hour.to24HourClock(hour, ampm);
}
/**
* Initializes this hour by copying the hour from a another 24-hour
* Hour
instance.
*
* @param other
* another 24-hour Hour instance
*/
public Hour(Hour other) {
this.hour = other.hour;
}
/**
* Returns the hour of this 24-hour instance.
*
* @return the hour of this 24-hour instance
*/
public int getHour() {
return this.hour;
}
/**
* Sets the hour of this 24-hour instance.
*
* @param hour
* the hour on a 24-hour clock
* @throws IllegalArgumentException
* if hour is less than 0 or greater than 23
*/
public void setHour(int hour) {
Hour.check24(hour);
this.hour = hour;
}
/**
* Sets the hour of this 24-hour instance using an hour from a 12-hour
* clock.
*
* @param hour
* the hour on a 12-hour clock
* @param ampm
* the string Hour.AM
or Hour.PM
* @throws IllegalArgumentException
* if hour is less than 1 or greater than 12
* @throws IllegalArgumentException
* if ampm is not one of Hour.AM or Hour.PM
*/
public void setHour(int hour, String ampm) {
Hour.check12(hour, ampm);
this.hour = Hour.to24HourClock(hour, ampm);
}
/**
* Compares this hour to a string containing an hour on a 12-hour clock. The
* string is assumed to start with an integer followed by a space followed
* by one or more characters; the string does not necessarily represent a
* valid hour on a 12-hour clock.
*
*
* The method returns true
if and only if all of the following
* are true:
*
*
h
is not equal to null
* h
represents a valid hour on a 12-hour clock
* h
is equivalent to the hour
* represented by this Hour
* * Some examples: * *
* Hour hour = new Hour(18); // 6PM * String h = "6 PM"; * boolean eq = hour.equals(h); // eq is equal to true * * h = "6 AM"; * eq = hour.equals(h); // eq is equal to false * * h = "100 elephants"; * eq = hour.equals(h); // eq is equal to false * * h = null; * eq = hour.equals(h); // eq is equal to false ** * @param h * an hour on a 12-hour clock represented as a string * @return true if the conditions described above are true, and false * otherwise * @pre. h starts with an integer followed by a space followed by one or more * characters */ public boolean equals(String h) { String[] parts = h.split(" "); int hour = Integer.parseInt(parts[0]); String ampm = parts[1]; boolean eq = h != null && Hour.isValid(hour, ampm); if (eq) { eq = this.hour == Hour.to24HourClock(hour, ampm); } return eq; } }