943,460 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 5086
  • Java RSS
Aug 13th, 2004
0

last section of code has to a static class

Expand Post »
I have a stopwatch program and the last of the code has to be a static class
can someone help running out of time.

I've tried all i know examples aren't helping.
Here is the code I've written.
I hope someone can help. TY...


// import java.io package and utility packages

import java.io.*;

import java.util.*;


class Stopwatch{


public void start() {
GregorianCalendar now = new GregorianCalendar();
startTime = now.getTimeInMillis();
}

public void showElapsedTime() {
GregorianCalendar now = new GregorianCalendar();
long currTime = now.getTimeInMillis();
long difference = (currTime - startTime)/1000;
System.out.print(difference);
System.out.println(" seconds have elapsed.");
}

//declared outside of all methods

private long startTime;
}

class Irritating_stopwatch {


public static void main(String arg[]) throws Exception {
BufferedReader keyboard = new BufferedReader(
new InputStreamReader(System.in));



Stopwatch timer = new Stopwatch();
Counter countDown = new Counter();



for (int i=9; i>=0; i--){

System.out.println("Hit the Enter key to Start timer");
keyboard.readLine();
timer.start();

System.out.println("Hit the Enter key to Stop timer");
keyboard.readLine();
timer.showElapsedTime();



countDown.decrease();
System.out.print(countDown.getValue());
System.out.println(" More Tries");
System.out.println();

System.out.println("Hit the Enter key to initialize timer");
keyboard.readLine();
}




System.out.println("Due to planned obsolescence, this Stopwatch is no longer operational");
System.out.println();

}
}

class Counter {

public void decrease() {
callCount --;
}public int getValue() {
return callCount;
}
private int callCount = 10;
}
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Bill T is offline Offline
14 posts
since Jul 2004
Aug 14th, 2004
0

Re: last section of code has to a static class

Ok dude, first you need to use the [*code] and [/code] tags...
Java Syntax (Toggle Plain Text)
  1. // import java.io package and utility packages
  2. import java.io.*;
  3. import java.util.*;
  4.  
  5. class Stopwatch{
  6.  
  7. //declared outside of all methods
  8. private long startTime;
  9. public void start() {
  10. GregorianCalendar now = new GregorianCalendar();
  11. startTime = now.getTimeInMillis();
  12. }
  13.  
  14. public void showElapsedTime() {
  15. GregorianCalendar now = new GregorianCalendar();
  16. long currTime = now.getTimeInMillis();
  17. long difference = (currTime - startTime)/1000;
  18. System.out.print(difference);
  19. System.out.println(" seconds have elapsed.");
  20. }
  21. }
  22.  
  23. class Irritating_stopwatch {
  24. public static void main(String arg[]) throws Exception {
  25. BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
  26. Stopwatch timer = new Stopwatch();
  27. for (int i=9; i>=0; i--){
  28. System.out.println("Hit the Enter key to Start timer");
  29. keyboard.readLine();
  30. timer.start();
  31. System.out.println("Hit the Enter key to Stop timer");
  32. keyboard.readLine();
  33. timer.showElapsedTime();
  34. Counter.decrease();
  35. System.out.print(Counter.getValue());
  36. System.out.println(" More Tries");
  37. System.out.println();
  38. System.out.println("Hit the Enter key to initialize timer");
  39. keyboard.readLine();
  40. }
  41. System.out.println("Due to planned obsolescence, this Stopwatch is no longer perational");
  42. System.out.println();
  43. }
  44. }
  45.  
  46. class Counter {
  47. private static int callCount = 10;
  48.  
  49. public static void decrease() {
  50. callCount --;
  51. }
  52.  
  53. public static int getValue() {
  54. return callCount;
  55. }
  56. }
Basically, just instead of creating an instance of Counter, you just call the methods direct... Counter.decrease(); and so on and so forth.
Reputation Points: 46
Solved Threads: 2
Junior Poster
Iron_Cross is offline Offline
117 posts
since Jul 2003
Aug 15th, 2004
0

Re: last section of code has to a static class

TY very much it works great.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Bill T is offline Offline
14 posts
since Jul 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: changing colours
Next Thread in Java Forum Timeline: I can't implement a word count into my text editor (JAVA)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC