{"id":"patterns-read-calculate-print","slug":"read-calculate-print","title":"Read, calculate, print","text":"Many programs have the same three parts, whatever they're about:\n\n1. **Read** the input into variables.\n2. **Calculate** the result from them.\n3. **Print** the result.\n\nHere the input is a temperature in Celsius, and the result is the same temperature in Fahrenheit:\n\n```java\nimport java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        // 1. Read\n        int celsius = Integer.valueOf(scanner.nextLine());\n        // 2. Calculate\n        double fahrenheit = celsius * 9 / 5.0 + 32;\n        // 3. Print\n        System.out.println(celsius + \" C is \" + fahrenheit + \" F\");\n    }\n}\n```\n\n```input\n25\n```\n\n```output\n25 C is 77.0 F\n```\n\nWhen a new problem looks hard, start by naming the three parts: what does the program get, what does it work out, and what does it show? Each part is often just a line or two.\n\nKeeping the calculation in its own variable (`fahrenheit`) makes the print line short and gives the result a name.\n","task":"Read the side of a square garden in metres and print its area. For the input `7` the program prints:\n\n```\nArea: 49\n```\n","kind":"code","seed":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n\n    }\n}\n","solution":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int side = Integer.valueOf(scanner.nextLine());\n        int area = side * side;\n        System.out.println(\"Area: \" + area);\n    }\n}\n","hints":["Read: `int side = Integer.valueOf(scanner.nextLine());`","Calculate: the area of a square is the side times itself. Then print it with its label."],"tests":[{"name":"Test 1","stdin":"7\n","expect":"Area: 49","hidden":false},{"name":"Test 2","stdin":"1\n","expect":"Area: 1","hidden":true},{"name":"Test 3","stdin":"12\n","expect":"Area: 144","hidden":true}],"require":[],"forbid":[],"more":[{"task":"Read the width and then the height of a rectangle, and print its area and its perimeter (the length of all four sides together). For the input `4` and `3` the program prints:\n\n```\nArea: 12\nPerimeter: 14\n```\n","kind":"code","seed":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n\n    }\n}\n","solution":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int width = Integer.valueOf(scanner.nextLine());\n        int height = Integer.valueOf(scanner.nextLine());\n        int area = width * height;\n        int perimeter = 2 * width + 2 * height;\n        System.out.println(\"Area: \" + area);\n        System.out.println(\"Perimeter: \" + perimeter);\n    }\n}\n","hints":["Two reads, two calculations, two prints.","The perimeter is two widths and two heights: `2 * width + 2 * height`."],"tests":[{"name":"Test 1","stdin":"4\n3\n","expect":"Area: 12\nPerimeter: 14","hidden":false},{"name":"Test 2","stdin":"10\n1\n","expect":"Area: 10\nPerimeter: 22","hidden":true},{"name":"Test 3","stdin":"6\n6\n","expect":"Area: 36\nPerimeter: 24","hidden":true}],"require":[],"forbid":[]},{"task":"Read a length of time in minutes and print it in hours and minutes. For the input `135` the program prints:\n\n```\n2 h 15 min\n```\n","kind":"code","seed":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int minutes = Integer.valueOf(scanner.nextLine());\n\n    }\n}\n","solution":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int minutes = Integer.valueOf(scanner.nextLine());\n        int hours = minutes / 60;\n        int rest = minutes % 60;\n        System.out.println(hours + \" h \" + rest + \" min\");\n    }\n}\n","hints":["Dividing two `int`s drops the remainder: `minutes / 60` is the whole hours.","The remainder operator gives the minutes left over: `minutes % 60`."],"tests":[{"name":"Test 1","stdin":"135\n","expect":"2 h 15 min","hidden":false},{"name":"Test 2","stdin":"45\n","expect":"0 h 45 min","hidden":true},{"name":"Test 3","stdin":"180\n","expect":"3 h 0 min","hidden":true}],"require":[],"forbid":[]}]}