需要关于java作业的帮助,标题为:时间比较
这是指定的作业: 对于此作业,您将从作业1更新时间类。要开始,您可以复制作业1 Time.java,或者下载解决方案Time.java。 首先,您需要更新Time,以便它实现可比较的接口。这将需要向类声明以及compareTo方法添加implements语句。然后,需要向类添加一个差分方法。这两种方法的要求如下:需要关于java作业的帮助,标题为:时间比较,java,Java,这是指定的作业: 对于此作业,您将从作业1更新时间类。要开始,您可以复制作业1 Time.java,或者下载解决方案Time.java。 首先,您需要更新Time,以便它实现可比较的接口。这将需要向类声明以及compareTo方法添加implements语句。然后,需要向类添加一个差分方法。这两种方法的要求如下: compareTo(Object other) //Returns -1 if current time is less than other. //R
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
要测试代码,请下载并运行runner类:student\u runner\u time.java。我们将使用一个不同但相似的测试类对该课程进行评分。您需要将runner更改为使用其他值进行测试,以确保您的程序符合所有要求
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
注意:此作业必须使用类名“Time”。记住:你必须提交你的答案。除非作业已提交,否则作业不算完成
这是我提交的:
public class Time implements Comparable<Time> {
private int hour;
private int minute;
/*
Sets the default time to 12:00.
*/
public Time() {
this(12, 0);
}
/*
Sets hour to h and minute to m.
*/
public Time(int h, int m) {
hour = 0;
minute = 0;
if (h >= 1 && h <= 23)
hour = h;
if (m >= 1 && m <= 59)
minute = m;
}
/*
Returns the time as a String of length 4 in the format: 0819.
*/
public String toString() {
String h = "";
String m = "";
if (hour < 10)
h += "0";
if (minute < 10)
m += "0";
h += hour;
m += minute;
return "" + h + "" + m;
}
public String difference(Time that) {
int thisMin = this.hour * 60 + this.minute;
int thatMin = that.hour * 60 + that.minute;
int diffMin = Math.abs(thisMin - thatMin);
int diffH = diffMin / 60;
int diffM = diffMin % 60;
String h = "";
String m = "";
if (diffH < 10) {
h += "0";
}
if (diffM < 10) {
m += "0";
}
h += diffH;
m += diffM;
return h + ":" + m;
}
@Override
public int compareTo(Time that) {
int thisMin = this.hour * 60 + this.minute;
int thatMin = that.hour * 60 + that.minute;
if (thisMin < thatMin) {
return -1;
} else if (thisMin > thatMin) {
return 1;
} else {
return 0;
}
}
}
所以调试它,看看有什么不对欢迎使用堆栈溢出!看起来你可能在请求家庭作业帮助。虽然我们对此本身没有问题,但请注意这些,并相应地编辑您的问题。(即使这不是家庭作业,也请考虑这个建议。)你可以通过查看SurvivRunEnthimeTime.java来告诉我们,在失败的测试中,什么样的值在时间中传递?看起来你可能在请求家庭作业帮助。虽然我们对此本身没有问题,但请注意这些,并相应地编辑您的问题。(即使这不是家庭作业,也请考虑这些建议。)你可以通过查看学生RunnRyTime.java,告诉你什么样的价值观会随着时间的推移而在失败的测试中传递?
Test2: difference()
Incorrect: First Time Less
Incorrect: First Time Greater
Incorrect: Large Difference
Incorrect: Small Difference