{"id":"calculating-calculator","slug":"calculator","title":"A small calculator","text":"Time to put the operators together. A calculator program reads two numbers and prints one line per operation:\n\n```java\nScanner scanner = new Scanner(System.in);\nint a = Integer.valueOf(scanner.nextLine());\nint b = Integer.valueOf(scanner.nextLine());\nSystem.out.println(a + \" + \" + b + \" = \" + (a + b));\nSystem.out.println(a + \" / \" + b + \" = \" + ((double) a / b));\n```\n\n```input\n7\n2\n```\n\n```output\n7 + 2 = 9\n7 / 2 = 3.5\n```\n\nTwo details make it right:\n\n- Each calculation is in parentheses, so it happens before the joining.\n- The division casts to `double`, so `7 / 2` shows `3.5` and not `3`.\n","task":"Read two whole numbers and print their sum and difference. For the input `8` and `3` the program prints:\n\n```\n8 + 3 = 11\n8 - 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        System.out.println(a + \" + \" + b + \" = \" + (a + b));\n        System.out.println(a + \" - \" + b + \" = \" + (a - b));\n    }\n}\n","hints":["Follow the example: `a + \" + \" + b + \" = \" + (a + b)`.","The second line is the same with `-`."],"tests":[{"name":"Test 1","stdin":"8\n3\n","expect":"8 + 3 = 11\n8 - 3 = 5","hidden":false},{"name":"Test 2","stdin":"3\n8\n","expect":"3 + 8 = 11\n3 - 8 = -5","hidden":true},{"name":"Test 3","stdin":"-4\n-6\n","expect":"-4 + -6 = -10\n-4 - -6 = 2","hidden":true}],"require":[],"forbid":[],"more":[{"task":"Now all four operations, with the division as a decimal number. For the input `9` and `2` the program prints:\n\n```\n9 + 2 = 11\n9 - 2 = 7\n9 * 2 = 18\n9 / 2 = 4.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        System.out.println(a + \" + \" + b + \" = \" + (a + b));\n        System.out.println(a + \" - \" + b + \" = \" + (a - b));\n        System.out.println(a + \" * \" + b + \" = \" + (a * b));\n        System.out.println(a + \" / \" + b + \" = \" + ((double) a / b));\n    }\n}\n","hints":["Four lines, one per operation, each with its calculation in parentheses.","For the division, cast first: `((double) a / b)`."],"tests":[{"name":"Test 1","stdin":"9\n2\n","expect":"9 + 2 = 11\n9 - 2 = 7\n9 * 2 = 18\n9 / 2 = 4.5","hidden":false},{"name":"Test 2","stdin":"10\n5\n","expect":"10 + 5 = 15\n10 - 5 = 5\n10 * 5 = 50\n10 / 5 = 2.0","hidden":true},{"name":"Test 3","stdin":"1\n4\n","expect":"1 + 4 = 5\n1 - 4 = -3\n1 * 4 = 4\n1 / 4 = 0.25","hidden":true}],"require":[],"forbid":[]},{"task":"Add one more line: the first number as a percentage of the second, as a decimal number. For the input `5` and `20` the program prints:\n\n```\n5 + 20 = 25\n5 - 20 = -15\n5 * 20 = 100\n5 / 20 = 0.25\n5 is 25.0% of 20\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        System.out.println(a + \" + \" + b + \" = \" + (a + b));\n        System.out.println(a + \" - \" + b + \" = \" + (a - b));\n        System.out.println(a + \" * \" + b + \" = \" + (a * b));\n        System.out.println(a + \" / \" + b + \" = \" + ((double) a / b));\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        System.out.println(a + \" + \" + b + \" = \" + (a + b));\n        System.out.println(a + \" - \" + b + \" = \" + (a - b));\n        System.out.println(a + \" * \" + b + \" = \" + (a * b));\n        System.out.println(a + \" / \" + b + \" = \" + ((double) a / b));\n        System.out.println(a + \" is \" + (100.0 * a / b) + \"% of \" + b);\n    }\n}\n","hints":["A percentage is 100 times the quotient: `100.0 * a / b`.","Starting with `100.0` makes the whole calculation a `double`, so the decimal part stays."],"tests":[{"name":"Test 1","stdin":"5\n20\n","expect":"5 + 20 = 25\n5 - 20 = -15\n5 * 20 = 100\n5 / 20 = 0.25\n5 is 25.0% of 20","hidden":false},{"name":"Test 2","stdin":"3\n4\n","expect":"3 + 4 = 7\n3 - 4 = -1\n3 * 4 = 12\n3 / 4 = 0.75\n3 is 75.0% of 4","hidden":true},{"name":"Test 3","stdin":"30\n12\n","expect":"30 + 12 = 42\n30 - 12 = 18\n30 * 12 = 360\n30 / 12 = 2.5\n30 is 250.0% of 12","hidden":true}],"require":[],"forbid":[]}]}