Quick Index
Goal


In our previous work we coded a number of methods.

Those methods did not include any method parameters (method variables / arguments).

Often we will want to "pass" data with messages.

The goal of this chapter is to code methods that will handle method parameters. That way, data can be passed along with messages.

We will use the words "parameter" and "argument" interchangeably. Note they are both just other words for "variable".

Again do not worry about full understanding on this pass -- we're learning iteratively.

Getting Started


We can first download these classes (from our previous work):


Put both files in a new working directory

Parameters (Concept)


Recall the concept of sending arguments with a message ...

Here we are sending the message setWidth to a Rectangle object (instance) and passing the argument value of 10.

Parameter


Coding Method With Parameter
Here we are coding the method setWidth and providing one method parameter named "aWidth".

This method sets the ivar width to the passed parameter (aWidth).

Note the use of the special variable this which refers to "this" object (i.e., the Rectangle we are coding)(.
	public void setWidth(int aWidth)  {
		this.width = aWidth;
	}
Sending Message With Parameter
Let's send the message to the method we just coded in the previous step.

We send the message setWidth to the object rec and "pass" the parameter 11.

We may also say call method for send message.
	public void testSetWidth()  {
		Rectangle rec;
		rec = new Rectangle();
		rec.setWidth(11);
		System.out.println("\n-- testSetWidth --");
		System.out.println("Set Width to 11");
		System.out.println("Actual Width: " + rec.getWidth());
	}


Two Parameters


Coding Method With Two Parameters
This method receives two parameters aWidth and aHeight.

Note that this.width refers to the ivar width for this object (similar for this.height)

Note that methods with more parameters (three, four, etc) would follow this same pattern.
	public void setDimensions(int aWidth, int aHeight)  {
		this.width = aWidth;
		this.height = aHeight;
	}
Sending Message With Two Parameters
We send the message setDimensions to the object rec and "pass" the parameters 10 and 5.
	public void testSetDimensions()  {
		Rectangle rec;
		rec = new Rectangle();
		rec.setDimensions(8, 7);
		System.out.println("\n-- testSetDimensions --");
		System.out.println("Set dimensions to 8 and 7");
		System.out.println("Actual Width: " + rec.getWidth());
		System.out.println("Actual Height: " + rec.getHeight());
	}


Method Overloading


Java allows us to implement code the same method (name) in multiple ways as long as the parameters are different.

We overload the "growBy" method below. We code up the method two times. Notice that each method has a different set of parameters.

	public void growBy(int amount)  {
		this.width = this.width + amount;
		this.height = this.height + amount;
	}

	public void growBy(int widthAmount, int heightAmount)  {
		this.width = this.width + widthAmount;
		this.height = this.height + heightAmount;
	}

Thus we could call this method via either of these:


Completed Code


The finished Rectangle code is shown below, and is downloadable here...

public class Rectangle {

	private int width;
	private int height;

	public void setSmall() {
		//Set ivars to arbritrary small values
		this.width = 5;
		this.height = 1;
	}

	public void setLarge() {
		//Set ivars to arbritrary small values
		this.width = 100;
		this.height = 50;
	}

	//------------------------------------------
	//Getters

	public int getWidth() {
		return this.width;
	}

	public int getHeight() {
		return this.height;
	}

	//------------------------------------------
	//Setters

	public void setWidth(int aWidth) {
		this.width = aWidth;
	}

	public void setHeight(int aHeight) {
		this.height = aHeight;
	}

	//------------------------------------------
	//Common

	public String toString() {
		//Return a "nice" display string
		return "" + this.width + " x " + this.height;
	}

	//------------------------------------------

	public void setDimensions(int aWidth, int aHeight) {
		this.width = aWidth;
		this.height = aHeight;
	}

	public void growBy(int amount) {
		this.width = this.width + amount;
		this.height = this.height + amount;
	}

	public void growBy(int widthAmount, int heightAmount) {
		this.width = this.width + widthAmount;
		this.height = this.height + heightAmount;
	}

}

Test Code


Whenever we add new code we want to test it to make sure it works.

The updated RectangleTests code is below. It adds a test for each new Rectangle method.

And it is downloadable here...

public class RectangleTests {

	public void testSetSmall() {

		Rectangle rec;
		rec = new Rectangle();
		rec.setSmall();

		System.out.println("\n-- testSetSmall --");
		System.out.println("Width: " + rec.getWidth());
		System.out.println("Height: " + rec.getHeight());
	}

	public void testSetLarge() {

		Rectangle rec;
		rec = new Rectangle();
		rec.setLarge();

		System.out.println("\n-- testSetLarge --");
		System.out.println("Width: " + rec.getWidth());
		System.out.println("Height: " + rec.getHeight());
	}

	public void testSetWidth() {
		Rectangle rec;
		rec = new Rectangle();
		rec.setWidth(11);
		System.out.println("\n-- testSetWidth --");
		System.out.println("Set Width to 11");
		System.out.println("Actual Width: " + rec.getWidth());
	}

	public void testSetHeight() {
		Rectangle rec;
		rec = new Rectangle();
		rec.setHeight(5);
		System.out.println("\n-- testSetHeight --");
		System.out.println("Set Height to 5");
		System.out.println("Actual Height: " + rec.getHeight());
	}

	public void testSetDimensions() {
		Rectangle rec;
		rec = new Rectangle();
		rec.setDimensions(8, 7);
		System.out.println("\n-- testSetDimensions --");
		System.out.println("Set dimensions to 8 and 7");
		System.out.println("Actual Width: " + rec.getWidth());
		System.out.println("Actual Height: " + rec.getHeight());
	}

	public void testGrowByWithOneParam() {
		Rectangle rec;
		rec = new Rectangle();
		rec.setDimensions(100, 100);
		rec.growBy(5);
		System.out.println("\n-- testGrowByWithOneParam --");
		System.out.println("Sent growBy with 5 (after setting dimensions to 100)");
		System.out.println("Actual Width: " + rec.getWidth());
		System.out.println("Actual Height: " + rec.getHeight());
	}

	public void testGrowByWithTwoParams() {
		Rectangle rec;
		rec = new Rectangle();
		rec.setDimensions(100, 100);
		rec.growBy(3, 2);
		System.out.println("\n-- testGrowByWithTwoParams --");
		System.out.println("Sent growBy with 3 and 2 (after setting dimensions to 100)");
		System.out.println("Actual Width: " + rec.getWidth());
		System.out.println("Actual Height: " + rec.getHeight());
	}

	//=======================================

	public static void main(String[] args) {
		RectangleTests test = new RectangleTests();
		test.testSetSmall();
		test.testSetLarge();
		test.testSetWidth();
		test.testSetHeight();
		test.testSetDimensions();
		test.testGrowByWithOneParam();
		test.testGrowByWithTwoParams();
	}

}