Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this assignment, you will create a Time class that holds an hour value and a

ID: 3673202 • Letter: F

Question

For this assignment, you will create a Time class that holds an hour value and a minute value to represent a time. We will be using "military time", so 12:01 am is 0001 and 1 pm is 1300. For this assignment, you may assume valid military times range from 0000 to 2359. Valid standard times range from 12:00 am to 11:59 pm.

class is required to be named Time.

To get started, download the template file, http://term.2.s3.amazonaws.com/week02/Time.java Your job will be to add the constructors and methods described in the following sections to the class, Time, that is declared in this template file.

Time should include two constructors:

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:

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.

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.

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.

To test your code, download the runner class http://term.2.s3.amazonaws.com/week02/student_time_runner.java into the same folder that holds your http://term.2.s3.amazonaws.com/week02/Time.java . Execute the methodstudent_time_runner.main, and verify that the output matches the sample run listed below.

In order to pass all tests, you will need to change http://term.2.s3.amazonaws.com/week02/student_time_runner.java to test different values to make sure your program fits the requirements. This file is provided to help you test your implementation of the class Time.

Sample Run:

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

Explanation / Answer

import java.io.IOException;
class Time
{
private int hour;
private int minute;
public Time()
{
this(12, 0);
}
public Time(int h, int m)
{
if (h >= 1 && h <= 23) this.hour = h; else this.hour = 0;
if (m >= 0 && m <= 59) this.minute = m; else this.minute = 0;
}
public String toString()
{
return String.format("%02d%02d", hour, minute);
}
public String convert()
{
return this.hour % 12 + ":" + this.minute + " " + (this.hour > 12 ? "am" : "pm");
}
public void increment()
{
if (this.minute == 59)
{
this.minute = 0;
if (this.hour == 23)
{
this.hour = 0;
}
else
{
this.hour++;
}
}
else
{
this.minute++;
}
}
}
class Main
{
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 + " ");
Time time2 = new Time(-7, 12);
System.out.println("time2: " + time2);
System.out.print("increment time2 67 times: ");
for (int i = 0; i < 67; i++)
time2.increment();
System.out.println(time2);
System.out.println("convert to time2 standard time: " + time2.convert());
System.out.println("time2: " + time2 + " ");
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(" time4: " + time4);
System.out.println("convert time4: " + time4.convert());
Time time5 = new Time(0, 15);
System.out.println(" time5: " + time5);
System.out.println("convert time5: " + time5.convert());
Time time6 = new Time(24, 15);
System.out.println(" time6: " + time6);
System.out.println("convert time6: " + time6.convert());
Time time7 = new Time(23,59);
System.out.println(" time7: " + time7);
System.out.println("convert time7: " + time7.convert());
time7.increment();
System.out.println("increment time7: " + time7);
System.out.println("convert time7: " + time7.convert());
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote