{"id":"patterns-read-decide-print","slug":"read-decide-print","title":"Read, decide, print","text":"In the second pattern the input decides what the program prints. Between reading and printing there's an `if`:\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        int battery = Integer.valueOf(scanner.nextLine());\n        if (battery < 20) {\n            System.out.println(\"Charge soon\");\n        } else {\n            System.out.println(\"Battery OK\");\n        }\n    }\n}\n```\n\n```input\n12\n```\n\n```output\nCharge soon\n```\n\nA decision can also change a value instead of printing: the `if` fixes up a variable, and one print line after it shows the result. This keeps the printing in one place:\n\n```java\nint speed = 140;\nif (speed > 120) {\n    speed = 120;\n}\nSystem.out.println(\"Speed: \" + speed);\n```\n\n```output\nSpeed: 120\n```\n","task":"A weather app reads how many degrees the temperature changed since yesterday, negative when it got colder. Print the size of the change, without a minus sign. Use an `if` for it, not `Math.abs`, to practice the pattern. For the input `-7` the program prints:\n\n```\nChange of 7 degrees\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 change = 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 change = Integer.valueOf(scanner.nextLine());\n        if (change < 0) {\n            change = -change;\n        }\n        System.out.println(\"Change of \" + change + \" degrees\");\n    }\n}\n","hints":["Only a negative change needs fixing up before it's printed.","`-change` is the number with its sign flipped: `if (change < 0) { change = -change; }`"],"tests":[{"name":"Test 1","stdin":"-7\n","expect":"Change of 7 degrees","hidden":false},{"name":"Test 2","stdin":"5\n","expect":"Change of 5 degrees","hidden":true},{"name":"Test 3","stdin":"0\n","expect":"Change of 0 degrees","hidden":true}],"require":[],"forbid":[{"pattern":"Math\\.abs","message":"Solve this one with an `if`, without `Math.abs`."}],"more":[{"task":"A volume control goes from 0 to 100. Read a volume setting and keep it in that range: below 0 becomes 0, above 100 becomes 100, and anything else stays. For the input `130` the program prints:\n\n```\nVolume: 100\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 volume = 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 volume = Integer.valueOf(scanner.nextLine());\n        if (volume < 0) {\n            volume = 0;\n        }\n        if (volume > 100) {\n            volume = 100;\n        }\n        System.out.println(\"Volume: \" + volume);\n    }\n}\n","hints":["Two separate checks, each fixing the variable when it's out of range.","Print once, after both checks: `System.out.println(\"Volume: \" + volume);`"],"tests":[{"name":"Test 1","stdin":"130\n","expect":"Volume: 100","hidden":false},{"name":"Test 2","stdin":"-5\n","expect":"Volume: 0","hidden":true},{"name":"Test 3","stdin":"42\n","expect":"Volume: 42","hidden":true},{"name":"Test 4","stdin":"100\n","expect":"Volume: 100","hidden":true}],"require":[],"forbid":[]},{"task":"Read the points from two rounds of a game and print the total. Then print `Level up!` when the total is 100 or more, `Almost there` when it's 80 to 99, and `Keep going` otherwise. For the input `45` and `60` the program prints:\n\n```\nTotal: 105\nLevel up!\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 first = Integer.valueOf(scanner.nextLine());\n        int second = 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 first = Integer.valueOf(scanner.nextLine());\n        int second = Integer.valueOf(scanner.nextLine());\n        int total = first + second;\n        System.out.println(\"Total: \" + total);\n        if (total >= 100) {\n            System.out.println(\"Level up!\");\n        } else if (total >= 80) {\n            System.out.println(\"Almost there\");\n        } else {\n            System.out.println(\"Keep going\");\n        }\n    }\n}\n","hints":["Calculate the total first and print it. Then decide, using the total.","Check the highest range first: `if (total >= 100)`, then `else if (total >= 80)`, then `else`."],"tests":[{"name":"Test 1","stdin":"45\n60\n","expect":"Total: 105\nLevel up!","hidden":false},{"name":"Test 2","stdin":"40\n40\n","expect":"Total: 80\nAlmost there","hidden":true},{"name":"Test 3","stdin":"10\n20\n","expect":"Total: 30\nKeep going","hidden":true},{"name":"Test 4","stdin":"50\n49\n","expect":"Total: 99\nAlmost there","hidden":true}],"require":[],"forbid":[]}]}