这可能是一个简单的修复。我想弄清楚如何将给定时间(其他)添加到当前时间。关于需要添加哪些时间记录,我有点困惑。我认为必须添加新的添加时间并与已经给定的时间相加?给定add方法中的代码,输出为“1:02:03”的原因是因为在TimeA.java中,add方法返回“TimeA(1,2,3)”,这是收到的相同输出。 这是TimeA.java
public class TimeA implements Time
{
private int hours;
private int minutes;
private int seconds;
/**
* Simple constructor assumes data is in proper format
* @param h number of hours
* @param m number of minutes
* @param s number of seconds
*/
public TimeA(int h, int m, int s)
{
hours = h;
minutes = m;
seconds = s;
}
/**
* Return the number of leftover seconds (those not part of a full minute)
* in this object
* @return the number of seconds
*/
public int getSeconds()
{
return seconds;
}
/**
* Return the number of leftover minutes (those not part of a full hour)
* in this object
* @return the number of minutes
*/
public int getMinutes()
{
return minutes;
}
/**
* Return the number of full hours in this object
* @return the number of hours
*/
public int getHours()
{
return hours;
}
/**
* Constructor that assumes a total number of seconds
* @param total the total number of seconds taken
*/
public TimeA(int total)
{
hours = total/3600;
minutes = (total/60) % 60;
seconds = total % 60;
}
/**
* Adds the given time to the current time, producing the sum
* @param other the given time to add
* @return the sum of this time and the other time
*/
public Time add (Time other)
{
//THIS IS WHAT NEEDS TO BE FIXED
return new TimeA(1,2,3);
}
/**
* Return a String representation of this time
* @return this time represented as a String in hh:mm:ss format
*/
public String toString()
{
return String.format("%d:%02d:%02d", hours, minutes, seconds);
// return hours + ":" + minutes + ":" + seconds;
}
public int compareTo(Time other) {
return 17;
}
}
//Time.java :
public class TimeATestAdd {
@Test
public void firstTest() {
Time t = new TimeA(0,44,19);
Time s = new TimeA(0,17,44);
Time u = s.add(t);
assertEquals("1:02:03", u.toString());
}
@Test
public void secondTest() {
Time t = new TimeA(4,19,21);
Time s = new TimeA(2,40,18);
Time u = s.add(t);
assertEquals("6:59:39", u.toString());
}
//TimeATestAdd.java:
public class TimeATestAdd {
@Test
public void firstTest() {
Time t = new TimeA(0,44,19);
Time s = new TimeA(0,17,44);
Time u = s.add(t);
assertEquals("1:02:03", u.toString());
}
@Test
public void secondTest() {
Time t = new TimeA(4,19,21);
Time s = new TimeA(2,40,18);
Time u = s.add(t);
assertEquals("6:59:39", u.toString());
}
//Relay:
public class Relay
{
public static void main(String[] args)
{
TimeA[] raceLegs = new TimeA[3];
raceLegs[0] = new TimeA(903);
raceLegs[1] = new TimeA(0,1,43);
raceLegs[2] = new TimeA(0,45,17);
System.out.println("First runner: " + raceLegs[0].toString());
System.out.println("Second runner: " + raceLegs[1].toString());
System.out.println("Third runner: " + raceLegs[2].toString());
}
}
答案 0 :(得分:0)
我将假设你想要返回TimeA对象的更新时间,在这种情况下你需要首先更改它的属性并返回它自己。
/**
* Adds the given time to the current time, producing the sum
* @param other the given time to add
* @return the sum of this time and the other time
*/
public Time add (Time other)
{
int remainder = 0;
int newSec = this.seconds + other.seconds;
// if over a minute, carry.
if (newSec >= 60) {
remainder = 1;
newSec -= 60;
}
this.seconds = newSec;
int newMin = this.minutes + remainder + other.minutes;
remainder = 0;
// carry if over an hour
if (newMin >= 60) {
remainder = 1;
newMin -= 60;
}
this.minutes = newMin;
this.hours += other.hours + remainder;
return this;
}