{"id":"return-values-computing","slug":"computing-return-values","title":"Computing the return value","text":"The returned value is usually calculated, often from the parameters:\n\n```java\npublic class Main {\n    public static void main(String[] args) {\n        int area = rectangleArea(4, 3);\n        System.out.println(\"Area: \" + area);\n        System.out.println(\"Two rooms: \" + (rectangleArea(4, 3) + rectangleArea(5, 2)));\n    }\n\n    public static int rectangleArea(int width, int height) {\n        int area = width * height;\n        return area;\n    }\n}\n```\n\n```output\nArea: 12\nTwo rooms: 22\n```\n\n`return` can give back any expression of the right type: `return width * height;` would do the same without the variable.\n\nA method that returns its result, instead of printing it, is more useful: the caller decides what to do with the value, whether that's printing it, adding it to something or comparing it. `rectangleArea` works for one room, for two, or in a loop over a whole house.\n\nThe checks in this step print what your method returns. If the method also prints something itself, that extra output makes the check fail, so leave the printing to the caller.\n","task":"Write a method `perimeter(int width, int height)` that returns the perimeter of a rectangle: two widths plus two heights. The check runs `System.out.println(perimeter(4, 3));` and expects:\n\n```\n14\n```\n","kind":"code","seed":"public class Main {\n    public static void main(String[] args) {\n        System.out.println(perimeter(4, 3));\n    }\n\n    // Write the perimeter method here.\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        System.out.println(perimeter(4, 3));\n    }\n\n    public static int perimeter(int width, int height) {\n        return 2 * width + 2 * height;\n    }\n}\n","hints":["`public static int perimeter(int width, int height) {`","`return 2 * width + 2 * height;`"],"tests":[{"name":"System.out.println(perimeter(4, 3));","stdin":"","call":"System.out.println(perimeter(4, 3));\n","expect":"14","hidden":false},{"name":"System.out.println(perimeter(1, 1));","stdin":"","call":"System.out.println(perimeter(1, 1));\n","expect":"4","hidden":true},{"name":"System.out.println(perimeter(10, 0));","stdin":"","call":"System.out.println(perimeter(10, 0));\n","expect":"20","hidden":true}],"require":[],"forbid":[],"more":[{"task":"Write a method `celsius(double fahrenheit)` that returns the temperature in Celsius: (fahrenheit - 32) * 5 / 9, as a `double`. The check runs `System.out.println(celsius(212));` and expects:\n\n```\n100.0\n```\n","kind":"code","seed":"public class Main {\n    public static void main(String[] args) {\n        System.out.println(celsius(212));\n    }\n\n    // Write the celsius method here.\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        System.out.println(celsius(212));\n    }\n\n    public static double celsius(double fahrenheit) {\n        return (fahrenheit - 32) * 5 / 9;\n    }\n}\n","hints":["Both the parameter and the return type are `double`.","`return (fahrenheit - 32) * 5 / 9;` (the parentheses make the subtraction happen first)"],"tests":[{"name":"System.out.println(celsius(212));","stdin":"","call":"System.out.println(celsius(212));\n","expect":"100.0","hidden":false},{"name":"System.out.println(celsius(32));","stdin":"","call":"System.out.println(celsius(32));\n","expect":"0.0","hidden":true},{"name":"System.out.println(celsius(-40));","stdin":"","call":"System.out.println(celsius(-40));\n","expect":"-40.0","hidden":true},{"name":"System.out.println(celsius(100));","stdin":"","call":"System.out.println(celsius(100));\n","expect":"37.77777777777778","hidden":true}],"require":[],"forbid":[]},{"task":"Write a method `secondsOf(int hours, int minutes, int seconds)` that returns the whole length of time in seconds. The check runs `System.out.println(secondsOf(1, 2, 3));` and expects:\n\n```\n3723\n```\n","kind":"code","seed":"public class Main {\n    public static void main(String[] args) {\n        System.out.println(secondsOf(1, 2, 3));\n    }\n\n    // Write the secondsOf method here.\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        System.out.println(secondsOf(1, 2, 3));\n    }\n\n    public static int secondsOf(int hours, int minutes, int seconds) {\n        return hours * 3600 + minutes * 60 + seconds;\n    }\n}\n","hints":["An hour has 3600 seconds and a minute has 60.","`return hours * 3600 + minutes * 60 + seconds;`"],"tests":[{"name":"System.out.println(secondsOf(1, 2, 3));","stdin":"","call":"System.out.println(secondsOf(1, 2, 3));\n","expect":"3723","hidden":false},{"name":"System.out.println(secondsOf(0, 0, 59));","stdin":"","call":"System.out.println(secondsOf(0, 0, 59));\n","expect":"59","hidden":true},{"name":"System.out.println(secondsOf(2, 0, 0));","stdin":"","call":"System.out.println(secondsOf(2, 0, 0));\n","expect":"7200","hidden":true}],"require":[],"forbid":[]}]}