{"id":"classes-methods","slug":"methods-of-an-object","title":"Methods of an object","text":"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:\n\n```java\npublic class Main {\n    public static void main(String[] args) {\n        Plant fern = new Plant(\"fern\", 30);\n        Plant cactus = new Plant(\"cactus\", 12);\n        fern.printInfo();\n        cactus.printInfo();\n        fern.printInfo();\n    }\n}\n// ==== Plant.java ====\npublic class Plant {\n    private String name;\n    private int height;\n\n    public Plant(String name, int height) {\n        this.name = name;\n        this.height = height;\n    }\n\n    public void printInfo() {\n        System.out.println(this.name + \", \" + this.height + \" cm\");\n    }\n}\n```\n\n```output\nfern, 30 cm\ncactus, 12 cm\nfern, 30 cm\n```\n\n- 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();`.\n- 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.\n- The method is `public`, so `Main` can call it, while the variables stay `private`. `void` means it returns nothing, as before.\n\nA 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.\n\n### Parameters and conditions\n\nA 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:\n\n```java\npublic class Main {\n    public static void main(String[] args) {\n        Plant fern = new Plant(\"fern\", 30);\n        Plant cactus = new Plant(\"cactus\", 12);\n        fern.printFits(25);\n        cactus.printFits(25);\n    }\n}\n// ==== Plant.java ====\npublic class Plant {\n    private String name;\n    private int height;\n\n    public Plant(String name, int height) {\n        this.name = name;\n        this.height = height;\n    }\n\n    public void printInfo() {\n        System.out.println(this.name + \", \" + this.height + \" cm\");\n    }\n\n    public void printFits(int shelfHeight) {\n        if (this.height < shelfHeight) {\n            System.out.println(\"The \" + this.name + \" fits under the shelf\");\n        } else {\n            System.out.println(\"The \" + this.name + \" is too tall for the shelf\");\n        }\n    }\n}\n```\n\n```output\nThe fern is too tall for the shelf\nThe cactus fits under the shelf\n```\n\n`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.\n","task":"`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:\n\n```java\nTram tram = new Tram(4, \"Harbor\");\ntram.printSign();\n```\n\nand expects:\n\n```\nLine 4 to Harbor\n```\n","kind":"code","seed":"// ==== Tram.java ====\npublic class Tram {\n    private int line;\n    private String destination;\n\n    public Tram(int line, String destination) {\n        this.line = line;\n        this.destination = destination;\n    }\n\n    // Write the printSign method here.\n}\n\n// ==== Main.java ====\npublic class Main {\n    public static void main(String[] args) {\n        Tram tram = new Tram(4, \"Harbor\");\n        tram.printSign();\n    }\n}\n","solution":"// ==== Tram.java ====\npublic class Tram {\n    private int line;\n    private String destination;\n\n    public Tram(int line, String destination) {\n        this.line = line;\n        this.destination = destination;\n    }\n\n    public void printSign() {\n        System.out.println(\"Line \" + this.line + \" to \" + this.destination);\n    }\n}\n\n// ==== Main.java ====\npublic class Main {\n    public static void main(String[] args) {\n        Tram tram = new Tram(4, \"Harbor\");\n        tram.printSign();\n    }\n}\n","hints":["The method goes after the constructor, inside the class, and without `static`: `public void printSign() {`.","Print with the object's own variables: `System.out.println(\"Line \" + this.line + \" to \" + this.destination);`."],"tests":[{"name":"Test 1","stdin":"","call":"Tram tram = new Tram(4, \"Harbor\");\ntram.printSign();\n","expect":"Line 4 to Harbor","hidden":false},{"name":"Test 2","stdin":"","call":"Tram tram = new Tram(12, \"Airport\");\ntram.printSign();\n","expect":"Line 12 to Airport","hidden":true},{"name":"Test 3","stdin":"","call":"Tram first = new Tram(1, \"Zoo\");\nTram second = new Tram(9, \"Old Town\");\nsecond.printSign();\nfirst.printSign();\nsecond.printSign();\n","expect":"Line 9 to Old Town\nLine 1 to Zoo\nLine 9 to Old Town","hidden":true}],"require":[],"forbid":[],"more":[{"task":"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:\n\n```java\nBridge bridge = new Bridge(\"Stone Bridge\", 20);\nbridge.printCrossing(12);\nbridge.printCrossing(35);\n```\n\nand expects:\n\n```\n12 tons: may cross Stone Bridge\n35 tons: too heavy for Stone Bridge\n```\n","kind":"code","seed":"// ==== Bridge.java ====\npublic class Bridge {\n    private String name;\n    private int maxTons;\n\n    public Bridge(String name, int maxTons) {\n        this.name = name;\n        this.maxTons = maxTons;\n    }\n\n    // Write the printCrossing method here.\n}\n\n// ==== Main.java ====\npublic class Main {\n    public static void main(String[] args) {\n        Bridge bridge = new Bridge(\"Stone Bridge\", 20);\n        bridge.printCrossing(12);\n        bridge.printCrossing(35);\n    }\n}\n","solution":"// ==== Bridge.java ====\npublic class Bridge {\n    private String name;\n    private int maxTons;\n\n    public Bridge(String name, int maxTons) {\n        this.name = name;\n        this.maxTons = maxTons;\n    }\n\n    public void printCrossing(int tons) {\n        if (tons <= this.maxTons) {\n            System.out.println(tons + \" tons: may cross \" + this.name);\n        } else {\n            System.out.println(tons + \" tons: too heavy for \" + this.name);\n        }\n    }\n}\n\n// ==== Main.java ====\npublic class Main {\n    public static void main(String[] args) {\n        Bridge bridge = new Bridge(\"Stone Bridge\", 20);\n        bridge.printCrossing(12);\n        bridge.printCrossing(35);\n    }\n}\n","hints":["The method takes a parameter: `public void printCrossing(int tons) {`. It compares the parameter with the bridge's own limit, `this.maxTons`.","A vehicle that weighs exactly the limit may cross: `if (tons <= this.maxTons)`."],"tests":[{"name":"Test 1","stdin":"","call":"Bridge bridge = new Bridge(\"Stone Bridge\", 20);\nbridge.printCrossing(12);\nbridge.printCrossing(35);\n","expect":"12 tons: may cross Stone Bridge\n35 tons: too heavy for Stone Bridge","hidden":false},{"name":"Test 2","stdin":"","call":"Bridge bridge = new Bridge(\"Stone Bridge\", 20);\nbridge.printCrossing(20);\nbridge.printCrossing(21);\n","expect":"20 tons: may cross Stone Bridge\n21 tons: too heavy for Stone Bridge","hidden":true},{"name":"Test 3","stdin":"","call":"Bridge rope = new Bridge(\"Rope Bridge\", 1);\nBridge steel = new Bridge(\"Steel Bridge\", 44);\nrope.printCrossing(2);\nsteel.printCrossing(2);\n","expect":"2 tons: too heavy for Rope Bridge\n2 tons: may cross Steel Bridge","hidden":true}],"require":[],"forbid":[]},{"task":"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:\n\n```java\nBanner banner = new Banner(\"Sale today\");\nbanner.print();\n```\n\nand expects:\n\n```\n**************\n* Sale today *\n**************\n```\n","kind":"code","seed":"// ==== Banner.java ====\npublic class Banner {\n    private String text;\n\n    public Banner(String text) {\n        this.text = text;\n    }\n\n    // Write the print method here.\n}\n\n// ==== Main.java ====\npublic class Main {\n    public static void main(String[] args) {\n        Banner banner = new Banner(\"Sale today\");\n        banner.print();\n    }\n}\n","solution":"// ==== Banner.java ====\npublic class Banner {\n    private String text;\n\n    public Banner(String text) {\n        this.text = text;\n    }\n\n    public void print() {\n        String border = \"\";\n        for (int i = 0; i < this.text.length() + 4; i++) {\n            border += \"*\";\n        }\n        System.out.println(border);\n        System.out.println(\"* \" + this.text + \" *\");\n        System.out.println(border);\n    }\n}\n\n// ==== Main.java ====\npublic class Main {\n    public static void main(String[] args) {\n        Banner banner = new Banner(\"Sale today\");\n        banner.print();\n    }\n}\n","hints":["The border has `this.text.length() + 4` stars. Build it in a loop with `+=`, starting from the empty string `\"\"`.","Print the border, then `\"* \" + this.text + \" *\"`, then the border again."],"tests":[{"name":"Test 1","stdin":"","call":"Banner banner = new Banner(\"Sale today\");\nbanner.print();\n","expect":"**************\n* Sale today *\n**************","hidden":false},{"name":"Test 2","stdin":"","call":"Banner banner = new Banner(\"Hi\");\nbanner.print();\n","expect":"******\n* Hi *\n******","hidden":true},{"name":"Test 3","stdin":"","call":"Banner banner = new Banner(\"Welcome to the fair\");\nbanner.print();\n","expect":"***********************\n* Welcome to the fair *\n***********************","hidden":true}],"require":[],"forbid":[]}]}