{"id":"patterns-math","slug":"math-methods","title":"Math methods","text":"Java has a toolbox of ready-made calculations in `Math`. You call them with values in parentheses, and they give back a result that you can store or print:\n\n| Call | Gives back | Example |\n|---|---|---|\n| `Math.sqrt(x)` | the square root of `x`, as a `double` | `Math.sqrt(9)` is `3.0` |\n| `Math.pow(a, b)` | `a` to the power `b`, as a `double` | `Math.pow(2, 10)` is `1024.0` |\n| `Math.max(a, b)` | the larger of the two | `Math.max(3, 8)` is `8` |\n| `Math.min(a, b)` | the smaller of the two | `Math.min(3, 8)` is `3` |\n| `Math.abs(x)` | `x` without its minus sign | `Math.abs(-5)` is `5` |\n\n```java\nint a = 3;\nint b = 4;\ndouble hypotenuse = Math.sqrt(a * a + b * b);\nSystem.out.println(hypotenuse);\nSystem.out.println(Math.max(a, b));\nSystem.out.println(Math.pow(a, 3));\n```\n\n```output\n5.0\n4\n27.0\n```\n\n`Math.max` and `Math.min` keep the type they're given (two `int`s give an `int`), while `Math.sqrt` and `Math.pow` always give a `double`. A call can be part of a bigger calculation, and a call can even go inside another: `Math.max(a, Math.max(b, c))` is the largest of three.\n","task":"Read a whole number and print its square root. For the input `16` the program prints:\n\n```\n4.0\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 number = 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 number = Integer.valueOf(scanner.nextLine());\n        double root = Math.sqrt(number);\n        System.out.println(root);\n    }\n}\n","hints":["`Math.sqrt(number)` gives the square root as a `double`.","Store it in a `double` variable, or print the call directly: `System.out.println(Math.sqrt(number));`"],"tests":[{"name":"Test 1","stdin":"16\n","expect":"4.0","hidden":false},{"name":"Test 2","stdin":"2\n","expect":"1.4142135623730951","hidden":true},{"name":"Test 3","stdin":"0\n","expect":"0.0","hidden":true}],"require":[{"pattern":"Math\\.sqrt\\s*\\(","message":"Use `Math.sqrt`."}],"forbid":[],"more":[{"task":"A screen's size is measured along its diagonal. Read the width and then the height of a screen in inches (whole numbers), and print the diagonal: with `Math.sqrt`, the square root of width times width plus height times height. For the input `16` and `9` the program prints:\n\n```\nDiagonal: 18.35755975068582\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 width = Integer.valueOf(scanner.nextLine());\n        int height = 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 width = Integer.valueOf(scanner.nextLine());\n        int height = Integer.valueOf(scanner.nextLine());\n        double diagonal = Math.sqrt(width * width + height * height);\n        System.out.println(\"Diagonal: \" + diagonal);\n    }\n}\n","hints":["The calculation goes inside the parentheses of `Math.sqrt`.","`Math.sqrt(width * width + height * height)`"],"tests":[{"name":"Test 1","stdin":"16\n9\n","expect":"Diagonal: 18.35755975068582","hidden":false},{"name":"Test 2","stdin":"3\n4\n","expect":"Diagonal: 5.0","hidden":true},{"name":"Test 3","stdin":"10\n10\n","expect":"Diagonal: 14.142135623730951","hidden":true}],"require":[{"pattern":"Math\\.sqrt\\s*\\(","message":"Use `Math.sqrt`."}],"forbid":[]},{"task":"Read three test scores and print the highest and the lowest, using `Math.max` and `Math.min`. For the input `67`, `92` and `80` the program prints:\n\n```\nHighest: 92\nLowest: 67\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 third = 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 third = Integer.valueOf(scanner.nextLine());\n        int highest = Math.max(first, Math.max(second, third));\n        int lowest = Math.min(first, Math.min(second, third));\n        System.out.println(\"Highest: \" + highest);\n        System.out.println(\"Lowest: \" + lowest);\n    }\n}\n","hints":["`Math.max` takes two values. For three, put one call inside another: `Math.max(first, Math.max(second, third))`.","The lowest works the same way with `Math.min`."],"tests":[{"name":"Test 1","stdin":"67\n92\n80\n","expect":"Highest: 92\nLowest: 67","hidden":false},{"name":"Test 2","stdin":"50\n50\n50\n","expect":"Highest: 50\nLowest: 50","hidden":true},{"name":"Test 3","stdin":"99\n12\n45\n","expect":"Highest: 99\nLowest: 12","hidden":true}],"require":[{"pattern":"Math\\.max\\s*\\(","message":"Use `Math.max` for the highest."},{"pattern":"Math\\.min\\s*\\(","message":"Use `Math.min` for the lowest."}],"forbid":[]}]}