Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Clients wishing to have the EggTimer notify them whenever a second had passed mu

ID: 3600535 • Letter: C

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 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 ‘’ 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.

****************************************************************************************************************************************************

****************************************************************************************************************************************************

Explanation / Answer

Hello, I guess you wanted to use the EggTimer counter inside a new class CountDown which implements TickListener. So I created a class CountDown with a main function. Inside main function, I created an object of CountDown and registered an EggTimer object with 10 seconds count down time. A tick listener has been setup with the timer, so that it’ll be invoked in every seconds. Inside the tick() callback method, a statement to print the time remaining to the output screen (terminal window). Now here comes the unfortunate part!. In the question, you said about using ‘’ as the escape character for backspace; so that the output will be overwriting the previous countdown value. But this is totally terminal dependent. It may work on some environments, but not always. I’m using Eclipse IDE, and this ‘’ wont work with that. That still remain as a bug in Eclipse; and there’s no way of using it. There may be ways to clear out the entire terminal output; but that’s not what we wanted. Either you should run the code in some other IDEs or use a GUI for this purpose (which will be really user friendly). I’m attaching the code for the CountDown class below. The EggTimer and TickListener are not including as there is no change.

//CountDown.java file

import java.awt.Toolkit;

public class CountDown implements TickListener{

      static EggTimer timer; /*An EggTimer object*/

      public static void main(String[] args) {

            /**

            * Instantiating a new Countdown object

            */

            CountDown object=new CountDown();

            /**

            * creating a timer with 10 seconds time

            */

            timer=new EggTimer(10);

            /**

            * attaching the listener

            */

            timer.addTickListener(object);

            /**

            * running the timer

            */

            timer.run();

           

      }

      public void tick(EggTimer timer) {

            /**

            * This method will be called in every seconds by the EggTimer

            * object.

            */

            /**

            * The below code will print the seconds remaining in one line;

            * overwriting the previous value in the console

            * (if possible; if it is not supported, a square

            * character will be displayed along the text)

            */

            System.out.print(""+timer.getSecondsLeft());

            /**

            * The below code will check if the timer expired;

            * will produce a beep sound according to the OS

            */

            if(timer.getSecondsLeft()==0){

                  Toolkit.getDefaultToolkit().beep();

            }

           

      }

}

//Output

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote