GNU Java Training Wheels 1.1: Tutorial 12 Overloading methods

Next: Tutorial 13 More about references, Previous: Tutorial 11 References to another class, Up: J.T.W. Tutorials [Contents][Index]

### 4.12 Tutorial 12 Overloading methods

Question 4.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
        public 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
        public 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 4.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 4.12.3: Fix the `race` `method` so that it checks for a draw.

Question 4.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 J.T.W. and Java. Add a call to this method from the `main` `function`.

Question 4.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 4.12.6: Is it true that any `method` can be re-worked into a `function` and vice versa?

Question 4.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 4.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 4.12.9: Write a method `swapSpeeds` that swaps the speed properties of two runners.

Next: Tutorial 13 More about references, Previous: Tutorial 11 References to another class, Up: J.T.W. Tutorials [Contents][Index]