试图计算和打印声明3个不同时间的总和

时间:2016-01-25 05:38:10

标签: java arrays string testing time

我正在尝试修改Relay类中的main方法,这样除了打印各种时间外,它还会打印它们的总和(所有3次的总和)。因为它们是一个字符串,我不知道数学可以在哪里完成。除此之外,我还尝试更改Relay类中的数组,以便它包含Time对象而不是TimeA对象。谢谢!

        //Relay.java
            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());

        //my clearly wrong attempt 
         System.out.println("The sum of all the runners is:  " + 
         raceLegs[0].toString() + raceLegs[1].toString()+ raceLegs[2].toString());
                }      
            }       

    //TimeATestToString.java

      public class TimeATestToString {

        @Test
        public void testClassic() {
            Time t = new TimeA(23,15,54);
            assertEquals("23:15:54", t.toString());
        }

        @Test
        public void testSingleDigits() {
            Time t = new TimeA(1,2,3);
            assertEquals("1:02:03", t.toString());
        }

        @Test
        public void testZeros() {
            Time t = new TimeA(1,3,0);
            assertEquals("1:03:00", t.toString());
        }
        @Test
        public void testZeros2() {
            Time t = new TimeA(12,0,0);
            assertEquals("12:00:00", t.toString());
        }
    }  
//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) 
//    {
//        return new TimeA(1,2,3);
//    }
    public Time add (Time other) 
    {
        int remainder = 0;
        int newSec = this.seconds + other.getSeconds();

        // if over a minute, carry.
        if (newSec >= 60) {
           remainder = 1;
           newSec -= 60;
        }
        this.seconds = newSec;
        int newMin = this.minutes + remainder + other.getMinutes();
        remainder = 0;

        // carry if over an hour
        if (newMin >= 60) {
            remainder = 1;
            newMin -= 60;
        }
        this.minutes = newMin;
        this.hours += other.getHours() + remainder;

        return this;
    }
    /**
     * 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;
    }
}
//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());
    }

    @Test
    public void thirdTest() {
        Time t = new TimeA(2,19,45);
        Time s = new TimeA(2,40,5);
        Time u = s.add(t);
        assertEquals("4:59:50", u.toString());
    }
    @Test
    public void fourthTest() {
        Time t = new TimeA(9,12,15);
        Time s = new TimeA(2,20,5);
        Time u = s.add(t);
        assertEquals("11:32:20", u.toString());
    }
}
//Time.java
public interface Time extends Comparable<Time> {

    /**
     * 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 the number of leftover minutes (those not part of a full hour) 
     *      in this object
     * @return the number of minutes
     */
    public int getMinutes();

    /**
     * Return the number of full hours in this object
     * @return the number of hours
     */
    public int getHours();

    /**
     * Return a String representation of this object in hh:mm:ss format
     * @return this object as a String 
     */
    public String toString();

    /**
     * Add the given time record to this one
     * @param other the time record to be added to this one
     * @return the sum of this time record and the other
     */
    public Time add(Time other);

}

1 个答案:

答案 0 :(得分:1)

对象的toString()方法总是返回String,但显然你将整数放入TimeA的对象中,这样你就可以进行添加。不知道那个班级是什么样的,我最好的猜测是:

class TimeA implements Time {

    private int hours;
    private int minutes;
    private int seconds;

    public TimeA(int hours, int minutes, int seconds) {
        this.hours = hours;
        this.minutes = minutes;
        this.seconds = seconds;
    }

    public void addTime(TimeA other) {
        this.hours += other.hours;
        this.minutes += other.minutes;
        this.seconds += other.seconds;
    }
}

那你怎么总结这些价值呢?没有编程你会怎么做?让我们说你有&#34; 5 + 7 + 9&#34;,你需要5,加7,然后是9,然后你就有了结果。这就是你现在想做的事情:

 raceLegs[0].add( raceLegs[1] );
 raceLegs[0].add( raceLegs[2] );

 System.out.println("Sum:  " + raceLegs[0].toString());

(我不确定你的意思&#34;我也试图更改Relay类中的数组,以便它保存Time对象&#34;,但它可能很容易将TimeA[] raceLegs = new TimeA[3]更改为Time[] raceLegs = new Time[3]。)