Write the Java class that corresponds to the UML class Diagram below. You must i
ID: 3828529 • Letter: W
Question
Write the Java class that corresponds to the UML class Diagram below. You must implement each method according to its description below the class diagram. Clock() Simple default constructor that initializes fields to zero clock (hours: int, mins:int, secs:int) Simple default constructor that initializes fields to the given parameter values Must check for invalid values like negative values and values of mins/secs being greater than or equal to 60. If an invalid value is found, pnnt out an error message. +addHours(amount:int) Adds the parameter amount to the number of hours field +addMins(amount:int) Adds the parameter amount to the number of minutes field You must detect when the number of minutes meets or exceeds 60. If it does, you must add 1 to the number of hours and subtract 60 from the number of minutes +addSecs(amount:int) Adds the parameter amount to the number of seconds field You must detect when the number of seconds meets or exceeds 60. If it does, you must add 1 to the number of minutes (which could cause the number of minutes to now be at or above 60, which must be handled if this happens) and subtract 60 from the number of seconds +print() Use the printf function to print the number of hours, number of minutes and number of seconds Must place a colon in between the value of each field. Write your code on the next page. Use the following page if more room is needed. Don't forget the bonus at the end of the last page.Explanation / Answer
class Clock
{
int hours;
int mins;
int secs;
public Clock()
{
hours = mins = secs = 0;
}
public Clock(int hours, int mins, int secs)
{
this.hours = hours;
if(mins < 0 || mins >= 60)
{
mins = 0;
System.out.println("Invalid minutes passed. Initialized to 0.");
}
else
this.mins = mins;
if(secs < 0 || secs >= 60)
{
secs = 0;
System.out.println("Invalid seconds passed. Initialized to 0.");
}
else
this.secs = secs;
}
public void addHours(int amount)
{
hours += amount;
}
public void addMins(int amount)
{
mins += amount;
if(mins >= 60)
{
hours += mins / 60;
mins = mins % 60;
}
}
public void addSecs(int amount)
{
secs += amount;
if(secs >= 60)
{
addMins(secs / 60);
secs = secs % 60;
}
}
public void print()
{
System.out.printf("%02d:%02d%02d ", hours, mins, secs);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.