Advertisement
StefanTobler

TERM 2 Assignment 1

Jan 6th, 2017
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.56 KB | None | 0 0
  1. /*
  2.  * AP CS MOOC
  3.  * Term 2 - Assignment 1: Time
  4.  * A class which represents the time of day in hours and seconds.
  5.  */
  6.  
  7. public class Time
  8. {
  9.      private int hour;
  10.      private int minute;    
  11.      
  12.      /* Complete required constructors and methods here */
  13.      
  14.      /*
  15.       *Default constructor that sets time to 0000.  
  16.       */
  17.      public Time()
  18.      {
  19.        hour = 0;
  20.        minute = 0;
  21.          
  22.      }
  23.      
  24.      /*
  25.       * If h is between 1 and 23 inclusive, set the hour to h.
  26.       * Otherwise, set the hour to 0. If m is between 0 and 59 inclusive,
  27.       * set the minutes to m. Otherwise, set the minutes to 0.
  28.       */
  29.      public Time(int h, int m)
  30.      {
  31.        if (h >= 1 && h <= 23)
  32.          hour = h;
  33.        else
  34.          hour = 0;
  35.        if (m >= 0 && m <= 59)
  36.          minute = m;
  37.        else
  38.          m = 0;
  39.      }
  40.      
  41.      /* Returns the time as a String of length 4 in the format: 0819.
  42.       * Notice that if the hour or minute is one digit, it should
  43.       * print a zero first. For example, 6 should print as 06.
  44.      */
  45.      public String toString()
  46.      {
  47.        if (hour/10 == 0)
  48.          System.out.print("0" + hour);
  49.        else
  50.          System.out.print(hour);
  51.        if (minute/10 == 0)
  52.          System.out.print("0" + minute);
  53.        else
  54.          System.out.print(minute);
  55.        return "";
  56.      }
  57.      
  58.      /*
  59.       * Returns the time as a String converted from military time
  60.       * to standard time. For example, 0545 becomes 5:45 AM and
  61.       * 1306 becomes 1:06 PM.
  62.       */
  63.      public String convert()
  64.      {
  65.        int flag = 0;
  66.        int f2 = 0;
  67.        int h = hour;
  68.        if (hour > 12)
  69.        {
  70.          h -= 12;
  71.          flag = 1;
  72.        }
  73.        else if (hour == 0)
  74.          h = 12;
  75.        if (minute/10 == 0)
  76.          f2 = 1;
  77.        if (f2 == 0)
  78.        System.out.print(h + ":" + minute + " ");
  79.        else
  80.          System.out.print(h + ":0" + minute + " ");
  81.        if (flag == 1)
  82.          System.out.print("PM");
  83.        else
  84.          System.out.print("AM");
  85.        
  86.        return "";
  87.      }
  88.      
  89.     /*
  90.      * Advances the time by one minute.
  91.      * Remember that 60 minutes = 1 hour.
  92.      * Therefore, if your time was 0359, and you add one minute,
  93.      * it becomes 0400. 2359 should increment to 0000.
  94.      */
  95.     public void increment()
  96.     {
  97.       if (minute < 59)
  98.         minute++;
  99.       else
  100.       {
  101.         if (hour < 23)
  102.           hour++;
  103.         else
  104.           hour = 0;
  105.         minute = 0;
  106.       }
  107.      
  108.     }
  109.      
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement