Advertisement
akosiraff

Time2 JAVA

May 26th, 2013
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 KB | None | 0 0
  1.  
  2. Download: http://solutionzip.com/downloads/time2-java/
  3. Ex 8.7 (To enhance class Time2)
  4. Modify class Time2 of Fig. 8.5 to include a tick method that increments the time stored in a Time2 object by one second. Provide method incrementMinute to increment the minute by one and method incrementHour to increment the hour by one. The Time2 object should always remain in a consistent state. Write a program that test the trick method, the incrementMinute method and the incrementHour method to ensure that they work correctly. Be sure to test the following cases:
  5. a) incrementing into the next minute,
  6. b) incrementing into the next hour and
  7. c) incrementing into the next day (i.e., 11:59:59 PM to 12:00:00 AM).
  8. New Java programming code logic is required to be added into this exercise Ex 8.7. You may copy an existing Java program example like Fig 8.5 below to start your work:
  9. Sample Java Code can be used for your program from Fig 8.5
  10. // Fig. 8.5: Time2.java
  11. // Time2 class declaration with overloaded constructors.
  12. public class Time2
  13. {
  14. private int hour; // 0 – 23
  15. private int minute; // 0 – 59
  16. private int second; // 0 – 59
  17. // Time2 no-argument constructor: initializes each instance variable
  18. // to zero; ensures that Time2 objects start in a consistent state
  19. public Time2()
  20. {
  21. this( 0, 0, 0 ); // invoke Time2 constructor with three arguments
  22. } // end Time2 no-argument constructor
  23. // Time2 constructor: hour supplied, minute and second defaulted to 0
  24. public Time2( int h )
  25. {
  26. this( h, 0, 0 ); // invoke Time2 constructor with three arguments
  27. } // end Time2 one-argument constructor
  28. // Time2 constructor: hour and minute supplied, second defaulted to 0
  29. public Time2( int h, int m )
  30. {
  31. this( h, m, 0 ); // invoke Time2 constructor with three arguments
  32. } // end Time2 two-argument constructor
  33. // Time2 constructor: hour, minute and second supplied
  34. public Time2( int h, int m, int s )
  35. {
  36. setTime( h, m, s ); // invoke setTime to validate time
  37. } // end Time2 three-argument constructor
  38. // Time2 constructor: another Time2 object supplied
  39. public Time2( Time2 time )
  40. {
  41. // invoke Time2 three-argument constructor
  42. this( time.getHour(), time.getMinute(), time.getSecond() );
  43. } // end Time2 constructor with a Time2 object argument
  44. // Set Methods
  45. // set a new time value using universal time; ensure that
  46. // the data remains consistent by setting invalid values to zero
  47. public void setTime( int h, int m, int s )
  48. {
  49. setHour( h ); // set the hour
  50. setMinute( m ); // set the minute
  51. setSecond( s ); // set the second
  52. } // end method setTime
  53. // validate and set hour
  54. public void setHour( int h )
  55. {
  56. hour = ( ( h >= 0 && h < 24 ) ? h : 0 );
  57. } // end method setHour
  58. // validate and set minute
  59. public void setMinute( int m )
  60. {
  61. minute = ( ( m >= 0 && m < 60 ) ? m : 0 );
  62. } // end method setMinute
  63. // validate and set second
  64. public void setSecond( int s )
  65. {
  66. second = ( ( s >= 0 && s < 60 ) ? s : 0 );
  67. } // end method setSecond
  68. // Get Methods
  69. // get hour value
  70. public int getHour()
  71. {
  72. return hour;
  73. } // end method getHour
  74. // get minute value
  75. public int getMinute()
  76. {
  77. return minute;
  78. } // end method getMinute
  79. // get second value
  80. public int getSecond()
  81. {
  82. return second;
  83. } // end method getSecond
  84. // convert to String in universal-time format (HH:MM:SS)
  85. public String toUniversalString()
  86. {
  87. return String.format(
  88. “%02d:%02d:%02d”, getHour(), getMinute(), getSecond() );
  89. } // end method toUniversalString
  90. // convert to String in standard-time format (H:MM:SS AM or PM)
  91. public String toString()
  92. {
  93. return String.format( “%d:%02d:%02d %s”,
  94. ( (getHour() == 0 || getHour() == 12) ? 12 : getHour() % 12 ),
  95. getMinute(), getSecond(), ( getHour() < 12 ? “AM” : “PM” ) );
  96. } // end method toString
  97. } // end class Time2
  98. Modify Java logic in Fig 8.5 to perform:
  99. 1. Increase second and increase minute by 1 when second reaches 60 (actually it will be set to 0) in Fig 8.5
  100. public void tick()
  101. {
  102. setSecond( getSecond() + 1 );
  103. if ( getSecond() == 0 )
  104. incrementMinute();
  105. } // end method tick
  106. 2. Increase minute and increase hour by 1 when minute reaches 60 (actually it will be set to 0) in Fig 8.5. The logic is similar to (1)
  107. 3. Increase hour and set hour to 0 when hour reaches 24 (actually it will be set to 0) in Fig 8.5. The logic is similar to (1)
  108. 4. In test.java to add the logic below to test new tick method:
  109. setTime(23, 58, 55);
  110. input = 1;
  111. while (input == 1) {
  112. display time by toUniversalString();
  113. tick();
  114. display time by toUniversalString();
  115. prompt (1 to tick or 0 to end);
  116. read input;
  117. }
  118.  
  119. Download: http://solutionzip.com/downloads/time2-java/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement