Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-10-2008, 11:02 AM
Member
 
Join Date: Apr 2008
Posts: 7
sidy is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-10-2008, 11:36 AM
sanjeevtarar's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Delhi(India)
Posts: 249
sanjeevtarar is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-10-2008, 11:42 AM
Member
 
Join Date: Apr 2008
Posts: 7
sidy is on a distinguished road
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();

}
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-10-2008, 12:44 PM
sanjeevtarar's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Delhi(India)
Posts: 249
sanjeevtarar is on a distinguished road
What errors ?? can you post them?.
I run your program .when i called , i got the output : 00:00:0-1
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-10-2008, 01:01 PM
Member
 
Join Date: Apr 2008
Posts: 7
sidy is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 04-10-2008, 01:08 PM
sanjeevtarar's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Delhi(India)
Posts: 249
sanjeevtarar is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 04-10-2008, 01:13 PM
sanjeevtarar's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Delhi(India)
Posts: 249
sanjeevtarar is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 04-10-2008, 02:21 PM
sanjeevtarar's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Delhi(India)
Posts: 249
sanjeevtarar is on a distinguished road
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

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

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
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 04-10-2008, 02:32 PM
Member
 
Join Date: Apr 2008
Posts: 7
sidy is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 04-10-2008, 02:42 PM
sanjeevtarar's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Delhi(India)
Posts: 249
sanjeevtarar is on a distinguished road
sidy, i did not understand what are you asking for, please explain some more.
or is there any problem in output.



sanjeev
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 04-10-2008, 02:49 PM
Member
 
Join Date: Apr 2008
Posts: 7
sidy is on a distinguished road
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" ..
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 04-10-2008, 02:58 PM
sanjeevtarar's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Delhi(India)
Posts: 249
sanjeevtarar is on a distinguished road
So what is the problem with this, still i confused?





sanjeev
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 04-10-2008, 03:03 PM
Member
 
Join Date: Apr 2008
Posts: 7
sidy is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 04-10-2008, 03:06 PM
sanjeevtarar's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Delhi(India)
Posts: 249
sanjeevtarar is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 04-10-2008, 03:09 PM
Member
 
Join Date: Apr 2008
Posts: 7
sidy is on a distinguished road
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 ..
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 04-10-2008, 08:47 PM
Member
 
Join Date: Feb 2008
Posts: 27
new_2_java is on a distinguished road
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.

Code:
public void countdown () { // ... codes System.out.println("\nTime is up!"); // --- other codes } // ... other codes public static void main (String[] arg) { // ... codes System.out.print("Time Is : " + timer.getTime()); System.out.print("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\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.
Bookmark Post in Technorati
Reply With Quote
  #17 (permalink)  
Old 04-11-2008, 07:41 AM
sanjeevtarar's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Delhi(India)
Posts: 249
sanjeevtarar is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #18 (permalink)  
Old 04-11-2008, 04:37 PM
Member
 
Join Date: Feb 2008
Posts: 27
new_2_java is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #19 (permalink)  
Old 04-11-2008, 04:41 PM
sanjeevtarar's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Delhi(India)
Posts: 249
sanjeevtarar is on a distinguished road
i tried it on Console....
Bookmark Post in Technorati
Reply With Quote
  #20 (permalink)  
Old 04-11-2008, 04:53 PM
Member
 
Join Date: Feb 2008
Posts: 27
new_2_java is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to cancel an individual timer in spite of canceling whole timer Java Tip java.util 0 04-04-2008 03:46 PM
CountDown timer Seema Sharma AWT / Swing 1 03-06-2008 05:26 PM
Simple timer im-not-alive New To Java 1 01-20-2008 10:04 PM
Please Help - Java Date/Countdown Query desktop_doodle New To Java 2 01-08-2008 05:53 PM
problem with timer Marcus Advanced Java 2 07-01-2007 06:13 AM


All times are GMT +3. The time now is 08:21 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org