Assignment 4 Time Comparable For this assignment you will b

Assignment 4 - Time Comparable For this assignment, you will be updating the Time class from Assignment 1. To get started, you can either make a copy of your Assignment 1 Time.java, or download the solution, Time.java. This is Assignment 1: --------------------------- public class Time { private int h; private int m; public Time() { super(); } public Time(int h, int m){ if(h>=1 && h<=23){ this.h = h; }else{ this.h = 0; } if(m>=0 && m<=59){ this.m = m; }else{ this.m = 0; } } public int getH() { return h; } public void setH(int h) { this.h = h; } public int getM() { return m; } public void setM(int m) { this.m = m; } public String convert(){ String standardTime = \"\"; String flag = \"\"; if(h>= 0 && h < 12){ flag = \" AM\"; if(h == 0){ standardTime = Integer.toString(12); } else{ standardTime = Integer.toString(h); } }else{ flag = \" PM\"; if(h == 12){ standardTime = Integer.toString(12); } else{ standardTime = Integer.toString(h - 12); } } standardTime += \":\"; standardTime += Integer.toString(m).toCharArray().length == 1 ? \"0\"+Integer.toString(m):Integer.toString(m); standardTime += flag; return standardTime; } public void increment() { int min = this.m; min++; if(min == 60){ this.m = 00; this.h++; if(this.h == 24){ this.h = 0; } }else{ this.m = min; } } public String toString() { String hour = Integer.toString(h).toCharArray().length == 1 ? \"0\" + Integer.toString(h) : Integer.toString(h); String minutes = Integer.toString(m).toCharArray().length == 1 ? \"0\" + Integer.toString(m) : Integer.toString(m); return hour + minutes ; } } ----------------------------------- First, you will need to update Time so that it implements the Comparable interface. This will require adding an implements statement to the class declaration as well as the compareTo method. Then, you will need to add a difference method to the class. These two methods\' requirements are as follows: compareTo(Object other) //Returns -1 if current time is less than other. //Returns 0 if current time is equal to other. //Returns 1 if current time is greater than other. String difference(Time t) //Returns a String holding the difference between the current time and //the Time t passed in via parameter. All values should be positive, //and in the format: //Time difference: 08:09 //Time difference: 10:35 To test your code, download and run the runner class: student_runner_time.java. We will use a different, but similar, test class to grade the program. You will need to change the runner to test with other values to make sure your program fits all the requirements. Sample Run of student_runner_time.java: 1712 0945 Greater than: 1 Less than: -1 Times equal: 0 Hours equal: 1 -1 Difference Time difference: 00:11 Time difference: 00:11 Time difference: 00:00 NOTE: You MUST use the class name \"Time\" for this assignment. REMEMBER: you must SUBMIT your answer. Your assignment doesn\'t count as complete unless it has been submitted. This is what I have so far: public class Time implements Comparable{ private int h; private int m; public Time() { super(); } public Time(int h, int m){ if(h>=1 && h<=23){ this.h = h; }else{ this.h = 0; } if(m>=0 && m<=59){ this.m = m; }else{ this.m = 0; } } public int getH() { return h; } public void setH(int h) { this.h = h; } public int getM() { return m; } public void setM(int m) { this.m = m; } public String convert(){ String standardTime = \"\"; String flag = \"\"; if(h>= 0 && h < 12){ flag = \" AM\"; if(h == 0){ standardTime = Integer.toString(12); } else{ standardTime = Integer.toString(h); } }else{ flag = \" PM\"; if(h == 12){ standardTime = Integer.toString(12); } else{ standardTime = Integer.toString(h - 12); } } standardTime += \":\"; standardTime += Integer.toString(m).toCharArray().length == 1 ? \"0\"+Integer.toString(m):Integer.toString(m); standardTime += flag; return standardTime; } public void increment() { int min = this.m; min++; if(min == 60){ this.m = 00; this.h++; if(this.h == 24){ this.h = 0; } }else{ this.m = min; } } public String toString() { String hour = Integer.toString(h).toCharArray().length == 1 ? \"0\" + Integer.toString(h) : Integer.toString(h); String minutes = Integer.toString(m).toCharArray().length == 1 ? \"0\" + Integer.toString(m) : Integer.toString(m); return hour + minutes ; } public boolean equals(Time obj){ if(h==obj.h &&m == obj.m) return true; else return false; } @Override public int compareTo(Time t) //Returns -1 if current time is less than other. //Returns 0 if current time is equal to other. //Returns 1 if current time is greater than other. { if(h > t.h) return 1; else if(h < t.h){ return -1; }else{ if(m < t.m) return -1; else if(m > t.m) return 1; else return 0; } } String difference(Time t){ //Returns a String holding the difference between the current time and //the Time t passed in via parameter. All values should be positive, //and in the format: //Time difference: 08:09 //Time difference: 10:35 int totalMinutes = m + h*60; int otherTotal = t.m + t.h*60; int diff = Math.abs(totalMinutes - otherTotal); int hr = diff/60; int min = diff%60; String hrStr = Integer.toString(hr); String minStr = Integer.toString(min); if(hr < 10) hrStr = \"0\"+hrStr; if(min < 10) minStr = \"0\"+minStr; return hrStr+\":\"+minStr; } } ----- But, I\'m only getting 3/7 of it correct Your code has been evaluated against a set of test data. You had 3 out of 7 tests pass correctly. Your score is 42%. The tests that failed were: Test2: difference() Incorrect: First Time Less Incorrect: First Time Greater Incorrect: Large Difference Incorrect: Small Difference

Solution

Your code mostly seems correct one minor change as question asked for string like \"Time difference:00:11\"

Please comment if it didn\'t work with a way of running those tests.

public class Time implements Comparable<Time> {
private int h;
private int m;

public Time() {
h = 12;
m = 0;
}

public Time(int h, int m) {
if (h >= 1 && h <= 23) {
this.h = h;
} else {
this.h = 0;
}
if (m >= 0 && m <= 59) {
this.m = m;
} else {
this.m = 0;
}
}

public int getH() {
return h;
}

public void setH(int h) {
this.h = h;
}

public int getM() {
return m;
}

public void setM(int m) {
this.m = m;
}

public String convert() {
String standardTime = \"\";
String flag = \"\";
if (h >= 0 && h < 12) {
flag = \" AM\";
if (h == 0) {
standardTime = Integer.toString(12);
} else {
standardTime = Integer.toString(h);
}
} else {
flag = \" PM\";
if (h == 12) {
standardTime = Integer.toString(12);
} else {
standardTime = Integer.toString(h - 12);
}
}
standardTime += \":\";
standardTime += Integer.toString(m).toCharArray().length == 1 ? \"0\" + Integer.toString(m) : Integer.toString(m);
standardTime += flag;
return standardTime;
}

public void increment() {
int min = this.m;
min++;
if (min == 60) {
this.m = 00;
this.h++;
if (this.h == 24) {
this.h = 0;
}
} else {
this.m = min;
}
}

public String toString() {
String hour = Integer.toString(h).toCharArray().length == 1 ? \"0\" + Integer.toString(h) : Integer.toString(h);
String minutes = Integer.toString(m).toCharArray().length == 1 ? \"0\" + Integer.toString(m) : Integer
.toString(m);
return hour + minutes;
}

@Override
public int compareTo(Time o) {
if (getH() > o.getH())
{
return 1;
}
else
{
if (getH() < o.getH())
{
return -1;
}
else
{
if (getM() > o.getM())
{
return 1;
}
else
{
if (getM() < o.getM())
{
return -1;
}
else
{
return 0;
}
}
  
}
}
}
  
  
public String difference(Time t)
{
//Returns a String holding the difference between the current time and
//the Time t passed in via parameter. All values should be positive,
//and in the format:
//Time difference: 08:09
//Time difference: 10:35
  
int totalMinutes = getM() + getH()*60;
int otherTotal = t.getM() + t.getH()*60;
System.out.println(\"My time: \" + totalMinutes);
System.out.println(\"Other time: \" + otherTotal);
int diff = Math.abs(totalMinutes - otherTotal);
System.out.println(\"diff: \" + diff);
int hr = diff/60;
int min = diff%60;
  
System.out.println(\"hour: \" + hr);
System.out.println(\"min: \" + min);
  
String hrStr = Integer.toString(hr);
String minStr = Integer.toString(min);
if(hr < 10)
hrStr = \"0\"+hrStr;
if(min < 10)
minStr = \"0\"+minStr;
return \"Time difference: \" + hrStr+\":\"+minStr;
}
}

Assignment 4 - Time Comparable For this assignment, you will be updating the Time class from Assignment 1. To get started, you can either make a copy of your As
Assignment 4 - Time Comparable For this assignment, you will be updating the Time class from Assignment 1. To get started, you can either make a copy of your As
Assignment 4 - Time Comparable For this assignment, you will be updating the Time class from Assignment 1. To get started, you can either make a copy of your As
Assignment 4 - Time Comparable For this assignment, you will be updating the Time class from Assignment 1. To get started, you can either make a copy of your As

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site