{"id":"calculating-formulas","slug":"printing-formulas","title":"Numbers and text together","text":"`+` has two jobs: it adds numbers, and it joins text. Java decides which one each `+` does, working **from left to right**. As soon as one side is a String, the `+` joins, and the result is a String again.\n\n```java\nint a = 3;\nint b = 4;\nSystem.out.println(\"Total: \" + a + b);\nSystem.out.println(\"Total: \" + (a + b));\nSystem.out.println(a + b + \" is the total\");\n```\n\n```output\nTotal: 34\nTotal: 7\n7 is the total\n```\n\n- In the first line, `\"Total: \" + a` is text, `Total: 3`, and adding `b` to text joins it: `Total: 34`.\n- In the second line the parentheses make `a + b` a calculation first.\n- In the third line the two numbers come first, so they're added before any text appears.\n\n### Numbers have limits\n\nAn `int` can hold values up to 2147483647. A calculation that goes past the limit doesn't stop with an error: it wraps around to the negative end of the range and counts on from there, which is called **overflow**. One past the limit is the most negative `int`:\n\n```java\nint big = 2147483647;\nbig = big + 1;\nSystem.out.println(big);\n```\n\n```output\n-2147483648\n```\n\nThe numbers in this course stay far from that limit, but it's worth knowing that it exists.\n","task":"This program should print `Total: 12` for the input `5` and `7`, but it prints `Total: 57`. Fix it.\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(\"Total: \" + 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(\"Total: \" + (a + b));\n    }\n}\n","hints":["Going from left to right, `\"Total: \" + a` is already text, so `+ b` joins instead of adding.","Put parentheses around the calculation, so it's done first."],"tests":[{"name":"Test 1","stdin":"5\n7\n","expect":"Total: 12","hidden":false},{"name":"Test 2","stdin":"10\n20\n","expect":"Total: 30","hidden":true},{"name":"Test 3","stdin":"0\n9\n","expect":"Total: 9","hidden":true}],"require":[],"forbid":[],"more":[{"task":"Read two whole numbers and print the whole calculation. For the input `3` and `4` the program prints:\n\n```\n3 + 4 = 7\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    }\n}\n","hints":["The line is built from five pieces: `a`, `\" + \"`, `b`, `\" = \"` and the sum.","Put the sum in parentheses: `(a + b)`."],"tests":[{"name":"Test 1","stdin":"3\n4\n","expect":"3 + 4 = 7","hidden":false},{"name":"Test 2","stdin":"12\n30\n","expect":"12 + 30 = 42","hidden":true},{"name":"Test 3","stdin":"-2\n2\n","expect":"-2 + 2 = 0","hidden":true}],"require":[],"forbid":[]},{"task":"Same idea with multiplication. For the input `6` and `7` the program prints:\n\n```\n6 * 7 = 42\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    }\n}\n","hints":["The operator in the text is the character `*` inside quotes: `\" * \"`.","The product goes in parentheses at the end: `(a * b)`."],"tests":[{"name":"Test 1","stdin":"6\n7\n","expect":"6 * 7 = 42","hidden":false},{"name":"Test 2","stdin":"100\n0\n","expect":"100 * 0 = 0","hidden":true},{"name":"Test 3","stdin":"-3\n11\n","expect":"-3 * 11 = -33","hidden":true}],"require":[],"forbid":[]}]}