Java Arena
Step 4 of 6

Methods of an object

A Plant can be built now, but it can't do anything. What an object can do is given by its methods, written in the class after the constructor. Here's one that prints the plant's details:

Main.java
public class Main {
    public static void main(String[] args) {
        Plant fern = new Plant("fern", 30);
        Plant cactus = new Plant("cactus", 12);
        fern.printInfo();
        cactus.printInfo();
        fern.printInfo();
    }
}
Plant.java
public class Plant {
    private String name;
    private int height;

    public Plant(String name, int height) {
        this.name = name;
        this.height = height;
    }

    public void printInfo() {
        System.out.println(this.name + ", " + this.height + " cm");
    }
}
Output
fern, 30 cm
cactus, 12 cm
fern, 30 cm
  • The method has no static. The methods in modules 10 and 11 were static: they belong to the class as a whole, and you call them on their own, as in printBanner();. A method without static belongs to each object, and you call it on an object: fern.printInfo();.
  • Inside the method, this is the object the method was called on. In fern.printInfo(), this.name is "fern"; in cactus.printInfo(), it's "cactus". The method is written once, and every object runs it with its own variables.
  • The method is public, so Main can call it, while the variables stay private. void means it returns nothing, as before.

A call needs an object in front of the dot. printInfo(); alone in main looks for a method of Main, and Plant.printInfo(); doesn't say which plant's info to print, so neither compiles.

Parameters and conditions

A method of an object can take parameters, like any method, and use them together with the object's own variables. printFits tells whether the plant fits under a shelf of a given height:

Main.java
public class Main {
    public static void main(String[] args) {
        Plant fern = new Plant("fern", 30);
        Plant cactus = new Plant("cactus", 12);
        fern.printFits(25);
        cactus.printFits(25);
    }
}
Plant.java
public class Plant {
    private String name;
    private int height;

    public Plant(String name, int height) {
        this.name = name;
        this.height = height;
    }

    public void printInfo() {
        System.out.println(this.name + ", " + this.height + " cm");
    }

    public void printFits(int shelfHeight) {
        if (this.height < shelfHeight) {
            System.out.println("The " + this.name + " fits under the shelf");
        } else {
            System.out.println("The " + this.name + " is too tall for the shelf");
        }
    }
}
Output
The fern is too tall for the shelf
The cactus fits under the shelf

shelfHeight comes from the call, and this.height from the object. A method of an object can hold anything a static method can: variables, conditions, loops and calls.

Your task

Challenge 1 of 3

Tram.java has a constructor for a tram's line number and destination. Add a method printSign() that prints them on one line, like the sign on the front of the tram. The check runs:

Tram tram = new Tram(4, "Harbor");
tram.printSign();

and expects:

Line 4 to Harbor

Challenge 2

Write the method printCrossing(int tons) for Bridge. It tells whether a vehicle that weighs tons may cross: a vehicle up to the bridge's limit may, a heavier one may not. The check runs:

Bridge bridge = new Bridge("Stone Bridge", 20);
bridge.printCrossing(12);
bridge.printCrossing(35);

and expects:

12 tons: may cross Stone Bridge
35 tons: too heavy for Stone Bridge

Challenge 3

Write the method print() for Banner: it prints the banner's text in a frame of * characters. The frame is 4 characters wider than the text, with a star and a space on each side of it. The check runs:

Banner banner = new Banner("Sale today");
banner.print();

and expects:

**************
* Sale today *
**************