Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * AP CS MOOC
- * Term 2 - Assignment 1: Time
- * A class which represents the time of day in hours and seconds.
- */
- public class Time
- {
- private int hour;
- private int minute;
- public Time()
- {
- hour = 00;
- minute = 00;
- }
- public Time(int h, int m)
- {
- hour = h;
- minute = m;
- if (h <= 23 && h >= 1)
- {
- hour = h;
- }
- else
- hour = 0;
- if (m <= 59 && m >= 0)
- {
- minute = m;
- }
- else
- minute = 0;
- }
- /* Returns the time as a String of length 4 in the format: 0819.
- * Notice that if the hour or minute is one digit, it should
- * print a zero first. For example, 6 should print as 06.
- */
- public String toString()
- {
- String min = "";
- String ho = "";
- if (minute >=0 && minute <= 9)
- {
- min = "0" + minute;
- }
- else
- min = "" + minute;
- if (hour >=0 && hour <= 9)
- {
- ho = "0" + hour;
- }
- else
- ho = "" + hour;
- String newTime = ho + min;
- return newTime;
- }
- /*
- * Returns the time as a String converted from military time
- * to standard time. For example, 0545 becomes 5:45 AM and
- * 1306 becomes 1:06 PM.
- */
- public String convert()
- {
- String tod = "";
- String min = minute + "";
- String ho = hour + "";
- String standard = "";
- if (hour > 12 && hour <=23)
- {
- tod = " PM";
- }
- else
- tod = " AM";
- if ( tod == " PM")
- {
- ho = hour - 12 + "";
- }
- if (hour == 12)
- {
- tod = " PM";
- }
- if (hour == 0)
- {
- ho = "12";
- }
- if (minute < 10)
- min = "0" + minute;
- standard = ho + ":" + min + tod;
- return standard;
- }
- /*
- * Advances the time by one minute.
- * Remember that 60 minutes = 1 hour.
- * Therefore, if your time was 0359, and you add one minute,
- * it becomes 0400. 2359 should increment to 0000.
- */
- public void increment()
- {
- minute ++;
- if (minute == 60)
- {
- hour ++;
- minute = 0;
- }
- if (hour == 24)
- {
- hour = 0;
- minute = 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement