Question: Clients wishing to have the EggTimer notify them whenever a second had passed must implement the TickListener interface. By doing so, the EggTimer can be
Clients wishing to have the EggTimer notify them whenever a second had passed must implement the TickListener interface. By doing so, the EggTimer can be assured that the client has a tick() operation that it can call.
Create a Countdown.java class that implements the TickListener interface. This class should have a main method that instantiates a Countdown object. The Countdown object will create an EggTimer and register with it as a TickListener using the addTickListener() method from the EggTimer. Each time the EggTimer ticks, the Countdown object should write the time remaining in the countdown on the screen until time is up.
All output should appear on a single line, with previous data erased by printing backspaces (the character \b in Java). The program should beep when time is up. If the parameter is missing, not an integer, or less than 1, the program should halt with no output.
*************************************************************************
EggTimer--Counts down in seconds to 0. The EggTimer is given the time
period it must count when created, and it immediately starts counting.
Once it gets to 0 the EggTimer is defunct and a new one must be created
to count once more. The EggTimer can be stopped at any time, but it is
then defunct and cannot be restarted.
Clients can interrogate the EggTimer to find out how many seconds are left at any time. They can also get a String version of the time left suitable for display
****************************************************************************************************************************************************
import java.util.HashSet; import java.util.Iterator; import java.util.Set; import java.util.Timer; import java.util.TimerTask; public class EggTimer extends TimerTask { /* public static attributes *****************************/ /* private static attributes ******************************/ /* attributes ***************/ private Timer timer; // provides one pulse every second private int secondsLeft; // for counting down from whatever private Set listeners; // tick listeners notified every tick /* constructors *****************/ /** * Creates a one-time EggTimer that starts right away and counts * down from the parameter, in seconds. * * @param secondsToCount The value from which it counts down. */ public EggTimer( int secondsToCount ) { secondsLeft = secondsToCount; timer = (0 < secondsLeft) ? new Timer() : null; if ( timer != null ) timer.scheduleAtFixedRate(this,1000,1000); listeners = new HashSet(); } // EggTimer /* public methods *******************/ /** * Respond to an alert from the java.util.Timer object. This is * a method required in extending the java.TimerUser class. * * The seconds left counter is decremented. If the EggTimer runs out * the Timer is stopped and forgotten. */ public void run() { secondsLeft--; if ( secondsLeft <= 0 ) stop(); notifyListeners(); } // run /** * Add a tick listener to the set of tick listeners. * * @param l The tick listener added to the set */ public void addTickListener( TickListener l ) { listeners.add( l ); } /** * Remove a tick listener from the set of tick listeners. * * @param l The tick listener removed from the set */ public void removeTickListener( TickListener l ) { listeners.remove( l ); } /** * Fetch the seconds left. /** * Fetch the seconds left. * @return How many seconds are left on this EggTimer. */ public int getSecondsLeft() { return secondsLeft; } /** * Fetch the time left as a String in the format m:ss. * @return The time left as a String. */ public String getTimeLeft() { int minutesPart = secondsLeft / 60; // minutes portion of String int secondsPart = secondsLeft % 60; // seconds portion of String return String.valueOf( minutesPart ) + ((secondsPart<10) ? ":0" : ":") + String.valueOf( secondsPart ); } // getTimeLeft /** * Stop this EggTimer. It may not be restarted. */ public void stop() { if ( timer != null ) { timer.cancel(); timer = null; } } // stop /* private methods ********************/ /** * Iterate through the list of tick listeners and notify each one * that the timer has ticked. */ private void notifyListeners() { Iterator i = listeners.iterator(); while ( i.hasNext() ) { TickListener l = i.next(); l.tick( this ); } } // notifyListeners } // EggTimer ****************************************************************************************************************************************************
interface TickListener { /* public methods *******************/ /** * Listen for EggTimer ticks occurring every second. * * @param timer The EggTimer producing the tick. */ void tick( EggTimer timer ); } // TickListener Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
