package eecs2030.test3; /** * 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
012AM
11AM
22AM
33AM
44AM
55AM
66AM
77AM
88AM
99AM
1010AM
1111AM
1212PM
131PM
142PM
153PM
164PM
175PM
186PM
197PM
208PM
219PM
2210PM
2311PM
* * @author EECS2030 Fall 2016 * */ public class Hour { /** * The string "AM" */ public static final String AM = "AM"; /** * The string "PM" */ public static final String PM = "PM"; /* * YOU SHOULD ADD WHATEVER FIELDS YOU REQUIRE BELOW HERE */ /** * Returns 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) { // HINT: Use isValid(int, String) } /** * 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) { // HINT: Use isValid(int) } /** * 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) { // HINT: Use check12(int, String) return 0; } /** * 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) { // HINT: Use check24(int) return 0; } /** * 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) { } /** * 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) { } /** * 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) { } /** * Returns the hour of this 24-hour instance. * * @return the hour of this 24-hour instance */ public int getHour() { return 0; } /** * 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) { } /** * 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) { } /** * 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: * *

* *

* 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) { // HINT: DO NOT USE THE ECLIPSE GENERATED VERSION OF EQUALS return true; } }