In: Computer Science
java
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.
In previous assignments, we had a requirement that your class be named Main. In this assignment, the class is required to be named Time.
To get started, download the template file, 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 0000.
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 student_time_runner.java into the same folder that holds your Time.java. Execute the method student_time_runner.main, and verify that the output matches the sample run listed below.
We will use a similar but different runner to grade the program. In order to pass all tests, you will need to change student_time_runner.java to test different values to make sure your program fits the requirements. Note: You will not be submitting student_time_runner.java. This file is provided to help you test your implementation of the class Time.
When you are done coding and testing, copy and paste your entire Time class into the Code Runner and press "Submit Answers" in order for your assignment to count as turned in.
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
NOTE: You must use the class name "Time" for this assignment. REMEMBER: You must SUBMIT your answer. Your assignment does not count as complete unless it has been submitted.
The following is the solution for the above problem :
Class : Time
Instance variable: h,m
These instance variables are declared as private by conforming with ususal coding norms, accompanied by their corresponding getters/setters for better accessibility.
toString() :
Method is returning a string by appending the required "0" and in order to minimize the code ternery operator has been brought into use:
condition ? statement/action if true : statement/action if false
increment() :
Method is incrementing the minutes by 1 unit. The use cases are:
convert()
Method returns a string after formatting the input with the provided use-cases:
time = 2355; ->
23 > 12 :=> 23-12 :=> 11
Hence, 11:55 PM
Following is the complete implementation of Time class :
public class Time {
private int h;
private int m;
public Time() {
super();
}
public Time(int h, int m){
if(h>=1 && h<=23){
this.h = h;
}else{
this.h = 0;
}
if(m>=0 && m<=59){
this.m = m;
}else{
this.m = 0;
}
}
public int getH() {
return h;
}
public void setH(int h) {
this.h = h;
}
public int getM() {
return m;
}
public void setM(int m) {
this.m = m;
}
public String convert(){
String standardTime = "";
String flag = "";
if(h>= 0 && h < 12){
flag = " AM";
if(h == 0){
standardTime = Integer.toString(12);
}
else{
standardTime = Integer.toString(h);
}
}else{
flag = " PM";
if(h == 12){
standardTime = Integer.toString(12);
}
else{
standardTime = Integer.toString(h - 12);
}
}
standardTime += ":";
standardTime += Integer.toString(m).toCharArray().length == 1 ? "0"+Integer.toString(m):Integer.toString(m);
standardTime += flag;
return standardTime;
}
public void increment() {
int min = this.m;
min++;
if(min == 60){
this.m = 00;
this.h++;
if(this.h == 24){
this.h = 0;
}
}else{
this.m = min;
}
}
public String toString() {
String hour = Integer.toString(h).toCharArray().length == 1 ? "0" + Integer.toString(h) : Integer.toString(h);
String minutes = Integer.toString(m).toCharArray().length == 1 ? "0" + Integer.toString(m) : Integer.toString(m);
return hour + minutes ;
}
}