Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    sidy is offline Member
    Join Date
    Apr 2008
    Posts
    7
    Rep Power
    0

    Default countdown timer, little help with method

    Hello, this is my first post so please be gentle and excuse my ignorance.

    I am doing a timer application, very simple stuff I guess. But I am new learner, and I have a problem with the last method in the program.

    what it does :

    The timer must be able to keep track of time down to the nearest second. This timer does a decrement one second at a time until the timer value reaches 00:00:00. Once the timer reaches 00:00:00, it will stop counting down and print a message to the standard output

    what I want it to do :

    Once the initial timer value is set through the constructor or the setTimer() method. the method start() which will start the count down. This method will repeatedly call timeTick() to decrement the timer value by 1 second and then sleeps for 1 second. After each timer value decrement, the timer value needs to be printed to the standard output.

    what I can't figure out

    How can I tell the start() method to repeatdly call timeTick() every 1 second and print the results to the standard output and finally stop once it reaches 00:00:00 and print out "time is up" .. I've set up a decrement to go down every one second in the start method, but thats all I was able to do .. I tried to implement an if statement but I kept getting errors " if(getValue.seconds = 0, getValue.minutes = 0, getValue = 0) { System.print.out("time is up!"); } "

    Any help would be SUPER, thanks

    the timer class can be found here : Nopaste - timer class
    the number display class can be found here : Nopaste - numberdisplay
    Last edited by sidy; 04-10-2008 at 11:05 AM.

  2. #2
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Hi Sidy,

    You can use Thread to do this. use Thread.sleep(1000) // thread will sleep for one second.

    if you need more help please let me know i will post changes in your code.


    sanjeev

  3. #3
    sidy is offline Member
    Join Date
    Apr 2008
    Posts
    7
    Rep Power
    0

    Default

    hey sanjeev, thanks for the reply .. I am having trouble putting things in their place, if you look at the code below, of course it gives me an error for the first if statement, do i even need the thread.sleep? isnt the decrement doing the job? or do I need it to view the timer in the standard output .. thanks

    /**
    * Start countdown
    */
    public void start()
    {
    if(seconds == 0 && minutes == 0 && hours == 0) {
    System.print.out("time is up!"); }

    else {

    seconds.decrement();
    if(seconds.getValue() == 0) {
    minutes.decrement();
    if(minutes.getValue() == 0)
    hours.decrement();

    }

    updateDisplay();

    }

  4. #4
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    What errors ?? can you post them?.
    I run your program .when i called , i got the output : 00:00:0-1

  5. #5
    sidy is offline Member
    Join Date
    Apr 2008
    Posts
    7
    Rep Power
    0

    Default

    thats because i added the the if statement shown above, it gives me a "incomparable types: NumberDisplay and int"

    here is the updated code ..

    Nopaste - updated timer

    can you please tell me how i should implement the thread command into the start() method?

  6. #6
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Sidy, what you are doing that : checking NumberDisplay class's Object with int value so it is giving you errors.

    one solution is that just make methods in DisplayNumber class that will return an int value of hour,minutes and seconds.



    sanjeev

  7. #7
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    you can use this also :

    please check with this :

    if(hours.getDisplayValue().equals("0") && minutes.getDisplayValue().equals("0") && seconds.getDisplayValue().equals("0")) {
    System.out.println("time is up!");
    }

    let me know if errors are coming.

  8. #8
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Sidy, Please check with these classes, and let me knoe if any error.

    First compile both files and run through console and see the output.I have created a main method to run the program in Timer.java, please modify as per your requirement.


    Timer.java

    Java Code:
    public class Timer extends Thread
    {
        private NumberDisplay hours;
        private NumberDisplay minutes;
        private NumberDisplay seconds;
    	public static final int ONE_SECONDS = 1000;
    	public  static boolean finished = true;
        private String displayString;    // simulates the actual display
     
        /**
         * Constructor for ClockDisplay objects. This constructor 
         * creates a new clock set at 00:00.
         */
        public Timer()
        {
    		
            hours = new NumberDisplay(48);
            minutes = new NumberDisplay(60);
            seconds = new NumberDisplay(60);
    
            updateDisplay();
        }
     
        /**
         * Constructor for ClockDisplay objects. This constructor
         * creates a new clock set at the time specified by the 
         * parameters.
         */
        public Timer(int hour, int minute, int second)
        {
            hours = new NumberDisplay(48);
            minutes = new NumberDisplay(60);
            seconds = new NumberDisplay(60);
            setTimer(hour, minute, second);
        }
     
        /**
         * This method should get called once every minute - it makes
         * the clock display go one minute forward.
         */
    
    	
        public void timeTick()
        {
                seconds.increment();
            if(seconds.getValue() == 0) {
                minutes.increment();
    
    		    if(minutes.getValue() == 0)  // it just rolled over!
    			    hours.increment();
            }
            updateDisplay();
        }
     
    	public void run(){}
        /**
         * Start countdown
         */
        public void countdown()
        {
    		if(hours.getDisplayValue().equals("00") && minutes.getDisplayValue().equals("00") && seconds.getDisplayValue().equals("00")) {
    			finished = false;
                System.out.println("time is up!"); 
    		}else {
     
                seconds.decrement();
            if(seconds.getValue() == 0) {
                minutes.decrement();
            if(minutes.getValue() == 0)
                hours.decrement();
     
            }
    		}
            updateDisplay();
        }
        /**
         * Set the time of the display to the specified hour and
         * minute.
         */
        public void setTimer(int hour, int minute, int second)
        {
            hours.setValue(hour);
            minutes.setValue(minute);
            seconds.setValue(second);
            updateDisplay();
        }
     
        /**
         * Return the current time of this display in the format HH:MM.
         */
        public String getTime()
        {
            return displayString;
        }
     
        /**
         * Update the internal string that represents the display.
         */
        private void updateDisplay()
        {
            displayString = hours.getDisplayValue() + ":" + 
                            minutes.getDisplayValue() + ":" +
                            seconds.getDisplayValue();
        }
    
    	public static void main(String args[]){
    
    		Timer timer = new Timer(00,00,10);
    		timer.start();
    		while(finished){
    			System.out.println("Time Is : " + timer.getTime());
    			timer.countdown();
    			try {
    				Thread.sleep(ONE_SECONDS);
    			}
    			catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    }
    NumberDisplay.java

    Java Code:
    public class NumberDisplay
    {
        private int limit;
        private int value;
     
        /**
         * Constructor for objects of class NumberDisplay.
         * Set the limit at which the display rolls over.
         */
        public NumberDisplay(int rollOverLimit)
        {
            limit = rollOverLimit;
            value = 0;
        }
     
        /**
         * Return the current value.
         */
        public int getValue()
        {
            return value;
        }
     
        /**
         * Return the display value (that is, the current value as a two-digit
         * String. If the value is less than ten, it will be padded with a leading
         * zero).
         */
        public String getDisplayValue()
        {
            if(value < 10) {
                return "0" + value;
            }
            else {
                return "" + value;
            }
    
        }
     
        /**
         * Set the value of the display to the new specified value. If the new
         * value is less than zero or over the limit, do nothing.
         */
        public void setValue(int replacementValue)
        {
            if((replacementValue >= 0) && (replacementValue < limit)) {
                value = replacementValue;
            }
        }
     
        /**
         * Increment the display value by one, rolling over to zero if the
         * limit is reached.
         */
        public void increment()
        {
            value = (value + 1) % limit;
        }
     
        /**
         * decrement the display value by one, rolling over to zero if the
         * limit is reached.
         */
        public void decrement()
        {
    		if(value>0)
    	        value = (value - 1) % limit;
    	
        }
    }



    sanjeev

  9. #9
    sidy is offline Member
    Join Date
    Apr 2008
    Posts
    7
    Rep Power
    0

    Default

    thanks sanjeev!! this is great .. um quick question though, how can i get it to actually show the timer running? .. the countdown() wont show the clock .. i know u created a view for it, but um i cant see make it show? thanks again

  10. #10
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    sidy, i did not understand what are you asking for, please explain some more.
    or is there any problem in output.



    sanjeev

  11. #11
    sidy is offline Member
    Join Date
    Apr 2008
    Posts
    7
    Rep Power
    0

    Default

    no no .. no problems with the output however the whole purpose of the program is so that when the user has the timer using setTimer constructor and then call the countdown() method .. it will actually show the clock counting down so for example if i inputed 11:13:43 in the setTimer and then ran countdown .. it will show the timer counting down

    11:13:43
    11:13:42
    11:13:41

    and so on until it reaches 00:00:00 and finall show the message "time is up" ..

  12. #12
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    So what is the problem with this, still i confused?





    sanjeev

  13. #13
    sidy is offline Member
    Join Date
    Apr 2008
    Posts
    7
    Rep Power
    0

    Default

    well, when i setTime and click on countdown .. nothing happens .. it just deducts one second and does nothing else it doesnt even show the timer .. am i doing something wrong?

  14. #14
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    sidy, If you call from console it gives right output...am i right?

    how you are using these classes? Calling from other program?

    did you start the Thread?

    can you send me complete program


    sanjeev

  15. #15
    sidy is offline Member
    Join Date
    Apr 2008
    Posts
    7
    Rep Power
    0

    Default

    I am using BlueJ to compile everything ( BlueJ - Teaching Java - Learning Java ) its a free 2mb program

    the complete program is the code that you just posted ..

  16. #16
    new_2_java is offline Member
    Join Date
    Feb 2008
    Posts
    60
    Rep Power
    0

    Default

    Sanjeev solution works.

    Just to tweak alittle bit the presentation, add the following add the follwoing SOPs. This will print the result as if it was the clock.

    Java Code:
    public void countdown () {
        // ... codes
        System.out.println("[B][COLOR="Red"]\n[/COLOR][/B]Time is up!");
        // --- other codes
    }
    
    // ... other codes
    public static void main (String[] arg) {
        // ... codes
        System.out.print("Time Is : " + timer.getTime());
        [B][COLOR="Red"]System.out.print("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");[/COLOR][/B] // adding this line, will erase the previouse printout and re-print the new time on top of it, so you get the illusion of time is ticking.
    }
    Last edited by new_2_java; 04-10-2008 at 08:50 PM.

  17. #17
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Hi,

    System.out.print("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b \b\b");

    Nothing different is happening after adding this line..


    sanjeev

  18. #18
    new_2_java is offline Member
    Join Date
    Feb 2008
    Posts
    60
    Rep Power
    0

    Default

    Are you running it from console, or an IDE e.g. Eclipse?

    If you run it from an IDE, then I don't think you would notice a difference, but if you run it from console, then you will notice that the time will display as a digital clock.

    Basically, what this \b does, is it ereases the previouse entries and re-print the new values in the same position.

  19. #19
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    i tried it on Console....

  20. #20
    new_2_java is offline Member
    Join Date
    Feb 2008
    Posts
    60
    Rep Power
    0

    Default

    it is print(...) and not println(...) and the out put should be

    11:13:43 // only this one line, after each seconds the time would go down 1 second.

    as apposed to:

    11:13:43
    11:13:42
    11:13:41
    ...

    printing one line each time

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 0
    Last Post: 04-04-2008, 03:46 PM
  2. CountDown timer
    By Seema Sharma in forum AWT / Swing
    Replies: 1
    Last Post: 03-06-2008, 05:26 PM
  3. Simple timer
    By im-not-alive in forum New To Java
    Replies: 1
    Last Post: 01-20-2008, 10:04 PM
  4. Please Help - Java Date/Countdown Query
    By desktop_doodle in forum New To Java
    Replies: 2
    Last Post: 01-08-2008, 05:53 PM
  5. problem with timer
    By Marcus in forum Advanced Java
    Replies: 2
    Last Post: 07-01-2007, 06:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •