{"id":"calculating-division","slug":"division","title":"Division and casting","text":"Dividing two `int` values gives an `int`: the part after the decimal point is simply dropped, not rounded.\n\n```java\nSystem.out.println(7 / 2);\nSystem.out.println(9 / 10);\nSystem.out.println(7.0 / 2);\n```\n\n```output\n3\n0\n3.5\n```\n\nAs soon as one of the two numbers is a `double`, like `7.0`, the division gives a `double`. With variables, turn an `int` into a `double` with a **cast**: `(double)` in front of it.\n\n```java\nint sum = 7;\nint count = 2;\ndouble average = (double) sum / count;\nSystem.out.println(average);\n```\n\n```output\n3.5\n```\n\nWhere the cast goes matters. `(double) sum / count` turns `sum` into `7.0` first and then divides, giving `3.5`. `(double) (sum / count)` divides the two `int` values first, giving `3`, and then turns that into `3.0`.\n","task":"Put a cast in the blank, so the program prints the exact average, `2.5`.\n","kind":"fill","seed":"public class Main {\n    public static void main(String[] args) {\n        int sum = 5;\n        int count = 2;\n        double average = [[(double)‖1.0 *]] sum / count;\n        System.out.println(average);\n    }\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        int sum = 5;\n        int count = 2;\n        double average = (double) sum / count;\n        System.out.println(average);\n    }\n}\n","hints":["Without a cast, `5 / 2` divides two `int` values and gives `2`.","Write `(double)` in the blank, so `sum` becomes a `double` before the division."],"tests":[{"name":"Output","stdin":"","expect":"2.5","hidden":false}],"require":[],"forbid":[],"more":[{"task":"Read two whole numbers and print their average as a decimal number. For the input `3` and `4` the program prints:\n\n```\nThe average is 3.5\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 a = Integer.valueOf(scanner.nextLine());\n        int b = 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 a = Integer.valueOf(scanner.nextLine());\n        int b = Integer.valueOf(scanner.nextLine());\n        double average = (double) (a + b) / 2;\n        System.out.println(\"The average is \" + average);\n    }\n}\n","hints":["The average is the sum divided by how many numbers there are.","Cast before dividing: `(double) (a + b) / 2`. The parentheses make the sum happen first."],"tests":[{"name":"Test 1","stdin":"3\n4\n","expect":"The average is 3.5","hidden":false},{"name":"Test 2","stdin":"2\n2\n","expect":"The average is 2.0","hidden":true},{"name":"Test 3","stdin":"5\n8\n","expect":"The average is 6.5","hidden":true}],"require":[],"forbid":[]},{"task":"Read three whole numbers and print their average. For the input `1`, `2` and `3` the program prints:\n\n```\nThe average is 2.0\n```\n\nA hidden test gives an average that doesn't come out even: it's printed with all the digits a `double` has.\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 a = Integer.valueOf(scanner.nextLine());\n        int b = Integer.valueOf(scanner.nextLine());\n        int c = Integer.valueOf(scanner.nextLine());\n        double average = (double) (a + b + c) / 3;\n        System.out.println(\"The average is \" + average);\n    }\n}\n","hints":["Read three numbers, add them, and divide by 3.","`(double) (a + b + c) / 3` keeps the decimal part."],"tests":[{"name":"Test 1","stdin":"1\n2\n3\n","expect":"The average is 2.0","hidden":false},{"name":"Test 2","stdin":"1\n1\n2\n","expect":"The average is 1.3333333333333333","hidden":true},{"name":"Test 3","stdin":"10\n-4\n0\n","expect":"The average is 2.0","hidden":true}],"require":[],"forbid":[]}]}