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

04-10-2008, 11:02 AM
|
|
Member
|
|
Join Date: Apr 2008
Posts: 7
|
|
|
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.
|
|

04-10-2008, 11:36 AM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Location: Delhi(India)
Posts: 249
|
|
|
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, 11:42 AM
|
|
Member
|
|
Join Date: Apr 2008
Posts: 7
|
|
|
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, 12:44 PM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Location: Delhi(India)
Posts: 249
|
|
|
What errors ?? can you post them?.
I run your program .when i called , i got the output : 00:00:0-1
|
|

04-10-2008, 01:01 PM
|
|
Member
|
|
Join Date: Apr 2008
Posts: 7
|
|
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, 01:08 PM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Location: Delhi(India)
Posts: 249
|
|
|
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, 01:13 PM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Location: Delhi(India)
Posts: 249
|
|
|
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, 02:21 PM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Location: Delhi(India)
Posts: 249
|
|
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
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
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, 02:32 PM
|
|
Member
|
|
Join Date: Apr 2008
Posts: 7
|
|
|
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, 02:42 PM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Location: Delhi(India)
Posts: 249
|
|
|
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, 02:49 PM
|
|
Member
|
|
Join Date: Apr 2008
Posts: 7
|
|
|
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, 02:58 PM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Location: Delhi(India)
Posts: 249
|
|
|
So what is the problem with this, still i confused?
sanjeev
|
|

04-10-2008, 03:03 PM
|
|
Member
|
|
Join Date: Apr 2008
Posts: 7
|
|
|
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, 03:06 PM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Location: Delhi(India)
Posts: 249
|
|
|
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, 03:09 PM
|
|
Member
|
|
Join Date: Apr 2008
Posts: 7
|
|
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, 08:47 PM
|
|
Member
|
|
Join Date: Feb 2008
Posts: 27
|
|
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.
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.
|
|

04-11-2008, 07:41 AM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Location: Delhi(India)
Posts: 249
|
|
|
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, 04:37 PM
|
|
Member
|
|
Join Date: Feb 2008
Posts: 27
|
|
|
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, 04:41 PM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Location: Delhi(India)
Posts: 249
|
|
|
i tried it on Console....
|
|

04-11-2008, 04:53 PM
|
|
Member
|
|
Join Date: Feb 2008
Posts: 27
|
|
|
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
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|