RunnerTest.java Questions

Instructions

  1. With the left mouse button pressed, drag the mouse across the contents of the program listing that is shown below. This will select the program listing. Then choose Copy from the Edit menu of your web browser to copy the program listing to the clipboard.
  2. Start your text editor and create a new file called RunnerTest.java. Choose Paste to paste the contents of the program listing from the clipboard into the new file.
  3. Save the file to disk and begin answering the questions that are shown below.
  4. When you need to look at the model answer, click here.
  5. Click here to go back to the tutorials menu.

Program listing

// REFERENCES TUTORIAL
// Copyright (C) 1998-2016 Davin Pearson
// Website: http://davin.50webs.com/java-tutorials

class SportsShoe
{

   /*
    * Properties of the class...
    */
   public String model;        // what kind of shoe it is
   public double speedBoost;   // the boosting factor of the shoe

   // constructor goes here:







   /*
    * This method allows you to print the current sports shoe.
    *
    */
   public String toString()
   {
      return ("I am a shoe called " + model +
              " and my boosting factor is " + speedBoost);
   }

}

class Runner
{
   /*
    * Properties of the class...
    */
   private String     name;     // Runner's name.
   private int        speed;    // speed of runner in km/hr.
   private SportsShoe shoes;    // which shoe they are wearing

   // constructor goes here:








   /*
    * This method allows you to print the current runner.
    *
    */
   public String toString()
   {
      return ("I am a runner and my name is " + name +
              " and the name of my shoes is " + shoes.name);
   }

   /*
    *
    * This private method computeSpeed works out the runners speed,
    * based on their basic speed and the speed boost due to the
    * SportsShoe that they are currently wearing.
    *
    */

   // method goes here:








   /*
    * Prints the result of racing two runners against each other.
    *
    */
   public static void race(Runner r1, Runner r2)
   {
      if (r1.computeSpeed() > r2.computeSpeed())
      {
         System.out.println("Runner " + r1.name +
                            " beats " + r2.name);
      }
      else
      {
         System.out.println("Runner " + r2.name +
                            " beats " + r1.name);
      }
   }

   /*
    * Swaps the shoes of two runners.
    */
   public static void swapShoes(Runner r1, Runner r2)
   {
      SportsShoe tempShoe = r1.shoes;
      r1.shoes = r2.shoes;
      r2.shoes = tempShoe;
   }
}

class RunnerTest
{
   /*
    *
    * The main method is the point of entry into the program...
    *
    */
   public static void main(String[] args)
   {
      SportsShoe nike    = new SportsShoe("Nike NX-71",   2.0);
      SportsShoe reebock = new SportsShoe("Reebock R20",  2.3);
      SportsShoe puma    = new SportsShoe("Puma P200-MMX",4.8);

      Runner sg = new Runner("Speedy Gonzalez", 55, nike);
      Runner sw = new Runner("Slick Willy",     49, reebock);
      Runner fa = new Runner("Fat Albert",      15, puma);

      Runner.race(sg,sw);
      // Runner.race(sg,sw,fa);
      // sg.raceAgainst(sw);
   }
}

// QUESTIONS:
//
// (1) Write constructors for the classes SportsShoe and
// Runner, by looking at the main method to see how many
// arguments each constructor has.
// 
// (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.
// 
// (3) Fix the "race" method so that it checks for a draw.
// 
// (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.
// 
// (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.
// 
// (6) Is it true that any non-static method could be
// re-worked into a static method and vice versa?
// 
// (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.
// 
// (8) Write a method called "swapNames" that swaps
// the names of two runners.
// 
// (9) Write a method "swapSpeeds" that swaps the "speed"
// properties of two runners.
//