Problem of the Day
Friday, November 13, 2026
Problem:
The Clock class tracks the current time, which can be displayed in either 24-hour mode (what some call "military time"), or 12-hour mode (where morning is "AM" and afternoon/evening is "PM"). A summary of some of the values in each mode is given here.
| 24-Hour time | 12-Hour time |
0:00 | 12:00 AM |
7:00 | 7:00 AM |
12:00 | 12:00 PM |
14:00 | 2:00 PM |
A partial listing of the Clock class is given here.
public class Clock
{
private int currentHour; // an int from 0-23
private boolean mode12;
/* Constructor and some methods not shown */
/**
* Determines a version of the currentHour that is suitable
* for display, depending on the mode that has been set
* @return an hour 0-23 (if in 24-hour mode) or an AM-PM hour
*/
public int getDisplayHour()
{
/* code not shown */
}
}
Which of the following getDisplayHour methods will return the correct displayHour?
public int getDisplayHour()
{
if (mode12)
return currentHour % 12;
else
return currentHour;
}public int getDisplayHour()
{
if (mode12)
if (currentHour == 0)
return 12;
else
return currentHour % 12;
else
return currentHour;
}public int getDisplayHour()
{
if (mode12)
if (currentHour % 12 == 0)
return 12;
else
return currentHour % 12;
else
return currentHour;
}public int getDisplayHour()
{
if (mode12)
if (currentHour == 12)
return 0;
else
return currentHour % 12;
else
return currentHour;
}
The correct answer is c. Getting the hour in 12-hour mode would be a simple currentHour % 12 solution except for the fact that midnight and noon are special case in the 12-hour system. Thus, there's an additional condition that needs to be checked.