RunnerTest.java Answers

Click here to go back to the questions
// ANSWERS:
//
// (1) Write constructors for the classes SportsShoe and
// Runner, by looking at the main method to see how many
// arguments each constructor has.
// 

// ANSWER: The following SportsShoe constructor belongs in
// the SportsShoe class:
public SportsShoe(String aModel, double aSpeedBoost)
{
   model = aModel;
   speedBoost = aSpeedBoost;
}
// The following Runner constructor belongs in the Runner
// class:
public Runner(String aName, int aSpeed, SportsShoe aShoe)
{
   name = aName;
   speed = aSpeed;
   shoes = aShoe;
}

// 
// (2) In the Runner class, write the private method
// computeSpeed that has no arguments and returns a
// double-precision floating point value that equals the
// runner's running speed.

// The speed of a runner is determined by multiplying their
// "speed" property with the "speedBoost" property of the
// shoes that they are wearing.  For example, Speedy
// Gonzalez's running speed = 55 * 2.0 = 110.0.
// 

private double computeSpeed()
{
   return speed * shoes.speedBoost;
}

// 
// (3) Fix the "race" method so that it checks for a draw.
// 

public static void race(Runner r1, Runner r2)
{
   
   if (r1.computeSpeed() > r2.computeSpeed())
   {
      System.out.println("Runner " + r1.name +
                         " beats " + r2.name);
   }
   else if (r1.computeSpeed() < r2.computeSpeed())
   {
      System.out.println("Runner " + r2.name +
                         " beats " + r1.name);
   }
   else
   {
      System.out.println("Runner " + r1.name +
                         " draws against " + r2.name);
   }
}

// 
// (4) By copying the "race" method, write a three-parameter
// race method for racing three runners against each other.
// Two methods in the same class with the same name is
// called "overloading" in Java.  Add a call to this method
// from the main method.
// 

public static void race(Runner r1, Runner r2, Runner r3)
{
   
   if (r1.computeSpeed() > r2.computeSpeed() &&
       r1.computeSpeed() > r3.computeSpeed())
   {
      System.out.println("Runner " + r1.name + " wins");
   }
   if (r2.computeSpeed() > r1.computeSpeed() &&
       r2.computeSpeed() > r3.computeSpeed())
   {
      System.out.println("Runner " + r2.name + " wins");
   }
   else
   {
      System.out.println("Runner " + r3.name + " wins");
   }
}

// 
// (5) What is the difference between a static (instance)
// method and a non-static (class) method?  Write the
// non-static method "raceAgainst" that behaves exactly like
// two-parameter method "race".  Look in the main method to
// see how many parameters raceAgainst must have.
// 

public void raceAgainst(Runner other)
{
   if (this.computeSpeed() > other.computeSpeed())
   {
      System.out.println("Runner " + this.name +
                         " beats " + other.name);
   }
   else
   {
      System.out.println("Runner " + other.name +
                         " beats " + this.name);
   }
}

// In the above example, the keyword "this" could be omitted.

// 
// (6) Is it true that any non-static method could be
// re-worked into a static method and vice versa?
// 

// ANSWER: Yes.

// 
// (7) The "swapShoes" method in the Runner class swaps the
// shoes of two runners.  Add some code to the main method
// to swap the shoes of two runners and verify that the
// shoes do indeed get swapped.
// 

System.out.println("Before swapping shoes:");
System.out.println(sg.toString());
System.out.println(sw.toString());
Runner.swapShoes(sg,sw);
System.out.println("After swapping shoes:");
System.out.println(sg.toString());
System.out.println(sw.toString());

// 
// (8) Write a method called "swapNames" that swaps
// the names of two runners.
// 

public static void swapNames(Runner r1, Runner r2)
{
   String tempName = r1.name;
   r1.name = r2.name;
   r2.name= tempName;
}

// 
// (9) Write a method "swapSpeeds" that swaps the "speed"
// properties of two runners.
// 

public static void swapSpeeds(Runner r1, Runner r2)
{
   int tempSpeed = r1.speed;
   r1.speed = r2.speed;
   r2.speed = tempSpeed;
}

//