Results 1 to 20 of 23
- 04-10-2008, 10:02 AM #1
Member
- Join Date
- Apr 2008
- Posts
- 7
- Rep Power
- 0
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 - numberdisplayLast edited by sidy; 04-10-2008 at 10:05 AM.
- 04-10-2008, 10:36 AM #2
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
- 04-10-2008, 10:42 AM #3
Member
- Join Date
- Apr 2008
- Posts
- 7
- Rep Power
- 0
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();
}
- 04-10-2008, 11:44 AM #4
What errors ?? can you post them?.
I run your program .when i called , i got the output : 00:00:0-1
- 04-10-2008, 12:01 PM #5
Member
- Join Date
- Apr 2008
- Posts
- 7
- Rep Power
- 0
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?
- 04-10-2008, 12:08 PM #6
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
- 04-10-2008, 12:13 PM #7
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.
- 04-10-2008, 01:21 PM #8
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
NumberDisplay.javaJava 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(); } } } }
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
- 04-10-2008, 01:32 PM #9
Member
- Join Date
- Apr 2008
- Posts
- 7
- Rep Power
- 0
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
- 04-10-2008, 01:42 PM #10
sidy, i did not understand what are you asking for, please explain some more.
or is there any problem in output.
sanjeev
- 04-10-2008, 01:49 PM #11
Member
- Join Date
- Apr 2008
- Posts
- 7
- Rep Power
- 0
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" ..
- 04-10-2008, 01:58 PM #12
So what is the problem with this, still i confused?
sanjeev
- 04-10-2008, 02:03 PM #13
Member
- Join Date
- Apr 2008
- Posts
- 7
- Rep Power
- 0
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?
- 04-10-2008, 02:06 PM #14
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
- 04-10-2008, 02:09 PM #15
Member
- Join Date
- Apr 2008
- Posts
- 7
- Rep Power
- 0
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 ..
- 04-10-2008, 07:47 PM #16
Member
- Join Date
- Feb 2008
- Posts
- 60
- Rep Power
- 0
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 07:50 PM.
- 04-11-2008, 06:41 AM #17
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
- 04-11-2008, 03:37 PM #18
Member
- Join Date
- Feb 2008
- Posts
- 60
- Rep Power
- 0
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.
- 04-11-2008, 03:41 PM #19
i tried it on Console....
- 04-11-2008, 03:53 PM #20
Member
- Join Date
- Feb 2008
- Posts
- 60
- Rep Power
- 0
Similar Threads
-
How to cancel an individual timer in spite of canceling whole timer
By Java Tip in forum java.utilReplies: 0Last Post: 04-04-2008, 02:46 PM -
CountDown timer
By Seema Sharma in forum AWT / SwingReplies: 1Last Post: 03-06-2008, 04:26 PM -
Simple timer
By im-not-alive in forum New To JavaReplies: 1Last Post: 01-20-2008, 09:04 PM -
Please Help - Java Date/Countdown Query
By desktop_doodle in forum New To JavaReplies: 2Last Post: 01-08-2008, 04:53 PM -
problem with timer
By Marcus in forum Advanced JavaReplies: 2Last Post: 07-01-2007, 05:13 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks