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

Implement a java class named Time that contains information about the time of da

ID: 3812888 • Letter: I

Question

Implement a java class named Time that contains information about the time of day, based on a 24-hour clock. The Time class has the following instance variables (a.k.a. fields or data):

The hour of the day (from 0 to 23)
The minute of the day (from 0 to 59)

The Time class will have methods to:

Create a new Time (given a value for hour and minute) [parameterized constructor]
Create a new Time (given no parameters - initialize time to midnight: 0 hour, 0 minute) [default constructor]
Create a new Time (from another Time) [copy constructor]
getHour
getMinute
setHour
setMinute
setTime(hour, minute) - sets both the hour and minute
isAM - returns true if the hour is < 12, false otherwise
equals - method to check if one Time is the same as another by comparing all instance variables
toString - method to turn a Time into a string for display, e.g. display as "Time [23:59 PM]"

  After you complete the class, please create a driver class (called TimeDemo) that tests the Time class just created.

Explanation / Answer

The code to implementy the above program is given as:

class Time
{
//Instance variables
private int hour;
private int minute;
  
//Default Constructor
public Time()
{
hour = 0;
minute = 0;
}
  
//Parameterized Constructor
public Time(int hr, int min)
{
hour = hr;
minute = min;
}
  
//Copy Constructor
public Time(Time other)
{
this.hour = other.getHour();
this.minute = other.getMinute();
}
  
//Setter method for Hour
public void setHour(int hr)
{
this.hour = hr;
}
  
//Setter method for minute
public void setMinute(int min)
{
this.minute = min;
}
  
//Getter method for Hour
public int getHour()
{
return hour;
}
  
//Getter method for Minute
public int getMinute()
{
return minute;
}
  
//Method that sets both the hour and minute
public void setTime(int hr, int min)
{
hour = hr;
minute = min;
}
  
//Checking for AM (hours < 12)
public boolean isAM()
{
//AM (hours < 12)
if(hour < 12)
{
return true;
}
//PM (hours > 12)
else
{
return false;
}
}
  
//Checking for valid time
private boolean isValid(int hr, int min)
{
//Hour 0 to 23, Minute 0 to 59
if( (hr >= 0 && hr <= 23) && (min >= 0 && min <= 59) )
{
return true;
}
else
{
return false;
}
}
  
//to check if one Time is the same as another by comparing all instance variables
public boolean equals(Time other)
{
//Hour 0 to 23, Minute 0 to 59
if(hour == other.getHour() && minute == other.getMinute())
{
return true;
}
else
{
return false;
}
}
  
//to turn a Time into a string for display, e.g. display as "Time [23:59 PM]"
public String toString()
{
String timeStr = "";
  
//Checking for AM / PM
if(isAM())
{
timeStr += "Time [" + getHour() + ":" + getMinute()+" AM]";
}
else
{
timeStr += "Time [" + getHour() + ":" + getMinute()+" PM]";
}
  
//Returning string representation
return timeStr;
}
}

//Demo class for Time class
class TimeDemo
{
//Main method
public static void main(String args[])
{
//Time objects
Time t1 = new Time(6, 23);
Time t2 = new Time(14, 55);
Time t3 = new Time(6, 23);
  
//Printing Time objects
System.out.println(t1);
System.out.println(t2);
System.out.println(t3);
  
//Comparing objects
System.out.println(" Comparing time objects t1 & t2: " + t1.equals(t2) + " ");
System.out.println(" Comparing time objects t1 & t3: " + t1.equals(t3) + " ");
}
}

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