Design and implement an Abstract Data Type (ADT) that represents the time of day
ID: 3541439 • Letter: D
Question
Design and implement an Abstract Data Type (ADT) that represents the time of day.Represent the time as hours and minutes on a 24 hour clock. The hours and minutes are the private data members of the class that implements the ADT.
Include at least two initialization operations: one that provides a default value for the time(midnight, all fields zero), and another that sets the time to a client-supplied value.These operations are the class's constructors.
Include operations that set the time, increase the present time by a number of minutes, and display the time in 12 hour and 24 hour notations.
Explanation / Answer
class clock
{
private int minutes,hours;
clock()
{
minutes =0;
hours=0;
}
clock(int x, int y)
{
hours = x;
minutes =y;
}
void setTime(int x, int y)
{
hours = x;
minutes =y;
}
void incTime(int x)
{
if(minutes+x>59)
{
minutes = (minutes+x)-60;
hours++;
}
else
minutes+=x;
}
void display12hr()
{
if (hours>12)
{
System.out.println("The time is "+(hours-12)+":"+minutes+"PM");
}
else
{
System.out.println("The time is "+(hours)+":"+minutes+"AM");
}
}
void display24hr()
{
System.out.println("The time is "+(hours)+":"+minutes);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.