dimanche 24 janvier 2016

Trying to calculate the total sum of 3 different Times and print

I am trying to modify the main method in the Relay class so that in addition to printing the various times, it also prints their sum (the sum of all 3 times). Because they are a string, i'm not sure where the math can be done. Along with this I also am trying to change the array in the Relay class so that it holds Time objects rather than TimeA objects. Thanks!

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

Aucun commentaire:

Enregistrer un commentaire