import java.text.DecimalFormat; /* TimeOfDay12Or24.java ** An instance of this class represents an immutable time of day, precise to ** a minute. It extends its parent class by supporting not only a 24-hour mode ** (as is supported by the parent class) but also an AM/PM mode (which this ** class introduces). Essentially, the new capability that this class provides ** is that its toString() method produces a string that describes the time of ** day either in 12-hour mode (e.g., "7:46AM", "8:19PM") or in 24-hour mode ** (e.g., "07:46", "20:19"), according to the current mode of the object. ** Mutator methods allow the client to set the mode as desired. ** ** Authors: R. McCloskey and < STUDENTS' NAMES > ** Flaws: ... ** */ public class TimeOfDay12Or24 extends TimeOfDay { // instance variable // ----------------- private boolean in24Mode; // true means 24-hour mode is in force; // false means 12-hour mode is in force // constructor // ----------- /* Initializes this time of day to the specified hour and minute, the hour ** value assumed to be expressed in accord with 24-hour mode. ** The object's initial mode is 24-hour. */ public TimeOfDay12Or24(int hr, int min) { super(hr, min); // call parent class's constructor setToMode24(); // sets the mode to 24-hour mode } // observers // --------- /* Reports whether or not this object is in 24-hour mode. */ public boolean in24HourMode() { return in24Mode; } /* Reports whether or not this object is in 12-hour mode. */ public boolean in12HourMode() { return !in24Mode; } /* Returns a string describing the time of day represented by this object, ** where the form of the string is consistent with the object's current ** mode. For example, when in 24-hour format, the string produced ** could be "08:15". When in 12-hour format, it would be "8:15AM". */ @Override public String toString() { DecimalFormat hourFormatter = new DecimalFormat("#0"); DecimalFormat minuteFormatter = new DecimalFormat("00"); // *** Missing code *** return ""; // STUB! } // mutators // -------- /* Sets this object's mode to 24-hour mode. */ public void setToMode24() { in24Mode = true; } /* Sets this object's mode to 12-hour mode. */ public void setToMode12() { in24Mode = false; } // private methods // --------------- /* Given a 24-hour mode hour value, returns either "AM" or "PM" according ** to whether, respectively, the indicated hour value occurs in the AM ** or the PM. */ private String hourToAMorPM(int hr) { return ""; // STUB } /* Given a 24-hour mode hour value, returns the corresponding hour value ** in 12-hour mode. For example, hour 0 translates into hour 12 (as in ** 12AM), and hour 19 translates to hour 7 (as in 7PM). */ private int hour24To12(int hr) { return 0; // STUB } }