{"id":"calculating-arithmetic","slug":"arithmetic","title":"Arithmetic and precedence","text":"Java calculates with the usual operators: `+` adds, `-` subtracts, `*` multiplies and `/` divides. A calculation can use numbers, variables, or both, and its result can be stored in a variable:\n\n```java\nint minutes = 3 * 60;\nint total = minutes + 45;\nSystem.out.println(total);\n```\n\n```output\n225\n```\n\n### Which part first\n\nAs in maths, `*` and `/` are done before `+` and `-`. Parentheses change the order: whatever is inside them is calculated first.\n\n```java\nSystem.out.println(2 + 3 * 4);\nSystem.out.println((2 + 3) * 4);\n```\n\n```output\n14\n20\n```\n\nA piece of code that has a value, like `2 + 3 * 4` or `minutes + 45`, is called an **expression**. A statement such as `int total = minutes + 45;` calculates the expression and does something with its value, here storing it.\n","task":"Add parentheses in the two blanks, so the program prints `20`.\n","kind":"fill","seed":"public class Main {\n    public static void main(String[] args) {\n        int result = [[(]]2 + 3[[)]] * 4;\n        System.out.println(result);\n    }\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        int result = (2 + 3) * 4;\n        System.out.println(result);\n    }\n}\n","hints":["Without parentheses, `3 * 4` is calculated first and the result is 14.","The addition has to happen first: `(2 + 3) * 4`."],"tests":[{"name":"Output","stdin":"","expect":"20","hidden":false}],"require":[],"forbid":[],"more":[{"task":"Read a number of hours and print how many minutes that is. For the input `3` the program prints:\n\n```\nHow many hours?\nThat is 180 minutes.\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        System.out.println(\"How many hours?\");\n        int hours = Integer.valueOf(scanner.nextLine());\n        int minutes = hours * 60;\n        System.out.println(\"That is \" + minutes + \" minutes.\");\n    }\n}\n","hints":["Read the number with `Integer.valueOf(scanner.nextLine())`.","An hour has 60 minutes: `int minutes = hours * 60;`"],"tests":[{"name":"Test 1","stdin":"3\n","expect":"How many hours?\nThat is 180 minutes.","hidden":false},{"name":"Test 2","stdin":"0\n","expect":"How many hours?\nThat is 0 minutes.","hidden":true},{"name":"Test 3","stdin":"24\n","expect":"How many hours?\nThat is 1440 minutes.","hidden":true}],"require":[],"forbid":[]},{"task":"Read a number of weeks and print how many seconds they last: 7 days a week, 24 hours a day, 60 minutes an hour and 60 seconds a minute. For the input `1` the program prints:\n\n```\nWeeks: 1\nSeconds: 604800\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 weeks = 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 weeks = Integer.valueOf(scanner.nextLine());\n        int seconds = weeks * 7 * 24 * 60 * 60;\n        System.out.println(\"Weeks: \" + weeks);\n        System.out.println(\"Seconds: \" + seconds);\n    }\n}\n","hints":["Multiply everything together: `weeks * 7 * 24 * 60 * 60`.","Print the two labelled lines."],"tests":[{"name":"Test 1","stdin":"1\n","expect":"Weeks: 1\nSeconds: 604800","hidden":false},{"name":"Test 2","stdin":"2\n","expect":"Weeks: 2\nSeconds: 1209600","hidden":true},{"name":"Test 3","stdin":"52\n","expect":"Weeks: 52\nSeconds: 31449600","hidden":true}],"require":[],"forbid":[]}]}