在java中返回两次和时出现问题

在java中返回两次和时出现问题,java,arrays,time,int,return,Java,Arrays,Time,Int,Return,这可能是一个简单的解决方案。我试图找出如何将给定的时间(其他)添加到当前时间。对于需要添加哪些时间记录,我有点困惑。我认为新的添加时间必须添加,并与已经给定的时间相加?给定add方法中的代码,输出为“1:02:03”的原因是因为在TimeA.java中,add方法返回的“TimeA(1,2,3)”与接收到的输出相同。 这里是TimeA.java public class TimeA implements Time { private int hours;

这可能是一个简单的解决方案。我试图找出如何将给定的时间(其他)添加到当前时间。对于需要添加哪些时间记录,我有点困惑。我认为新的添加时间必须添加,并与已经给定的时间相加?给定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());

    }


}       

我将假设您想要返回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;
    }

如果您只是希望返回添加两个TimeA对象的结果,那么此方法作为静态方法可能会更好。通常,类上的方法意味着您正在与对象交互,因此通过向其添加另一个TimeA来更改TimeA的属性。非常感谢!你是个救生员。