CS 149 Algorithm Development
Lab11B: Method Overloading


Objectives:

Students will be able to:

Background:

Object Oriented programming switches the view of a program as a series of steps to a view of a program as an interaction between objects. You design the objects with what they should look like (instance variables, fields) and what behaviors they should have (methods). Then your driver will control how those objects interact in the world. In this version of the lab, you will be extending your Car class to do some new behaviors.

New Terms:

Materials:


Acknowledgment:    Gaddis, Programming Challenges 2, 4th edition


Part 1:  General Instructions:

  1. Setup your program environment by creating a new folder named lab11b.

  2. Copy your Car and CarTester classes from the previous lab into the new directory.

Part 2: Beginning - Revisit your constructor:

  1. In your constructor, you probably used parameter names (for the model and year) that are different from the instance variable names. We are going to look at "shadowing" (see Gaddis 6.5 (Puzzle) or 6.7 (Watermelon)).

    Change the names of the two constructor parameters to the same name as the Car instance variables. The body of the constructor is now within the scope of both the parameters and the instance variables.

  2. Run your CarTester. Look in particular at the toString call after you call the constructor. What do you see?
    (No formal answer required, just think through what happened).

    Java uses the rule that when there are two identifiers with the same name in scope, the one with the most local scope (the parameter in this case) has precedence. The other (the instance variable in this case) is "shadowed" by the local variable.

    So the statement modelYear = modelYear; does not move the value from the parameter to the instance variable; rather it moves it from the parameter to the parameter.

  3. Inside of the class, the built-in reference variable "this" refers to the object that is executing the method (see Gaddis 9.8 (Puzzle) or 8.8 (Watermelon)). We solve the shadowing problem by referring to the instance variable as this.modelYear. Now the compiler knows that we are moving the data from the parameter to the instance variable. Change the assignment statements in the constructor to use this. on the left-hand side, then rerun your tester. Is everything okay?

  4. The this. qualifier should be used for any reference to instance variables, even though it's not generally required. It is good practice because it makes it clear in any method which variables are local to the method and which belong to the object itself.

    Change each of the references to instance variables in the Car methods to this.variableName .

Part 3: Overloading a constructor:

  1. Add to your Car class a color instance variable that is a String.

  2. Build a new constructor (also keep the original constructor) that takes in the same parameters as the first one plus color.

  3. In the original constructor, set color to the empty String, "";

  4. Test the new constructor in your tester by making a new car with a color argument filled in.

  5. Add color to your toString method. It should read ("A %s %d %s that is going %d mph", color, year, make, speed)

  6. Test the new Car constructor by creating a Car with color and displaying its toString() return value.

4 Refinement:

  1. Car has two mutator methods, accelerate() and brake(). Add error checking to these methods. For accelerate(), assume an upper limit of 150 mph and don't increase beyond that point.

  2. For brake(), limit the lower range of speed to 0 mph.

  3. Now let's overload the methods. For each (accelerate and brake), create a new version of the method that takes a parameter for the amount to increase or decrease the speed. Adhere to the limits that you just placed into the former accelerate() and brake() methods.

  4. In your tester, test that it's not possible to accelerate to more than 150 or to brake below 0. Be sure to test both versions of the methods.

5 Further refinement:

Instead of having the top speed built into the methods, wouldn't it be better to include it as an instance variable (instance variable) for that car?

  1. Add an instance variable for the top speed (bottom speed will always be assumed to be 0).

  2. Add another constructor which includes maxSpeed as well as the make, year, and color. This constructor should insure that the top speed value is > 0. If not, set the top speed to 150.

  3. For the other two constructors, add a statement to set the top speed to 150.
     
  4. Also add topSpeed to the toString method.

  5. Test your new constructor and retest the former constructors.

  6. Change your code for accelerate() to insure that it will respect the top speed limit. Test this change, making sure that you try to accelerate() each of your cars past the top speed and insure that the speed is cutoff at the appropriate place.

6 (OPTIONAL) Exploring a copy constructor:

In some instances it is necessary to make a copy of a particular object. We can provide a copy constructor that will set the values of "this" object to the values of the passed object. Reference is Gaddis 9.6.

  1. Create a new constructor that accepts a Car object as its only parameter.

  2. Set the instance variables to the corresponding values of the Car parameter. You will need to add "get" methods for instance variables that don't already have them.

  3. Test by creating a new Car based on one of the other four cars you have created. accelerate() one, but not the other. Display (using the toString) to see that they are in fact different objects.

7 (OPTIONAL) Exploring variable length argument lists - Assumes that you have made a crash method in the former lab:

You have created a crash method which takes in a single car to crash with this car. What if you want two cars to crash with this car? What about three? We can build overloaded methods that take in differing numbers of car objects and then will set each of their speeds to 0. But there is a better way. Reference Gaddis (Watermelon) 8.12.

  1. Create a method which takes in a variable length parameter list, modeled after the totalBalance method that you find in VarargsDemo2.

  2. For each element in the incoming array, and for the calling object, set all of the speeds to 0.

  3. Test this in your Tester with differing numbers of Car objects. Try it with one, with two, with three, and with an array of Car. Will it work with no Cars at all

Updated 11/03/2013 (rfg, nlh)