Quick Index
Goal


So far we've coded structure [instance variables (ivars)] into our classes.

Our goal now is to start coding methods. This will give our objects their behavior.

And we'll be proud of our objects, even considering they will be mischievous from time to time. We'll call this "object pride".

Do not worry about full understanding on this pass. We learn iteratively.

We've learned how to define object structure as shown here for Rectangle.

What is missing is "behavior". Objects cannot do anything without behavior (action).
public class Rectangle {

	private int width;
	private int height;

}

Methods That Do Not Return a Value


In this section we'll look at a couple methods that do not return a value.

"Review: Message Concept"

Recall the concept of an object sending a message to another...

Here we are sending the message "setSmall" to a rectangle object (instance).

The behavior of the rectangle will be to set it's size to a small size.

"Adding A Simple Method (Behavior)"

Here we are adding a simple method named "setSmall".

This method simply sets width to 5, and height to 1.
public class Rectangle {
	private int width;
	private int height;

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

	//etc (more code follows)

}

"What We Learned"

In the next sections, we'll explain what we learned.

Remember, we're just getting a feel for it here. We'll go deeper later.

"Method Body"

We learned that the Java method body (highlighted) uses the same braces {} as the class body...

We'll use braces a lot in Java.

For { and } we might use the term braces, squiggly brackets, or curly brackets.
public void setSmall()  {
	//Set ivars to arbritrary small values
	width = 5;
	height = 1;
}

"Method Header"

We learned the method has a method header similar to the class header we worked with.

One thing to note is that the method header includes the keyword void. This means the method does not return a value.

Also note that the method does not have a return statement in it.

We'll be digging deeper into method headers in an upcoming chapter.
	public void setSmall()  {
		//Set ivars to arbritrary small values
		this.width = 5;
		this.height = 1;
	}

"Using Ivars"

We assign the value 5 to the object's instance variable (ivar) width (and similarily set height to 1).

The method has access to all of the object's ivars.

Remember the terms "instance" and "object" are interchangable
public class Rectangle {
	private int width;
	private int height;

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

	//etc (more code follows)

}

Add a method "setLarge" similar to how we added "setSmall".

This method simply sets the ivars to larger values
public class Rectangle {
	private int width;
	private int height;

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

	//etc (more code follows)

}

Method That Returns a Value


"Review: Message Concept"

Recall the concept of a method that returns a value...

"Adding Method"

  • "int" means the method returns an int (integer) data type
  • Name the method "getWidth"
  • Use the Java keyword "return"
  • Return the ivar named "width" (the return statement tells Java to return the value to the method caller)
Note: The reason this method returns an int type is that it is returning the ivar width (which is of type int)
public class Rectangle {
	private int width;
	private int height;

	public 
1
int
2
getWidth()
{
3
return this.
4
width; } //etc (more code follows) }

"Using Ivars"

We learned again that we are able to access the object's ivar from the method.
public class Rectangle {
	private int width;
	private int height;

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

	//etc (more code follows)

}

Java Keyword 'return'

We learned the power of the Java keyword "return".

Also note that int in the method header means that the return value for this method is int type.
public class Rectangle {
	private int width;
	private int height;

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

	//etc (more code follows)

}

Code a method "getHeight" similar to how we added "getWidth".

This method simply returns the instance variable "height".

This method again returns a value (height) and the return data type is int (integer) as indicated in the method header
public class Rectangle {
	private int width;
	private int height;

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

	//etc (more code follows)

}
It is suggested you always code a "toString" for each class you code. That way you will get a nice string when you print objects of that type.

How you code it is up to you.

Note that this method again returns a value and the return data type is String as indicated in the method header.
public class Rectangle {
	private int width;
	private int height;

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

	//etc (more code follows)

}

Completed Code


The completed code is shown below and 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 large values
		this.width = 100;
		this.height = 50;
	}

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

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

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

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

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

}