Programming Assignment 1 Write Class Called Clock Class 3 Instance Variables One Hour One Q43866479
Programming Assignment 1
Write a class called Clock. Your class should have 3 instancevariables, one for the hour, one for the minute and one for thesecond. Your class should have the following methods:
- A default constructor that takes no parameters (make sure thisconstructor assigns values to the instance variables)
- A constructor that takes 3 parameters, one for each instancevariable
- A mutator method called setHour which takes a single integerparameter. This method sets the value of the instance variable forthe hour
- A mutator method called setMinute which takes a single integerparameter. This method sets the value of the instance variable forthe minute
- A mutator method called setSecond which takes a single integerparameter. This method sets the value of the instance variable forthe second
- An accessor method called getHour which takes no parameters andreturns the value of the instance variable for the hour
- An accessor method called getMinute which takes no parametersand returns the value of the instance variable for the minute
- An accessor method called getSecond which takes no parametersand returns the value of the instance variable for the second
- A mutator method called tick. This will be similar to what wedid in class, but this method will take an integer parameter. Thisparameter will specify how many ticks of the clock should takeplace. For example, tick(5) means to advance the clock 5 seconds.NOTE: The parameter can have a negative value, which means we tickbackwards
- An accessor method called toString which returns a stringrepresentation of the time, in the following format: HH:MM:SS AM orHH:MM:SS PM
I am including a main program for you to use to test your class,and also a screenshot of what the expected output should be. (Notethis main program doesn’t explicitly test all of your methods, butthey should still be provided.)
General notes that apply to all programmingassignments:
- Your class and all of its methods should be documented inaccordance with the Javadoc Tool.
- Every method of every class should ensure the integrity of itsinstance variables. For example, an instance variable that holdsthe number of days in a month must always be 28, 29, 30 or 31.
- You should always attempt to do your own work, but the truth iswe often get outside help, intentionally or accidentally. If youget assistance from any person or resource, you must give credit tothat person or resource. You won’t lose points. If you get outsidehelp and don’t acknowledge it, you will receive a 0.
- If you write your program with another classmate, only one ofyou should submit the assignment, including a note indicating whoyou worked with. You will both get full credit. Again, I encouragestudents to do their own work, but teamwork is not always a badthing. No more than two may collaborate on a program.
Here is the main program, just copy and paste:
public class clockTest
{
/**
* A main program to test theClock class
*/
public static void main (String[]args)
{
Clock myClock= new Clock(23, 59, 59);
System.out.println (“The time is ” +myClock.toString());
myClock.tick(1);
System.out.println (“After adding one second, the time is “+
myClock.toString());
myClock.tick(-1);
System.out.println (“After subtracting one second, the time is “+
myClock.toString());
myClock.tick(86400);
System.out.println (“After adding a full day, the time is “+
myClock.toString());
myClock.tick(43200);
System.out.println (“After adding a half day, the time is “+
myClock.toString());
myClock.tick(60);
System.out.println (“After adding a minute, the time is “+
myClock.toString());
myClock.tick(-3600);
System.out.println (“After subtracting an hour, the time is “+
myClock.toString());
}
}
Expert Answer
Answer to Programming Assignment 1 Write a class called Clock. Your class should have 3 instance variables, one for the hour, one …
OR