Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. 78
  2.  
  3. public class BoundedCounter {
  4.  
  5. private int value;
  6. private int upperLimit;
  7.  
  8. public BoundedCounter(int upperLimit) {
  9. // write code here
  10. this.value=0;
  11. this.upperLimit=upperLimit;
  12. }
  13.  
  14. public void next() {
  15. // write code here
  16. this.value++;
  17. if(this.value>this.upperLimit)
  18. this.value=0;
  19. }
  20.  
  21. public String toString() {
  22. // write code here
  23. if (this.value<10)
  24. return "0"+this.value;
  25. return ""+this.value;
  26. }
  27.  
  28. public int getValue() {
  29. // write here code that returns the value
  30. return this.value;
  31. }
  32.  
  33. public void setValue(int value){
  34. if (value>0 && value<=this.upperLimit)
  35. this.value=value;
  36. }
  37. }
  38. ////////////////////////////////////////////////////////////////////
  39.  
  40. import java.util.Scanner;
  41.  
  42. public class Main {
  43.  
  44. public static void main(String[] args) {
  45. // write here code to ensure that BoundedCounter works as expected
  46. // before starting 78.3 remove the extra code and use the skeleton shown
  47. // in the assignment
  48. Scanner reader = new Scanner(System.in);
  49. BoundedCounter seconds = new BoundedCounter(59);
  50. BoundedCounter minutes = new BoundedCounter(59);
  51. BoundedCounter hours = new BoundedCounter(23);
  52.  
  53. System.out.print("seconds: ");
  54. int s =reader.nextInt(); // read the initial value of seconds from the user
  55. System.out.print("minutes: ");
  56. int m =reader.nextInt(); // read the initial value of minutes from the user
  57. System.out.print("hours: ");
  58. int h =reader.nextInt(); // read the initial value of hours from the user
  59.  
  60. seconds.setValue(s);
  61. minutes.setValue(m);
  62. hours.setValue(h);
  63.  
  64.  
  65.  
  66.  
  67. int i = 0;
  68. while ( i < 121 ) {
  69. // like in previous but seconds taken into account
  70. System.out.println(hours.toString()+":"+minutes.toString()+":"+seconds.toString());
  71. seconds.next();
  72. if (seconds.getValue()==0)
  73. minutes.next();
  74. if(minutes.getValue()==0 && seconds.getValue()==0)
  75. hours.next();
  76. i++;
  77.  
  78. }
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement