Java Arena
Step 2 of 8

Computing the return value

The returned value is usually calculated, often from the parameters:

public class Main {
    public static void main(String[] args) {
        int area = rectangleArea(4, 3);
        System.out.println("Area: " + area);
        System.out.println("Two rooms: " + (rectangleArea(4, 3) + rectangleArea(5, 2)));
    }

    public static int rectangleArea(int width, int height) {
        int area = width * height;
        return area;
    }
}
Output
Area: 12
Two rooms: 22

return can give back any expression of the right type: return width * height; would do the same without the variable.

A 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.

The 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.

Your task

Challenge 1 of 3

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:

14

Challenge 2

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:

100.0

Challenge 3

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:

3723