Create a program named Clock.java. Use the following header and instance variabl
ID: 673684 • Letter: C
Question
Create a program named Clock.java. Use the following header and instance variables. Write a class called Clock. Here are the first few lines:
public class Clock {
// instance variables
private int hour;
private int minute;
private int second;
Write the following methods:
a) Default constructor. Initialize hour to 12, minute to 0, and second to zero.
b) 3 accessors: one for each instance variable.
c) 3 mutators: one for each instance variable. Mutator for hour should perform data validation to insure that the value is between 1 and 12 (inclusive). Mutators for minute and second should insture that the value is between 0 and 59 (inclusive).
d) Write a toString method that will return a String representation of a Clock object using the format hour : minute : second. Each integer should occupy 2 digits. For example, if hour contains 12, minute contains 5, and second contains 9, the toString method should return the String “12:05:09”.
e) Write a method called tick that will add 1 to the number of seconds, then check to see that second, minute, and hour contain valid data. Adjust the instance variables as needed.
f) Write an equals method that will return true if the Clock parameter has the same hour, minute, and second as the calling object.
Write a program called TestClock.java.
In a manner similar to what was done with TestCableCar.java, perform tests on all of the methods in the Clock class.
If anyone can help me with this, I would be extermley thankful, because I have no idea how to do this, and I have asked my teacher and I just dont understand it, so I am hoping someone can help me here, thank you in advanced for any given help.
Explanation / Answer
Clock.java :
public class Clock {
// instance variables
private int hour;
private int minute;
private int second;
// Default constructor.
// Initialize hour to 12, minute to 0, and second to zero.
public Clock() {
hour = 12;
minute = 0;
second = 0;
}
// 3 accessors: one for each instance variable.
/**
* @return the hour
*/
public int getHour() {
return hour;
}
/**
* @return the minute
*/
public int getMinute() {
return minute;
}
/**
* @return the second
*/
public int getSecond() {
return second;
}
/*
* 3 mutators: one for each instance variable. Mutator for hour should
* perform data validation to insure that the value is between 1 and 12
* (inclusive). Mutators for minute and second should insture that the value
* is between 0 and 59 (inclusive).
*/
/**
* @param hour
* the hour to set
*/
public void setHour(int hour) {
if (hour <= 12 && hour >= 1) {
this.hour = hour;
}
}
/**
* @param minute
* the minute to set
*/
public void setMinute(int minute) {
if (minute <= 59 && minute >= 0) {
this.minute = minute;
}
}
/**
* @param second
* the second to set
*/
/**
* @param second
*/
public void setSecond(int second) {
if (second <= 59 && second >= 0) {
this.second = second;
}
}
/*
* Write a toString method that will return a String representation of a
* Clock object using the format hour : minute : second. Each integer should
* occupy 2 digits. For example, if hour contains 12, minute contains 5, and
* second contains 9, the toString method should return the String
* “12:05:09”.
*/
@Override
public String toString() {
return String.format("%02d", hour) + ":"
+ String.format("%02d", minute) + ":"
+ String.format("%02d", second);
}
/*
* Write a method called tick that will add 1 to the number of seconds, then
* check to see that second, minute, and hour contain valid data. Adjust the
* instance variables as needed.
*/
public void tick() {
this.second += 1;
// add overflow to minutes from seconds
this.minute += (int) (this.second / 60);
// update seconds
this.second = this.second % 60;
// add overflow to minutes from seconds
this.hour += (int) (this.minute / 60);
// update minutes
this.minute = this.minute % 60;
// adjust hours
this.hour = this.hour % 24;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + hour;
result = prime * result + minute;
result = prime * result + second;
return result;
}
/*
* Write an equals method that will return true if the Clock parameter has
* the same hour, minute, and second as the calling object
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Clock other = (Clock) obj;
if (hour != other.hour)
return false;
if (minute != other.minute)
return false;
if (second != other.second)
return false;
return true;
}
}
TestClock.java:
public class TestClock {
public static void main(String ar[]) {
Clock clock1 = new Clock();
Clock clock2 = new Clock();
System.out.println("Default Contructor:" + clock1.toString());
// Adding 1 second to the clock
clock1.tick();
System.out.println("Add 1 Second to clock1: " + clock1.toString());
// setting clock using mutators
clock2.setHour(11);
clock2.setMinute(59);
clock2.setSecond(59);
// checking equality
if (clock1.equals(clock2)) {
System.out.println("Clock1 : " + clock1 + " and Clock2 : " + clock2
+ " are equal");
} else {
System.out.println("Clock1 : " + clock1 + " and Clock2 : " + clock2
+ " are not equal");
}
// if we add 2 seconds to clock2
clock2.tick();
clock2.tick();
System.out.println("After adding two seconds to clock2 : " + clock2);
// checking equality
if (clock1.equals(clock2)) {
System.out.println("Clock1 : " + clock1 + " and Clock2 : " + clock2
+ " are equal");
} else {
System.out.println("Clock1 : " + clock1 + " and Clock2 : " + clock2
+ " are not equal");
}
}
}
output :
Default Contructor:12:00:00
Add 1 Second to clock1: 12:00:01
Clock1 : 12:00:01 and Clock2 : 11:59:59 are not equal
After adding two seconds to clock2 : 12:00:01
Clock1 : 12:00:01 and Clock2 : 12:00:01 are equal
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.