I'm actually trying to create an alarm clock but i'm a bit confused on how to write this method, this is the directions and what i have so far.
/**
* Formats the time one of the following ways:
* "It is the top-of-the-hour" (if the minutes is 0)
* "It is half-past the hour" (if the minutes is 30)
* "It is 23 minutes past the hour" (if the minutes is more than 0 and less than 30; use the correct number
* of minutes here (23 is just an example)
* "It is 10 minutes before the hour" (if the minutes is more than 30 and less than 60; use the correct number
* of minutes here (10 is just an example)
*
* @precondition clock != null
* @postcondition none
*
* @param clock the clock to format
* @return a string as described above
*/
public String formatMinutesInConversationalTone(AlarmClock clock) {
if (clock == null) {
throw new IllegalArgumentException("clock cannot be null");
}
if (clock.getMinutes() == 0) {
return "It is the top-of-the-hour";
}
if(clock.getMinutes() == 30) {
return "it is half past the hour";
}
if(clock.getMinutes() > 0 && clock.getMinutes() < 30) {
}
}
I'd use different conditions for minutes that are before and after the hour, and use String.format
to create those strings:
public String formatMinutesInConversationalTone(AlarmClock clock) {
if (clock == null) {
throw new IllegalArgumentException("clock cannot be null");
}
int minutes = clock.getMinutes();
if (minutes == 0) {
return "It is the top-of-the-hour";
}
if (minutes == 30) {
return "It is half past the hour";
}
if (minutes < 30) {
return String.format("It is %d minutes past the hour", minutes);
}
return String.format("It is %d minutes before the hour", (60 - minutes));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.