Guest User

Untitled

a guest
Jun 22nd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. public class stopwatch {
  2. public long thetime = 0;
  3. public long stoppedtime = 0;
  4. public boolean ticking = false;
  5.  
  6.  
  7. public static void main(String[] args) {
  8. stopwatch s = new stopwatch();
  9. Scanner sc = new Scanner(System.in);
  10. boolean loop = true;
  11. while (loop = true) {
  12. System.out.println("1 start 2 is started 3 stop 4 "
  13. + "reset 5 check time 6 stop");
  14. int i = sc.nextInt();
  15. if (i == 1) {
  16. s.start();
  17. } else if (i == 2) {
  18. System.out.println(s.isStarted());
  19. } else if (i == 3) {
  20. s.stop();
  21. } else if (i == 4) {
  22. s.reset();
  23. } else if (i == 5) {
  24. System.out.println ("saved time is "+ s.time()+" Seconds");
  25. } else if (i == 6) {
  26. System.out.println("closing");
  27. loop = false;
  28. break;
  29. } else {
  30. System.out.println("invalid");
  31. }
  32.  
  33. }
  34.  
  35. }
  36. public void start() {
  37.  
  38. if (ticking == true) {
  39. thetime = thetime;
  40. } else {
  41. thetime = System.currentTimeMillis();
  42. ticking = true;
  43. }
  44. }
  45. public boolean isStarted() {
  46. return ticking;
  47. }
  48.  
  49. public void stop() {
  50. if (ticking == false) {
  51. stoppedtime = stoppedtime;
  52. } else {
  53. stoppedtime = thetime;
  54. ticking = false;
  55. }
  56. }
  57.  
  58.  
  59. public void reset() {
  60. thetime = 0;
  61. stoppedtime = 0;
  62. }
  63. public double time() {
  64. double seconds = 1000.000000;
  65. double currenttime = 0;
  66. double saved = stoppedtime;
  67. if (ticking == true) {
  68. currenttime = ((System.currentTimeMillis() - thetime)/seconds);
  69. return currenttime;
  70.  
  71.  
  72. }else {
  73. currenttime = (stoppedtime/seconds);
  74. return currenttime;
  75. }
  76.  
  77. }}enter code here
Add Comment
Please, Sign In to add comment