Java Arena
Step 6 of 8

Methods that return objects

A method can return an object, just as it returns a number or a String. You've seen some already: a getter such as getAddress() returns the object in an instance variable, and module 19's findOldest returned one of the pets in a list. This step is about methods that make a new object and return it:

Main.java
public class Main {
    public static void main(String[] args) {
        Photo photo = new Photo(800, 600);
        Photo turned = photo.rotated();
        Photo small = photo.scaled(25);
        System.out.println(photo);
        System.out.println(turned);
        System.out.println(small);
        System.out.println(photo.rotated().scaled(50));
    }
}
Photo.java
public class Photo {
    private int width;
    private int height;

    public Photo(int width, int height) {
        this.width = width;
        this.height = height;
    }

    public Photo rotated() {
        return new Photo(this.height, this.width);
    }

    public Photo scaled(int percent) {
        return new Photo(this.width * percent / 100, this.height * percent / 100);
    }

    public String toString() {
        return this.width + " x " + this.height + " pixels";
    }
}
Output
800 x 600 pixels
600 x 800 pixels
200 x 150 pixels
300 x 400 pixels
  • The return type is the class: public Photo rotated() returns a Photo.
  • return new Photo(this.height, this.width); makes a new Photo from this photo's values and returns it. The photo the method was called on doesn't change: after both calls, photo is still 800 x 600.
  • The caller keeps the new object in a variable, as in Photo turned = photo.rotated();, or uses it right away: photo.rotated().scaled(50) rotates the photo, and then scales the rotated one.
  • Like any return value, the new object is lost if the caller doesn't use it. photo.rotated(); on a line of its own changes nothing.

Changing it, or returning a new one?

A class could also have a method that turns the photo itself around:

public void rotate() {
    int oldWidth = this.width;
    this.width = this.height;
    this.height = oldWidth;
}

photo.rotate() changes that photo, and every variable that refers to it sees the change. photo.rotated() leaves it alone and gives back a new photo. Both are fine, and the names tell them apart. String methods never change the String they're called on: word.toUpperCase() returns the text in capitals, and word stays as it was.

Returning an object that's already there

The returned object doesn't have to be new. This method returns whichever of two parcels is heavier, either the other one or the parcel it was called on, which is this:

public Parcel heavierOf(Parcel other) {
    if (other.grams > this.grams) {
        return other;
    }
    return this;
}

No new parcel is made: the caller gets a reference to one of the two that already exist.

Your task

Challenge 1 of 3

In Route.java, write the method reversed(). It returns a new Route for the way back: from where this route ends to where it starts. The route it's called on stays as it was. The check runs:

Route there = new Route("Harbor Town", "Hill Park");
Route back = there.reversed();
System.out.println(there);
System.out.println(back);

and expects:

Harbor Town to Hill Park
Hill Park to Harbor Town

Challenge 2

A journey is made of legs. In Leg.java, write the method then(Leg next). It returns a new Leg that starts where this leg starts and ends where the next leg ends, and its kilometers are the two legs' kilometers added up. Neither leg changes. The check runs:

Leg first = new Leg("Northport", "Eastvale", 120);
Leg second = new Leg("Eastvale", "Southby", 85);
Leg whole = first.then(second);
System.out.println(whole);
System.out.println(first);

and expects:

Northport to Southby, 205 km
Northport to Eastvale, 120 km

Challenge 3

What does this program print? A cutting is a small piece taken from a plant to grow a new one.