Object methods

Figure 202. Object methods Slide presentation
Change an object's state.

Example: Scale a rectangle.

Get dependent values

Example: Calculate a rectangle's perimeter.

Combined

Scale a rectangle and calculate its new perimeter.


Figure 203. Scaling a rectangle Slide presentation

Figure 204. Scaling method implementation Slide presentation
public class Rectangle {
    int width, height;
    boolean hasSolidBorder;

    public void scale (int factor) {
        width *= factor;
        height *= factor;
    }
}
Scaling method implementation

Figure 205. Scaling method signature Slide presentation
void  scale (int factor ) {
...}

No value is being returned to caller.

A single value of type int is being provided as method argument.


Figure 206. Using the scale(...) method Slide presentation
Rectangle r = new Rectangle();
r.width = 33;
r.height = 22;

r.scale(2);

System.out.println("width=" + r.width);
System.out.println("height=" + r.height);
width=66
height=44

Figure 207. Method definition syntax Slide presentation
public  void  scale (int factor ) { 
   width *= factor; 
   height *= factor;
}
[access modifier]  return_type  methodName  ([arguments] ) {
   [statement(s)] 
}

Optional access control modifier either of public, protected or private.

The method's return type either of:

void

The method will not return a value on completion.

A data type e.g. int, double, ...

The method will return a value of the given type to its caller.

The method's name.

Arguments being required for execution.

Start of method's body.

The method's implementation.


Figure 208. A rectangle's perimeter Slide presentation

Figure 209. getPerimeter() method implementation Slide presentation
public class Rectangle {
    int width, height;
    boolean hasSolidBorder;

    public void scale (int factor) { ... }

    public int getPerimeter() {
        return 2 * (width + height);
    }
}
getPerimeter() method implementation

Figure 210. Using Rectangle.getPerimeter() Slide presentation
Rectangle r = new Rectangle();

r.width = 33;
r.height = 22;

System.out.println("Perimeter=" + r.getPerimeter());
Perimeter=110

exercise No. 84

Compile time error

Q:

Try to compile the following snippet:

public int getMinimum(int a, int b) {
  if (a < b) {
    return a;
  } 
}

You'll encounter a Missing return statement error. What's wrong here?

On contrary the following code compiles and executes perfectly well:

public class ReturnDemo {

  int minimum;

  public void getMinimum(int a, int b) {
    if (a < b) {
      minimum = a;
      return;
    }
  }
}

A:

The compiler effectively complains about a missing if related path: In case of b <= a no return value has been defined. There are two possible solutions:

  1. public int getMinimum(int a, int b) {
      if (a < b) {
        return a;
      } else {
        return b;
      }
    }
  2. Less readable but still producing an identical result:

    public int getMinimum(int a, int b) {
      if (a < b)
        return a;
      return b;
    }

On the other hand the return statement in ReturnDemo.getMinimum(...) is unnecessary. The following code produces exactly the same result. The method will terminate anyway when ending the method's body:

public class ReturnDemo {

  int minimum;

  public void getMinimum(int a, int b) {
    if (a < b) {
      minimum = a; // Same as before but no subsequent return statement
    }
  }
}

Albeit executing well the above code is flawed: Calling e.g. getMinimum(4, 3) does not assign any value to our instance variable minimum. Correction requires an else clause:

public class ReturnDemo {

  int minimum;

  public void getMinimum(int a, int b) {
    if (a < b) {
      minimum = a; // Same as before but no subsequent return statement
    } else {
      minimum = b;
    }
  }
}

exercise No. 85

An Address class

Q:

Consider the following UK postal address sample code and desired execution result:

Code Execution result
final Address address = new Address();

address.name = "Joe";
address.surname = "Simpson";
address.street = "Featherstone Street";
address.number = "49";
address.city = "London";
address.postCode = "EC1Y 8SY";


System.out.println(address.printAddress());
Joe Simpson
49 Featherstone Street
London
EC1Y 8SY

Create an appropriate Address class. Test it using the above code example.

A:

Our Address class requires six attributes of type String and a String printAddress() method:

public class Address {

    String name;
    String surname;
    
    String city;
    String street;
    String number;
    String postCode;

    String printAddress() {
        return name + ' ' + surname + '\n' +
                number + ' ' + street + '\n' +
                city + '\n' +
                postCode;
    }
}