{"id":"object-methods-returning","slug":"returning-values","title":"Methods that return a value","text":"In module 17 the methods of your classes did something: they printed, or they changed the object's instance variables. They were all `void`. An object's method can also **return** a value, just like the methods of module 11. This basket counts its fruit:\n\n```java\npublic class Main {\n    public static void main(String[] args) {\n        Basket basket = new Basket();\n        System.out.println(basket.isEmpty());\n        basket.addApples(3);\n        basket.addPears(2);\n        int fruit = basket.total();\n        System.out.println(\"Fruit in the basket: \" + fruit);\n        System.out.println(basket.isEmpty());\n    }\n}\n// ==== Basket.java ====\npublic class Basket {\n    private int apples;\n    private int pears;\n\n    public Basket() {\n        this.apples = 0;\n        this.pears = 0;\n    }\n\n    public void addApples(int count) {\n        this.apples = this.apples + count;\n    }\n\n    public void addPears(int count) {\n        this.pears = this.pears + count;\n    }\n\n    public int total() {\n        return this.apples + this.pears;\n    }\n\n    public boolean isEmpty() {\n        return this.apples + this.pears == 0;\n    }\n}\n```\n\n```output\ntrue\nFruit in the basket: 5\nfalse\n```\n\n- `public int total()` returns an `int`: the apples and pears of *this* basket added up. There's no `static` in the header, because the method belongs to an object and works with that object's own variables.\n- In `main`, the call `basket.total()` stands for the value it returns. Here `main` stores it in `fruit`, but the call can go anywhere a value can: in a print line, a calculation or a comparison.\n- `isEmpty()` returns a `boolean`, so its call works as a condition on its own: `if (basket.isEmpty()) { ... }`. A method that answers a yes-or-no question usually has a name that starts with `is` or `has`, such as `isEmpty` or `hasRoom`.\n\nYou've used methods like these on lists and strings: `list.size()` returns how many values a list has, and `word.isEmpty()` tells whether a string is empty. Now your own classes can have them too.\n\nWhy return the value instead of printing it? For the same reason as in module 11: the caller decides what to do with it. `main` can compare the totals of two baskets, add them up, or print them in its own words.\n","task":"The `Scoreboard` keeps the points of a home team and an away team. Add a method `totalPoints()` that returns the points of both teams together. The check gives the home team 3 and 1 points and the away team 2, and prints what `totalPoints()` returns:\n\n```\n6\n```\n","kind":"code","seed":"// ==== Scoreboard.java ====\npublic class Scoreboard {\n    private int home;\n    private int away;\n\n    public Scoreboard() {\n        this.home = 0;\n        this.away = 0;\n    }\n\n    public void homeScores(int points) {\n        this.home = this.home + points;\n    }\n\n    public void awayScores(int points) {\n        this.away = this.away + points;\n    }\n\n    // Write the totalPoints method here.\n}\n\n// ==== Main.java ====\npublic class Main {\n    public static void main(String[] args) {\n        Scoreboard game = new Scoreboard();\n        game.homeScores(3);\n        game.awayScores(2);\n        game.homeScores(1);\n        System.out.println(\"Points so far: \" + game.totalPoints());\n    }\n}\n","solution":"// ==== Scoreboard.java ====\npublic class Scoreboard {\n    private int home;\n    private int away;\n\n    public Scoreboard() {\n        this.home = 0;\n        this.away = 0;\n    }\n\n    public void homeScores(int points) {\n        this.home = this.home + points;\n    }\n\n    public void awayScores(int points) {\n        this.away = this.away + points;\n    }\n\n    public int totalPoints() {\n        return this.home + this.away;\n    }\n}\n\n// ==== Main.java ====\npublic class Main {\n    public static void main(String[] args) {\n        Scoreboard game = new Scoreboard();\n        game.homeScores(3);\n        game.awayScores(2);\n        game.homeScores(1);\n        System.out.println(\"Points so far: \" + game.totalPoints());\n    }\n}\n","hints":["The method returns an `int` and has no parameters: `public int totalPoints() {`. It goes inside the class, next to the other methods.","Its only line: `return this.home + this.away;`"],"tests":[{"name":"Test 1","stdin":"","call":"Scoreboard game = new Scoreboard();\ngame.homeScores(3);\ngame.awayScores(2);\ngame.homeScores(1);\nSystem.out.println(game.totalPoints());\n","expect":"6","hidden":false},{"name":"Test 2","stdin":"","call":"Scoreboard game = new Scoreboard();\nSystem.out.println(game.totalPoints());\n","expect":"0","hidden":true},{"name":"Test 3","stdin":"","call":"Scoreboard first = new Scoreboard();\nScoreboard second = new Scoreboard();\nfirst.awayScores(7);\nfirst.awayScores(3);\nsecond.homeScores(2);\nSystem.out.println(first.totalPoints());\nSystem.out.println(second.totalPoints());\n","expect":"10\n2","hidden":true}],"require":[],"forbid":[],"more":[{"task":"Add a method `leader()` to the `Scoreboard`. It returns the String `home` when the home team has more points, `away` when the away team has more, and `tie` when they have the same. The check asks at the start, after 2 points for the away team, and after 3 more for the home team:\n\n```\ntie\naway\nhome\n```\n","kind":"code","seed":"// ==== Scoreboard.java ====\npublic class Scoreboard {\n    private int home;\n    private int away;\n\n    public Scoreboard() {\n        this.home = 0;\n        this.away = 0;\n    }\n\n    public void homeScores(int points) {\n        this.home = this.home + points;\n    }\n\n    public void awayScores(int points) {\n        this.away = this.away + points;\n    }\n\n    public int totalPoints() {\n        return this.home + this.away;\n    }\n\n    // Write the leader method here.\n}\n\n// ==== Main.java ====\npublic class Main {\n    public static void main(String[] args) {\n        Scoreboard game = new Scoreboard();\n        game.awayScores(2);\n        System.out.println(\"Leading: \" + game.leader());\n    }\n}\n","solution":"// ==== Scoreboard.java ====\npublic class Scoreboard {\n    private int home;\n    private int away;\n\n    public Scoreboard() {\n        this.home = 0;\n        this.away = 0;\n    }\n\n    public void homeScores(int points) {\n        this.home = this.home + points;\n    }\n\n    public void awayScores(int points) {\n        this.away = this.away + points;\n    }\n\n    public int totalPoints() {\n        return this.home + this.away;\n    }\n\n    public String leader() {\n        if (this.home > this.away) {\n            return \"home\";\n        }\n        if (this.away > this.home) {\n            return \"away\";\n        }\n        return \"tie\";\n    }\n}\n\n// ==== Main.java ====\npublic class Main {\n    public static void main(String[] args) {\n        Scoreboard game = new Scoreboard();\n        game.awayScores(2);\n        System.out.println(\"Leading: \" + game.leader());\n    }\n}\n","hints":["The method returns a String: `public String leader() {`.","Returning from branches, as in module 11: `if (this.home > this.away) { return \"home\"; }`, the same for the away team, and `return \"tie\";` last."],"tests":[{"name":"Test 1","stdin":"","call":"Scoreboard game = new Scoreboard();\nSystem.out.println(game.leader());\ngame.awayScores(2);\nSystem.out.println(game.leader());\ngame.homeScores(3);\nSystem.out.println(game.leader());\n","expect":"tie\naway\nhome","hidden":false},{"name":"Test 2","stdin":"","call":"Scoreboard game = new Scoreboard();\ngame.homeScores(5);\ngame.awayScores(5);\nSystem.out.println(game.leader());\ngame.awayScores(1);\nSystem.out.println(game.leader());\n","expect":"tie\naway","hidden":true},{"name":"Test 3","stdin":"","call":"Scoreboard game = new Scoreboard();\ngame.homeScores(1);\ngame.awayScores(4);\ngame.homeScores(4);\nSystem.out.println(game.leader());\n","expect":"home","hidden":true}],"require":[],"forbid":[]},{"task":"What does this program print? The water is in milliliters, and a cup is 250 of them.\n","kind":"predict","seed":"public class Main {\n    public static void main(String[] args) {\n        Kettle kettle = new Kettle(1000);\n        kettle.cupsLeft();\n        System.out.println(kettle.cupsLeft());\n        kettle.pour(300);\n        int cups = kettle.cupsLeft();\n        System.out.println(cups + \" cups\");\n        kettle.pour(700);\n        System.out.println(kettle.isEmpty());\n    }\n}\n// ==== Kettle.java ====\npublic class Kettle {\n    private int water;\n\n    public Kettle(int water) {\n        this.water = water;\n    }\n\n    public void pour(int amount) {\n        this.water = this.water - amount;\n    }\n\n    public int cupsLeft() {\n        return this.water / 250;\n    }\n\n    public boolean isEmpty() {\n        return this.water == 0;\n    }\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        Kettle kettle = new Kettle(1000);\n        kettle.cupsLeft();\n        System.out.println(kettle.cupsLeft());\n        kettle.pour(300);\n        int cups = kettle.cupsLeft();\n        System.out.println(cups + \" cups\");\n        kettle.pour(700);\n        System.out.println(kettle.isEmpty());\n    }\n}\n// ==== Kettle.java ====\npublic class Kettle {\n    private int water;\n\n    public Kettle(int water) {\n        this.water = water;\n    }\n\n    public void pour(int amount) {\n        this.water = this.water - amount;\n    }\n\n    public int cupsLeft() {\n        return this.water / 250;\n    }\n\n    public boolean isEmpty() {\n        return this.water == 0;\n    }\n}\n","hints":["`kettle.cupsLeft();` on a line of its own returns 4, but nothing uses the value, so nothing is printed. The next line prints it.","After pouring 300 ml, 700 ml is left, and `700 / 250` is 2 in integer division. After pouring the other 700 ml, the water is 0."],"tests":[{"name":"Output","stdin":"","expect":"4\n2 cups\ntrue","hidden":false}],"require":[],"forbid":[],"lines":["4","2 cups","true"]}]}