/** * A countdown timer with minutes and seconds. * The timer does not go less than zero. * @author Beth Katz */ public class Timer { private static final int secInMin = 60; /** construct a timer with minutes and seconds of timer (combined value should be positive) @param minutes number of minutes on newly constructed timer @param seconds number of seconds on newly constructed timer */ public Timer(int minutes, int seconds) { set(minutes, seconds); } /** construct a timer with a positive number of minutes @param minutes number of minutes on newly constructed timer */ public Timer(int minutes) { // alternatively could call this(minutes, 0); // to call the two parameter constructor set(minutes, 0); } /** construct a timer with no initial value; timer set to zero */ public Timer( ) { set(0, 0); } /** sets minutes and seconds of timer (combined value should be positive) @param minutes number of minutes @param seconds number of seconds */ public void set(int minutes, int seconds) { theSeconds = (minutes * secInMin) + seconds; if (theSeconds < 0) { System.err.println("Time must be non-negative." + " Timer set to zero"); theSeconds = 0; } } /** sets minutes of timer (value should be positive) @param minutes number of minutes */ public void set(int minutes) { set(minutes, 0); } /** sets timer to zero */ public void set( ) { set(0, 0); } /** builds a string of the timer's value in minutes:seconds form @return a string of the timer's value in minutes:seconds form */ public String toString( ) { int seconds = theSeconds % secInMin; String fill = seconds < 10 ? "0" : ""; return (theSeconds/secInMin) + ":" + fill + seconds; } /** subtracts minutes and seconds from timer; timer should not fall below zero @param minutes number of minutes to subtract @param seconds number of seconds to subtract */ public void subtract(int minutes, int seconds) { theSeconds -= (minutes * secInMin) + seconds; if (theSeconds < 0) { theSeconds = 0; } } /** returns whether timer has run out of time @return whether timer has run out of time */ public boolean outOfTime( ) { return theSeconds == 0; } private int theSeconds; }