Clock.java

Reply

Join Date: Dec 2008
Posts: 1
Reputation: jinuyasha is an unknown quantity at this point 
Solved Threads: 0
jinuyasha jinuyasha is offline Offline
Newbie Poster

Clock.java

 
0
  #1
Dec 14th, 2008
Hello, first time poster here. So if I come off as asking something so hastily and quickly my bad, but currently I'm creating a clock class on DrJava. This is what I have so far.

  1. clock.java
  2. import java.util.*;
  3. import java.text.*;
  4. public class Clock
  5. {private int hours;
  6. private int minutes;
  7. private int seconds;
  8. int minutesleft, secondsleft;
  9. //Constructor
  10. //item a
  11. Clock()
  12. {
  13. hours=0;
  14. minutes=0;
  15. seconds=0;
  16. }
  17. //item b
  18. Clock(int h, int m, int s)
  19. {hours=h;
  20. minutes=m;
  21. seconds=s;
  22. }
  23. //Accessors
  24. //item c
  25. public void setTime(int h, int m, int s)
  26. {
  27. hours=h;
  28. minutes=m;
  29. seconds=s;
  30. }
  31. //item d
  32. public int getHours()
  33. {return hours;
  34. }
  35. //item e
  36. public int getMinutes()
  37. {return minutes;
  38. }
  39. //item f
  40. public int getSeconds()
  41. {return seconds;
  42. }
  43.  
  44. //item g
  45. public void setHours(int h)
  46. {hours=h;
  47. }
  48. //Modifiers
  49. //item h
  50. public void setMinutes(int m)
  51. {minutes=m;
  52.  
  53. }
  54. //item i
  55. public void setSeconds(int s)
  56. {seconds=s;
  57. }
  58. //item j
  59. public void printTime()
  60. { System.out.println(hours+ "H:" + minutes + "M:" + seconds + "S");
  61. }
  62. //item k
  63. public void inputTime()
  64. {Scanner input=new Scanner(System.in);
  65. System.out.println("Enter hours: ");
  66. hours=input.nextInt();
  67. System.out.println("Enter minutes: ");
  68. minutes=input.nextInt();
  69. System.out.println("Enter seconds: ");
  70. seconds=input.nextInt();
  71. }
  72. //item l
  73. public String toString()
  74. {String s= hours+ "H:" + minutes + "M:" + seconds + "S";
  75. return s;
  76. }
  77. //item m
  78. public void incrementSeconds(int seconds)
  79. {this.seconds=this.seconds+seconds;
  80. }
  81. //item n
  82. public void incrementMinutes(int minutes)
  83. {this.minutes=this.minutes+ minutes;
  84. }
  85. //item o
  86. public void incrementHours(int hours)
  87. {this.hours=this.hours+hours;
  88. }
  89. }
  90. // this is the end of class Clock, put all new methods before this
  91.  
  92.  
  93. MAIN METHOD:
  94. import java.util.*;
  95. import java.text.*;
  96. public class lab80
  97. {public static void main(String[] args)
  98. {
  99. //item a
  100. Clock t1=new Clock();
  101. //item b
  102. Clock t2=new Clock();
  103. //item c
  104. Clock t3=new Clock(10, 30, 15);
  105. //item d
  106. t1.inputTime();
  107.  
  108. //item e
  109. t2.setTime(20,12, 45);
  110. //item f
  111. System.out.println("t1 hours:"+ t1.getHours());
  112. //item g
  113. System.out.println("t2 minutes:" + t2.getMinutes());
  114. //item h
  115. System.out.println("t3 seconds:" + t3.getSeconds());
  116. //item i
  117. t1.setHours(5);
  118. //item j
  119. t2.setMinutes(13);
  120. //item k
  121. t3.setSeconds(55);
  122. //item l
  123. t1.printTime();
  124. //item m
  125. System.out.println(t2);
  126. //item n
  127. t1.incrementHours(5);
  128. //item o
  129. t1.incrementMinutes(10);
  130. //item p
  131. t1.incrementSeconds(2);
  132. //item q
  133. t1.printTime();
  134.  
  135.  
  136.  
  137.  
  138.  
  139. }//end main
  140. }//end class

I have fullfilled most of the requirements that my profressor has asked except for a few.

1-A way to increment the time, so that if seconds/minutes goes >= 60 they become minute/hour.

2-Things displayed in the main Method:
A:Create an array of objects called timeArray of type Time with 3 elements.
B: Store the Time objects t1, t2, t3 in the array timeArray
C: Display the time of all the objects in the array timeArray.


Just incase of anymore questions, I have attached the file of the requirements of the clock class. So any tips/help would be helpful. Hope it dosen't sound like alot.
Attached Files
File Type: txt Clockclassproject.txt (2.9 KB, 9 views)
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,810
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Clock.java

 
0
  #2
Dec 15th, 2008
You should probably have your printTime () function call the toString () function. It's the same code. No sense writing it twice.

Regarding going from 0 to 59 and 0 to 23 and not beyond, in your increment functions, use the / and % operators. Add the new amount to the old amount as you do, but then divide by 60 and take the mod of 60. For hours, it would be 24.

  1. public void incrementSeconds(int seconds)
  2. {
  3. this.seconds=this.seconds+seconds;
  4. int minutestoIncrement = this.seconds / 60;
  5. if (minutesToIncrement != 0)
  6. incrementMinutes (minutesToIncrement);
  7.  
  8. this.seconds = this.seconds % 60;
  9. }

Do you have to worry about negative numbers at all? Not sure what the question is regarding the array. Do you not know how to declare an array? Do you not know how to write and read from it?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC