Advertisement
AndenDenAnden

Untitled

Jan 25th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.49 KB | None | 0 0
  1. /*
  2. * Class whose objects are point in time represented by hours, minutes and seconds.
  3. * The class has getters that return the hours, minutes and seconds for a given time stamp.
  4. * The class has no setters due to the implementation of the method void skip(TimeStamp time).
  5. * This method can change a timestamp if that is what one want to do.
  6. */
  7.  
  8. public class TimeStamp{
  9.  
  10. //Attributes
  11. private int hours;
  12. private int minutes;
  13. private int seconds;
  14.  
  15. public static void main(String[] args){
  16. System.out.println("This is a class that can manage time stamps.");
  17. System.out.println("The timestamps are given in hours:minutes:seconds.");
  18. System.out.println("Hours are in the interval from 0 to 23, minutes in ");
  19. System.out.println("the interval 0 to 59 and seconds in the interval from 0 to 59.");
  20. System.out.print("\n");
  21.  
  22. TimeStamp timeStamp1 = new TimeStamp(23, 59, 59);
  23. TimeStamp timeStamp2 = new TimeStamp(1, 23, 45);
  24.  
  25. System.out.println(timeStamp1.toString());
  26. timeStamp1.skipMinute();
  27. System.out.println(timeStamp1.toString());
  28. timeStamp1.skipSecond();
  29. System.out.println(timeStamp1.toString());
  30. timeStamp1.skip(timeStamp2);
  31. System.out.println(timeStamp1.toString());
  32. System.out.println(timeStamp1.equals(timeStamp2));
  33. System.out.println(timeStamp1.copy().toString());
  34. }
  35.  
  36. //Getters
  37. public int getHours(){
  38. return this.hours;
  39. }
  40.  
  41. public int getMinutes(){
  42. return this.minutes;
  43. }
  44.  
  45. public int getSeconds(){
  46. return this.seconds;
  47. }
  48.  
  49.  
  50. //Default constructor i.e. hours, minutes and seconds are set to 0.
  51. public TimeStamp(){
  52. this.hours = 0;
  53. this.minutes = 0;
  54. this.seconds = 0;
  55. }
  56.  
  57. //Constructor where you set the hours. Minutes and seconds are set to 0.
  58. public TimeStamp(int hours){
  59. if(valid(hours, 0, 0)){
  60. this.hours = hours;
  61. this.minutes = 0;
  62. this.seconds = 0;
  63. } else {
  64. System.out.println("Please enter valid amount of hours");
  65. }
  66. }
  67.  
  68. //Constructor where you set the hours and minutes. Seconds are set to 0.
  69. public TimeStamp(int hours, int minutes){
  70. if(valid(hours, minutes, 0)){
  71. this.hours = hours;
  72. this.minutes = minutes;
  73. this.seconds = 0;
  74. } else {
  75. System.out.println("Please enter valid amount of hours and/or minutes");
  76. }
  77. }
  78.  
  79. //Constructor where you set the hours, minutes and seconds.
  80. public TimeStamp(int hours, int minutes, int seconds){
  81. if(valid(hours, minutes, seconds)){
  82. this.hours = hours;
  83. this.minutes = minutes;
  84. this.seconds = seconds;
  85. } else {
  86. System.out.println("Please enter valid amount of hours, minutes and/or seconds");
  87. }
  88. }
  89.  
  90. //Method that checks whether the given time stamp is valid
  91. public boolean valid(int hours, int minutes, int seconds){
  92. boolean valid;
  93. if(0 <= hours && hours < 24 && 0 <= minutes && minutes < 60 && 0 <= seconds && seconds < 60){
  94. valid = true;
  95. } else {
  96. valid = false;
  97. }
  98. return valid;
  99. }
  100.  
  101.  
  102. //Method that adds one second to a time stamp
  103. public void skipSecond(){
  104. if(seconds < 59){
  105. seconds = seconds + 1;
  106. } else {
  107. seconds = 0;
  108. skipMinute();
  109. }
  110. }
  111.  
  112. //Method that adds one minute to a time stamp
  113. public void skipMinute(){
  114. if(minutes < 59){
  115. minutes = minutes + 1;
  116. } else {
  117. minutes = 0;
  118. skipHour();
  119. }
  120. }
  121.  
  122. //Method that adds one hour to a time stamo
  123. public void skipHour(){
  124. if(hours < 23){
  125. hours = hours + 1;
  126. } else {
  127. hours = 0;
  128. }
  129. }
  130.  
  131. //Adds the amount of time described in time to the time stamp.
  132. public void skip(TimeStamp time){
  133. for(int i = 0; i < time.getSeconds(); i++){
  134. skipSecond();
  135. }
  136. for(int i = 0; i < time.getMinutes(); i++){
  137. skipMinute();
  138. }
  139. for(int i = 0; i < time.getHours(); i++){
  140. skipHour();
  141. }
  142.  
  143. }
  144.  
  145. //Checks whether two time stamps are equal.
  146. public boolean equals(Object other){
  147. TimeStamp otherTimeStamp = (TimeStamp)other;
  148. return this.seconds == otherTimeStamp.seconds && this.minutes == otherTimeStamp.minutes && this.hours == otherTimeStamp.hours;
  149. }
  150.  
  151. //Produces a copy of a time stamp.
  152. public TimeStamp copy(){
  153. TimeStamp copy = new TimeStamp();
  154. copy.seconds = this.seconds;
  155. copy.minutes = this.minutes;
  156. copy.hours = this.hours;
  157. return copy;
  158. }
  159.  
  160.  
  161. //toString method that returns textual representation of a time stamp
  162. public String toString(){
  163. return getHours() + ":" + getMinutes() + ":" + getSeconds();
  164. }
  165.  
  166. }
  167.  
  168.  
  169.  
  170.  
  171. _______________________________________________________________________________________________________________________________
  172.  
  173.  
  174.  
  175. /*
  176. * Class with objects that represent a date as a year, month and day.
  177. * We have getters for the year, month and day. Setters are omitted since
  178. * if we want to change a date, we do so by using void skipTime().
  179. */
  180.  
  181. public class Date extends TimeStamp{
  182.  
  183. private int year;
  184. private int month;
  185. private int day;
  186.  
  187.  
  188. //Constructor that creates a date corresponding to the given year, month and day. The time is set to midnight.
  189. public Date(int year, int month, int day){
  190. super();
  191. this.year = year;
  192. this.month = month;
  193. this.day = day;
  194. }
  195.  
  196. //Constructor that creates a date corresponding to the given year, month, date and time stamp.
  197. public Date(int year, int month, int day, TimeStamp time(hours, minutes, seconds)){
  198. super(hours, minutes, seconds);
  199. this.year = year;
  200. this.month = month;
  201. this.day = day;
  202. }
  203.  
  204. //Checks whether the given date is a valid date
  205. public boolean valid(int year, int month, int day){
  206. boolean valid;
  207.  
  208. return valid;
  209. }
  210.  
  211.  
  212.  
  213.  
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement