Overloading Methods
Written by Keith Petty   
Tuesday, 07 October 2008 00:00
Assuming that we want to compare two computers we would first need a way to rate a computer.

We are going to expand on our Computer Class by adding a method to give a rating to a computer based on a number of factors.

The data within each computer that will come into play when we rate a computer is:
	    int speed;

int diskspace;

int monitor;

boolean floppy;

boolean dvd;

The method will return an int, it will take no parameters, and we will call it getRating.

Points are calculated as follows.
Speed @ 1 pt per Mhz
Diskspace @ 10 pts per GB
Monitor @ 20 pts per diagonal inch
100 pts for a DVD
10 pts for a floppy

Here is the code----
	    int getRating() {

int rating;

rating = speed;

rating = rating + diskspace * 10;

rating = rating + monitor * 20;

if(dvd) {

rating += 100

}

if(floppy) {

rating += 10;

}

return rating;

}


We have placed this code in the Computer Class.

Here is what we do in our running application to build two computers and compare them.

  Computer dell = new Computer("Dell", 1000, 80, 21, true, true);

Computer gateway = new Computer("Gateway", 750, 40, 19, true, false);

System.out.println("Dell Rating: " + dell.getRating());

System.out.println("Gateway Rating: " + gateway.getRating());

What if we wanted to change the way we rate computers on the fly without changing the hard coded Point values we have been rating computers with?

This is a case where we could use an overloaded method to accomplish the task.

The overloaded getRating method will still return an “int but this one will take a set of parameters that represent the Point values for any given component of the computer.

Here is the code----
  int getRating(int sPoints, int dPoints, int mPoints, int fPoints, int dvdPoints) {

int score;

score = speed * sPoints;

score = score + diskspace * dPoints;

score = score + monitor * mPoints;

if(floppy) {

score = score + fPoints;

}

if(dvd) {

score = score + dvdPoints;

}

return score;

}

In our running application the two computers are compared like this.

  Computer dell = new Computer("Dell", 1000, 80, 21, true, true);

Computer gateway = new Computer("Gateway", 750, 40, 19, true, false);

System.out.println("Dell Rating: " + dell.getRating());

System.out.println("Dell using the new rating system " + dell.getRating(2, 5, 10, 10, 50));

System.out.println("Gateway Rating: " + gateway.getRating());

System.out.println("Gateway using the new rating system " + gateway.getRating(2, 5, 10, 10, 50));

This brings up another thought

Can we simplify our running application by placing the Point values in an array so we only have to pass the array?
Yes we could by defining another overloaded getRating method in the Computer Class.

Here is the code----
    int getRating(int points[]) {

// if the array is the right size we will use the overloaded method that takes the point values
if(points.length == 5) {
return getRating(points[0], points[1], points[2], points[3], points[4]);

}

// if the array is not the right size we will use the first method we wrote
return getRating();

}

So in our running application the two computers could be compared like this.

  int points[] = {5, 10, 20, 20, 500};


Computer dell = new Computer("Dell", 1000, 80, 21, true, true);

Computer gateway = new Computer("Gateway", 750, 40, 19, true, false);

System.out.println("Dell Rating: " + dell.getRating());

System.out.println("Dell using the 1st overridden rating system method " + dell.getRating(2, 5, 10, 10, 50));

System.out.println("Dell using the 2nd overridden rating system method " + dell.getRating(points));

System.out.println("Gateway Rating: " + gateway.getRating());

System.out.println("Gateway using the 1st overridden rating system method " + gateway.getRating(2, 5, 10, 10, 50));

System.out.println("Gateway using the 2nd overridden rating system method " + gateway.getRating(points));

So now we have three overloaded getRating methods that are all defined in the same Computer Class.
One that takes no parameters.
One that takes five int values.
One that takes an array of int values and then calls one of the other two based on the results of a test.