Question
Programming Assignment 1 Write a class called Clock. Your class should have 3 instance variables, one for the hour, one for the minute and one
Programming Assignment 1
Write a class called Clock. Your class should have 3 instance variables, one for the hour, one for the minute and one for the second. Your class should have the following methods:
- A default constructor that takes no parameters (make sure this constructor assigns values to the instance variables)
- A constructor that takes 3 parameters, one for each instance variable
- A mutator method called setHour which takes a single integer parameter. This method sets the value of the instance variable for the hour
- A mutator method called setMinute which takes a single integer parameter. This method sets the value of the instance variable for the minute
- A mutator method called setSecond which takes a single integer parameter. This method sets the value of the instance variable for the second
- An accessor method called getHour which takes no parameters and returns the value of the instance variable for the hour
- An accessor method called getMinute which takes no parameters and returns the value of the instance variable for the minute
- An accessor method called getSecond which takes no parameters and returns the value of the instance variable for the second
- A mutator method called tick. This will be similar to what we did in class, but this method will take an integer parameter. This parameter will specify how many ticks of the clock should take place. For example, tick(5) means to advance the clock 5 seconds. NOTE: The parameter can have a negative value, which means we tick backwards
- An accessor method called toString which returns a string representation of the time, in the following format: HH:MM:SS AM or HH: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. (Note this main program doesnt explicitly test all of your methods, but they should still be provided.)
General notes that apply to all programming assignments:
- Your class and all of its methods should be documented in accordance with the Javadoc Tool.
- Every method of every class should ensure the integrity of its instance variables. For example, an instance variable that holds the 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 is we often get outside help, intentionally or accidentally. If you get assistance from any person or resource, you must give credit to that person or resource. You wont lose points. If you get outside help and dont acknowledge it, you will receive a 0.
- If you write your program with another classmate, only one of you should submit the assignment, including a note indicating who you worked with. You will both get full credit. Again, I encourage students to do their own work, but teamwork is not always a bad thing. 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 the Clock 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());
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started