NEED HELP WITH JAVA CODE, MY CODE IS AT THE BOTTOM OF THIS PAGE HERE ARE THE INS
ID: 3834395 • Letter: N
Question
NEED HELP WITH JAVA CODE, MY CODE IS AT THE BOTTOM OF THIS PAGE
HERE ARE THE INSTRUCTIONS
HERE IS MY CODE SO FAR:
4 Modeling a Digital Watch Implement the following UML class diagram, which specifies a Watch class that main- tains the time in 24-hour format, such as 2:43:44 (hours:minutes seconds). Note that the description column to the right of the box is not related to the UML; it is provided here to describe the role of each class member. The class Watch Watch stores the hours hours int stores the minutes minutes int stores the seconds seconds int Creates a Watch object initialized to the supplied values Watch (hh int mm int SS int hh, mm, and ss. Delegates the initialization of the in- stance fields hours, minutes, and seconds to the setter methods, which are in charge of validation of the sup- plied values. Note: this is the most general constructor to which the other constructors delegate their tion tasks. Watch (hh int, mm int) Delegates the task of initializing hours to hh, minutes to mm, and seconds to 0, to the most general constructor. Watch (hh int) Delegates the task of initializing hours to hh, minutes to 0, and seconds to 0 to the most general constructor. Delegates the task of initializing hours to 0, minutes to Watch 0, and seconds to 0, to the most general constructor. Returns hours get Hours intExplanation / Answer
public class Watch extends Object {
private int hours;
private int minutes;
private int seconds;
public Watch(int hh, int mm, int ss) {
setHours(hh);
setMinutes(mm);
setSeconds(ss);
}
public Watch(int hh, int mm) {
this(hh, mm, 0);
}
public Watch(int hh) {
this(hh, 0, 0);
}
public Watch() {
this(0,0,0);
}
public int getHours() {
return this.hours;
}
public int getMinutes() {
return this.minutes;
}
public int getSeconds() {
return this.seconds;
}
public void setHours(int hh) {
if ((hh < 0) || (hh >= 24))
hh = 0;
this.hours = hh;
}
public void setMinutes(int mm) {
if ((mm < 0) || (mm >= 60)) {
mm = 0;
}
this.minutes = mm;
}
public void setSeconds(int ss) {
if ((ss < 0) || (ss >= 60)) {
ss = 0;
}
this.seconds = ss;
}
public String toString() {
String result = String.format("%02d", getHours()) + ":" +
String.format("%02d", getMinutes()) + ":" +
String.format("%02d", getSeconds());
return result;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + hours;
result = prime * result + minutes;
result = prime * result + seconds;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Watch other = (Watch) obj;
if (hours != other.hours)
return false;
if (minutes != other.minutes)
return false;
if (seconds != other.seconds)
return false;
return true;
}
public void advance(int ss) {
int tempSec = this.getHours() * 60 * 60 + this.getMinutes() * 60 + this.getSeconds();
tempSec += ss;
setSeconds(tempSec % 60);
tempSec = tempSec/60;
setMinutes(tempSec%60);
setHours(tempSec/60);
}
public void advance(int mm , int ss){
int tempSec = mm*60 + ss;
advance(tempSec);
}
public void advance(int hh ,int mm , int ss){
int tempSec = hh * 60 * 60 + mm * 60 + ss;
advance(tempSec);
}
public static void main(String[] args) {
Watch w1 = new Watch();
System.out.println("w1: " + w1);
Watch w2 = new Watch(19);
System.out.println("w2: " + w2);
Watch w3 = new Watch(3,37);
System.out.println("w3: " + w3);
Watch w4 = new Watch(16,29,45);
System.out.println("w4: " + w4);
w1.setHours(w2.getHours());
w1.setMinutes(w3.getMinutes());
w1.setSeconds(w4.getSeconds());
System.out.println("w1: " + w1);
System.out.println();
for(int i = 0; i < 10; i++)
{
w1.advance(1, 10, 15);
System.out.println("w1: " + w1);
}
System.out.println();
for(int i = 0; i < 10; i++)
{
w2.advance(30, 15);
System.out.println("w2: " + w2);
}
System.out.println();
for(int i = 0; i < 10; i++)
{
w3.advance(150);
System.out.println("w3: " + w3);
}
if(w1.equals(w4))
{
System.out.println(w1 + " is equal to " + w4);
}
else
{
System.out.println(w1 + " is not equal to " + w4);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.