title: Military time class assignment java
body: <p>Time() - Default constructor that sets the time to 1200. Time(int h, int m) - If h is between 1 and 23 inclusive, set the hour to h. Otherwise, set the hour to 0. If m is between 0 and 59 inclusive, set the minutes to m. Otherwise, set the minutes to 0. Time should include the following methods:</p>
  
  <p>String toString() - 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.</p>
  
  <p>String convert() - 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.</p>
  
  <p>void increment() - 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.
  My assignment is only the time class not the main method.
  This is what I've written (with the exception of the main method):</p>
  
  <pre><code>public class Time { private int hour; private int minute;
  
  public Time() { hour = 12; minute = 0; this(12, 0); }
  
  public Time(int h, int m) { hour = 0; minute = 0; if (h &gt; 0 &amp;&amp; h &lt; 24) {  hour = h; } if (m &lt;= 59 &amp;&amp; m &gt;= 0) { minute = m; } }
  
  public String toString() {
  
  String hours = "";
  
  String minutes = "";
  
  if (hour &lt; 10) {
  
  hours += "0";
  
  }
  
  if (minute &lt; 10) {
  
  minutes = "0";
  
  }
  
  hours += hour;
  
  minutes += minute;
  
  return hours + minutes;
  
  }
  
  public String convert() {
  
  String meridian;
  
  if (hour &gt;= 12) {
  
  meridian = "pm";
  
  } else {
  
  meridian = "am";
  
  }
  
  String M1 = toString().substring(2,3);
  
  String H1;
  
  if ((hour - 12) &gt; 0) {
  
  H1 = "" + (hour - 12) + ":";
  
  } else {
  
  H1 = "" + (hour) + ":";
  
  }
  
  return (H1 + M1 + " " + meridian);
  
  }
  
  
  
  
  public void increment() {
  
  minute ++;
  
  if (minute == 60) {
  
  hour += 1;
  
  minute = 00;
  
  }
  
  if (hour == 24) {
  
  hour = 00;
  
  }
  
  
  }
  
  }
  </code></pre>
  
  <p>And here's the main method:</p>
  
  <pre><code> public static void main(String str[]) throws IOException
   {
  
    Time time1 = new Time(14, 56);
    System.out.println("time1: " + time1);
    System.out.println("convert time1 to standard time: " + time1.convert());
    System.out.println("time1: " + time1);
    System.out.print("increment time1 five times: ");
    time1.increment();
    time1.increment();
    time1.increment();
    time1.increment();
    time1.increment();
    System.out.println(time1 + "\n");
  
    Time time2 = new Time(-7, 12);
    System.out.println("time2: " + time2);
    System.out.print("increment time2 67 times: ");
    for (int i = 0; i &lt; 67; i++)
     time2.increment();
    System.out.println(time2);
    System.out.println("convert to time2 standard time: " + time2.convert());
    System.out.println("time2: " + time2 + "\n");
  
  
    Time time3 = new Time(5, 17);
    System.out.println("time3: " + time3);
    System.out.print("convert time3: ");
    System.out.println(time3.convert());
  
    Time time4 = new Time(12, 15);
    System.out.println("\ntime4: " + time4);
    System.out.println("convert time4: " + time4.convert());
  
    Time time5 = new Time(0, 15);
    System.out.println("\ntime5: " + time5);
    System.out.println("convert time5: " + time5.convert());
  
    Time time6 = new Time(24, 15);
    System.out.println("\ntime6: " + time6);
    System.out.println("convert time6: " + time6.convert());
  
    Time time7 = new Time(23,59);
    System.out.println("\ntime7: " + time7);
    System.out.println("convert time7: " + time7.convert());
    time7.increment();
    System.out.println("increment time7: " + time7);
    System.out.println("convert time7: " + time7.convert());
  
   }
  
   }
  </code></pre>
  
  <p>When I run this in my code runner it says i didn't pass the assignment.
  What am I missing?
  Here's my output</p>
  
  <pre><code>time1: 0000
  convert time1 to standard time: 0am
  time1: 0000
  increment time1 five times: 0005
  
  time2: 0000
  increment time2 67 times: 0107
  convert to time2 standard time: 8am
  time2: 0107
  
  time3: 0000
  convert time3: 0am
  
  time4: 0000
  convert time4: 0am
  
  time5: 0000
  convert time5: 0am
  
  time6: 0000
  convert time6: 0am
  
  time7: 0000
  convert time7: 0am
  increment time7: 0001
  convert time7: 1am
  </code></pre>
  
  <p>Here's the expected output:</p>
  
  <pre><code> time1: 1456
   convert time1 to standard time: 2:56 PM
   time1: 1456
   increment time1 five times: 1501
  
  time2: 0012
  increment time2 67 times: 0119
  convert to time2 standard time: 1:19 AM
  time2: 0119
  
  time3: 0517
  convert time3: 5:17 AM
  
  time4: 1215
  convert time4: 12:15 PM
  
  time5: 0015
  convert time5: 12:15 AM
  
  time6: 0015
  convert time6: 12:15 AM
  
  time7: 2359
  convert time7: 11:59 PM
  increment time7: 0000
  convert time7: 12:00 AM
  </code></pre>