{"id":"patterns-combining","slug":"combining-patterns","title":"Combining the patterns","text":"Bigger programs combine the patterns: read several values, calculate from them, decide, and print a sentence that uses the results. Here are two prices, and the program says which is cheaper and by how much:\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 shopA = Integer.valueOf(scanner.nextLine());\n        int shopB = Integer.valueOf(scanner.nextLine());\n        int difference = Math.abs(shopA - shopB);\n        if (shopA < shopB) {\n            System.out.println(\"Shop A is cheaper by \" + difference);\n        } else if (shopB < shopA) {\n            System.out.println(\"Shop B is cheaper by \" + difference);\n        } else {\n            System.out.println(\"Same price\");\n        }\n    }\n}\n```\n\n```input\n35\n29\n```\n\n```output\nShop B is cheaper by 6\n```\n\nThe difference is worked out once, before the decision, and each branch uses it. The same plan works for most problems of this kind: read everything, calculate what the sentences need, then decide which sentence to print.\n","task":"Read the heights of two plants in centimetres. Print `The first plant is taller by X cm`, `The second plant is taller by X cm` or `The plants are equally tall`. For the input `30` and `25` the program prints:\n\n```\nThe first plant is taller by 5 cm\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 difference = Math.abs(first - second);\n        if (first > second) {\n            System.out.println(\"The first plant is taller by \" + difference + \" cm\");\n        } else if (second > first) {\n            System.out.println(\"The second plant is taller by \" + difference + \" cm\");\n        } else {\n            System.out.println(\"The plants are equally tall\");\n        }\n    }\n}\n","hints":["Work out the difference first: `Math.abs(first - second)` is the same whichever plant is taller.","Three cases, like the shop example."],"tests":[{"name":"Test 1","stdin":"30\n25\n","expect":"The first plant is taller by 5 cm","hidden":false},{"name":"Test 2","stdin":"12\n40\n","expect":"The second plant is taller by 28 cm","hidden":true},{"name":"Test 3","stdin":"18\n18\n","expect":"The plants are equally tall","hidden":true}],"require":[],"forbid":[],"more":[{"task":"Read the prices of two items and then how much money you have. Print `You can buy both` when the money covers both, and otherwise `You need X more`. For the input `12`, `15` and `20` the program prints:\n\n```\nYou need 7 more\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        int money = 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 money = Integer.valueOf(scanner.nextLine());\n        int total = first + second;\n        if (total <= money) {\n            System.out.println(\"You can buy both\");\n        } else {\n            System.out.println(\"You need \" + (total - money) + \" more\");\n        }\n    }\n}\n","hints":["Calculate the total price first.","Exactly enough money is enough: `if (total <= money)`. Otherwise the missing amount is `total - money`."],"tests":[{"name":"Test 1","stdin":"12\n15\n20\n","expect":"You need 7 more","hidden":false},{"name":"Test 2","stdin":"10\n10\n20\n","expect":"You can buy both","hidden":true},{"name":"Test 3","stdin":"5\n5\n100\n","expect":"You can buy both","hidden":true}],"require":[],"forbid":[]},{"task":"Alex and Sam each took two tests. Read Alex's two scores, then Sam's two, and print both averages and who has the higher one (or `Same average`). For the input `80`, `90`, `85` and `86` the program prints:\n\n```\nAlex: 85.0\nSam: 85.5\nSam has the higher average\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 alex1 = Integer.valueOf(scanner.nextLine());\n        int alex2 = Integer.valueOf(scanner.nextLine());\n        int sam1 = Integer.valueOf(scanner.nextLine());\n        int sam2 = 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 alex1 = Integer.valueOf(scanner.nextLine());\n        int alex2 = Integer.valueOf(scanner.nextLine());\n        int sam1 = Integer.valueOf(scanner.nextLine());\n        int sam2 = Integer.valueOf(scanner.nextLine());\n        double alex = (alex1 + alex2) / 2.0;\n        double sam = (sam1 + sam2) / 2.0;\n        System.out.println(\"Alex: \" + alex);\n        System.out.println(\"Sam: \" + sam);\n        if (alex > sam) {\n            System.out.println(\"Alex has the higher average\");\n        } else if (sam > alex) {\n            System.out.println(\"Sam has the higher average\");\n        } else {\n            System.out.println(\"Same average\");\n        }\n    }\n}\n","hints":["Divide by `2.0`, not `2`, so the average keeps its decimals.","Calculate and print both averages, then compare the two averages."],"tests":[{"name":"Test 1","stdin":"80\n90\n85\n86\n","expect":"Alex: 85.0\nSam: 85.5\nSam has the higher average","hidden":false},{"name":"Test 2","stdin":"70\n71\n60\n80\n","expect":"Alex: 70.5\nSam: 70.0\nAlex has the higher average","hidden":true},{"name":"Test 3","stdin":"90\n90\n100\n80\n","expect":"Alex: 90.0\nSam: 90.0\nSame average","hidden":true}],"require":[],"forbid":[]}]}