I need this class completed in Java Create a class named Time that contains inte
ID: 3737355 • Letter: I
Question
I need this class completed in Java
Create a class named Time that contains integer fields for hours and minutes. Store the hours in military time, that is, 0 through 23. Add a function that displays the fields, using a colon to separate hours and minutes. (Make sure the minutes display as two digits. For example, 3 o’clock should display as 3:00, not 3:0.) Add another function that takes an argument that represents minutes to add to the time. The function updates the time based on the number of minutes added. For example, 12:30 plus 15 is 12:45, 14:50 plus 20 is 15:10, and 23:59 plus 2 is 0:01. The Time constructor requires an argument for hours. The argument for minutes is optional; the value defaults to 0 if no argument is supplied. The constructor ensures that the hours field is not greater than 23 and that the minutes field is not greater than 59; default to these maximum values if the arguments to the constructor are out of range. Create a main() function that instantiates an array of at least four Time objects and demonstrates that they display correctly both before and after varying amounts of time have been added to them. Save the file as Time.java.
Explanation / Answer
Time.java
import java.io.IOException;
import java.util.Scanner;
public class Time implements Comparable<Time>
{
private int hour;
private int minute;
/*
* Sets the default time to 12:00.
*/
public Time ()
{
this(12, 0);
}
/*
* Sets hour to h and minute to m.
*/
public Time (int h, int m)
{
hour = 0;
minute = 0;
if (h >=1 && h <= 23)
hour = h;
if (m >= 1 && m <= 59)
minute = m;
}
/*
* Returns the time as a String of length 4 in the format: 0819.
*/
public String toString ()
{
String h = "";
String m = "";
if ( hour <10)
h +="0";
if (minute <10)
m +="0";
h += hour;
m+=minute;
return "" + h + "" + m;
}
//Returns -1 if current time is less than other.
//Returns 0 if current time is equal to other.
//Returns 1 if current time is greater than other.
String difference(Time t){
String s = "";
s = s + String.format("%02d", (Math.abs(this.hour-t.hour)))+":";
s = s + String.format("%02d", (Math.abs(this.minute-t.minute)));
return s;
}
public static void main(String str[]) throws IOException
{
Scanner scan = new Scanner(System.in);
// time 1
Time t1 = new Time(17, 12);
System.out.println(t1);
Time t2 = new Time(9, 45);
System.out.println(t2);
System.out.println("Greater Than:");
System.out.println(t1.compareTo(t2));
System.out.println("Less Than:");
System.out.println(t2.compareTo(t1));
System.out.println("Times equal:");
Time t3 = new Time(9, 45);
System.out.println(t3.compareTo(t2));
System.out.println("Hours equal:");
Time t4 = new Time(8, 45);
Time t5 = new Time(8, 34);
System.out.println(t4.compareTo(t5));
System.out.println(t5.compareTo(t4));
System.out.println("Difference");
System.out.println(t4.difference(t5));
System.out.println(t5.difference(t4));
System.out.println(t4.difference(t4));
}// main
public int compareTo(Time t) {
// TODO Auto-generated method stub
if(this.hour == t.hour){
if(this.minute > t.minute)
return 1;
else if(this.minute < t.minute)
return -1;
else
return 0;
}
else if(this.hour > t.hour )
return 1;
else if(this.hour < t.hour)
return -1;
return 0;
}
}// class
Output:
1712
0945
Greater Than:
1
Less Than:
-1
Times equal:
0
Hours equal:
1
-1
Difference
00:11
00:11
00:00
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.