【问题标题】:Trouble returning sum of two times in java在java中返回两次的总和时遇到问题
【发布时间】:2016-05-01 07:21:10
【问题描述】:

这可能是一个简单的修复。我试图弄清楚如何将给定时间(其他)添加到当前时间。对于需要添加哪些时间记录,我有点困惑。我认为新的添加时间必须与已经给定的时间相加并相加?给定 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 对象的结果,则此方法作为静态方法可能会更好。通常,类上的方法意味着您正在与对象交互,因此通过向其添加另一个 TimeA 来更改 TimeA 的属性。

标签: java arrays time int return


【解决方案1】:

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

【讨论】:

  • 非常感谢!你是救生员。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多