GNU   davin.50webs.com/J.T.W
a GNU world order – your home of everything that is free

Main Menu Research Projects Photo Album Curriculum Vitae The Greatest Artists
Email Address Computer Games Web Design Java Training Wheels The Fly (A Story)
Political Activism Scruff the Cat My Life Story Smoking Cessation Other Links
Tutorial 1 Tutorial 2 Tutorial 3 Tutorial 4 Tutorial 5
Tutorial 6 Tutorial 7 Tutorial 8 Tutorial 9 Tutorial 10
Tutorial 11 Tutorial 12 Tutorial 13 Tutorial 14 Tutorial 15
Tutorial 16 Tutorial 17 Tutorial 18 Using Emacs Download Links


J.T.W. tutorial 12: more questions about references


§ 12 Tutorial 12

Question 12.1: Write constructors for the classes SportsShoe and Runner below, by looking at the main function to see how many arguments each constructor has.

class SportsShoe 
begin 

   property String model;        // what kind of shoe it is 
   property double speedBoost;   // the boosting factor of the shoe 

   // constructor goes here: 

   // Useful method for debugging 
   method String toString() 
   begin 
      return "I am a shoe called " + model + " and my boosting factor is " + speedBoost; 
   end 

end 

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

   // constructor goes here: 

   // Useful method for debugging 
   method String toString() 
   begin 
      return "I am a runner and my name is " + name + " and my shoes are " + shoes; 
   end 

   
   /** 
   *** 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. 
   */ 
   function void race(Runner r1, Runner r2) 
   begin 
      if (r1.computeSpeed()>r2.computeSpeed()) 
      then begin 
         System.out.println("Runner " + r1.name + " beats " + r2.name); 
      end 
      else begin 
         System.out.println("Runner " + r2.name + " beats " + r1.name); 
      end 
   end 

   
   /** 
   ** Swaps the shoes of two runners. 
   */ 
   function void swapShoes(Runner r1, Runner r2) 
   begin 
      var SportsShoe tempShoe = r1.shoes; 
      r1.shoes = r2.shoes; 
      r2.shoes = tempShoe; 
   end 
end 

class RunnerTest 
begin 
   beginMain 
      var SportsShoe nike    = new SportsShoe("Nike NX-71", 2.0); 
      var SportsShoe reebock = new SportsShoe("Reebock R20", 2.3); 
      var SportsShoe puma    = new SportsShoe("Puma P200-MMX",4.8); 

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

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

Question 12.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. Note that 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.

Question 12.3: Fix the race method so that it checks for a draw.

Question 12.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 function.

Question 12.5: What is the difference between a method and a function? Write a one parameter method raceAgainst that behaves exactly like two-parameter function race. There are two ways of doing this, one is to optionally use the this keyword rather than one of the parameters r1 or r2. The second way is for race to simply call race using this as one of the arguments to the function.

Question 12.6: Is it true that any method can be re-worked into a function and vice versa?

Question 12.7: The swapShoes method in the Runner class swaps the shoes of two runners. Add some code to the main function to swap the shoes of two runners and verify that the shoes do indeed get swapped.

Question 12.8: Write a method called swapNames that swaps the names of two runners. You can put this function into any class but it makes the most sense to put it into the Runner class since it has two Runner parameters.

Question 12.9: Write a method swapSpeeds that swaps the speed properties of two runners.

Back to J.T.W
This page has the following hit count:
| Main Menu | Research Projects | Photo Album | Curriculum Vitae | The Greatest Artists |
| Email Address | Computer Games | Web Design | Java Training Wheels | The Fly (A Story) |
| Political Activism | Scruff the Cat | My Life Story | Smoking Cessation | Other Links |
| Tutorial 1 | Tutorial 2 | Tutorial 3 | Tutorial 4 | Tutorial 5 |
| Tutorial 6 | Tutorial 7 | Tutorial 8 | Tutorial 9 | Tutorial 10 |
| Tutorial 11 | Tutorial 12 | Tutorial 13 | Tutorial 14 | Tutorial 15 |
| Tutorial 16 | Tutorial 17 | Tutorial 18 | Using Emacs | Download Links
Last modified: Mon 15 Aug 2022 11:24:51 NZST
Best viewed at 1024x768 or above resolution.
© Copyright 1998-2022 Davin Pearson.
Please report any broken links to