{"id":"methods-several-parameters","slug":"several-parameters","title":"Several parameters","text":"A method can have several parameters, separated by commas, each with its own type. The arguments in a call go to the parameters in order: the first to the first, the second to the second.\n\n```java\npublic class Main {\n    public static void main(String[] args) {\n        printSum(3, 4);\n        printSum(10, -2);\n    }\n\n    public static void printSum(int first, int second) {\n        System.out.println(first + \" + \" + second + \" = \" + (first + second));\n    }\n}\n```\n\n```output\n3 + 4 = 7\n10 + -2 = 8\n```\n\nA call must give exactly as many arguments as there are parameters, and of fitting types. With one argument missing, it doesn't compile:\n\n```java\npublic class Main {\n    public static void main(String[] args) {\n        printSum(3);\n    }\n\n    public static void printSum(int first, int second) {\n        System.out.println(first + \" + \" + second + \" = \" + (first + second));\n    }\n}\n```\n\n```javac\nMain.java:3: error: method printSum in class Main cannot be applied to given types;\n        printSum(3);\n        ^\n  required: int,int\n  found:    int\n  reason: actual and formal argument lists differ in length\n1 error\n```\n\n`required` is what the method's parameters need, and `found` is what the call gave. The parameters can have different types too: `printPrice(String item, double price)` is called as `printPrice(\"Tea\", 2.5)`.\n","task":"Write a method `printProduct(int a, int b)` that prints the calculation and its result. The check calls `printProduct(3, 4);` and expects:\n\n```\n3 * 4 = 12\n```\n","kind":"code","seed":"public class Main {\n    public static void main(String[] args) {\n        printProduct(3, 4);\n    }\n\n    // Write the printProduct method here.\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        printProduct(3, 4);\n    }\n\n    public static void printProduct(int a, int b) {\n        System.out.println(a + \" * \" + b + \" = \" + (a * b));\n    }\n}\n","hints":["The method starts `public static void printProduct(int a, int b) {`.","Put the multiplication in parentheses inside the print line: `(a * b)`."],"tests":[{"name":"printProduct(3, 4);","stdin":"","call":"printProduct(3, 4);\n","expect":"3 * 4 = 12","hidden":false},{"name":"printProduct(-2, 5);","stdin":"","call":"printProduct(-2, 5);\n","expect":"-2 * 5 = -10","hidden":true},{"name":"printProduct(0, 9);","stdin":"","call":"printProduct(0, 9);\n","expect":"0 * 9 = 0","hidden":true}],"require":[],"forbid":[],"more":[{"task":"Write a method `printAverage(int a, int b)` that prints the average of its two parameters as a decimal number. The check calls `printAverage(3, 4);` and expects:\n\n```\nThe average is 3.5\n```\n","kind":"code","seed":"public class Main {\n    public static void main(String[] args) {\n        printAverage(3, 4);\n    }\n\n    // Write the printAverage method here.\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        printAverage(3, 4);\n    }\n\n    public static void printAverage(int a, int b) {\n        double average = (a + b) / 2.0;\n        System.out.println(\"The average is \" + average);\n    }\n}\n","hints":["Dividing two `int`s drops the decimals. Divide by `2.0` instead of `2`.","`double average = (a + b) / 2.0;`"],"tests":[{"name":"printAverage(3, 4);","stdin":"","call":"printAverage(3, 4);\n","expect":"The average is 3.5","hidden":false},{"name":"printAverage(10, 10);","stdin":"","call":"printAverage(10, 10);\n","expect":"The average is 10.0","hidden":true},{"name":"printAverage(-3, 2);","stdin":"","call":"printAverage(-3, 2);\n","expect":"The average is -0.5","hidden":true}],"require":[],"forbid":[]},{"task":"Write a method `printEvery(int start, int end, int step)` that prints `start`, then every `step`-th number after it, as long as the number is at most `end`. The check calls `printEvery(3, 10, 3);` and expects:\n\n```\n3\n6\n9\n```\n","kind":"code","seed":"public class Main {\n    public static void main(String[] args) {\n        printEvery(3, 10, 3);\n    }\n\n    // Write the printEvery method here.\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        printEvery(3, 10, 3);\n    }\n\n    public static void printEvery(int start, int end, int step) {\n        for (int i = start; i <= end; i = i + step) {\n            System.out.println(i);\n        }\n    }\n}\n","hints":["Three parameters, all `int`: `(int start, int end, int step)`.","A loop that adds `step` each round: `for (int i = start; i <= end; i = i + step)`."],"tests":[{"name":"printEvery(3, 10, 3);","stdin":"","call":"printEvery(3, 10, 3);\n","expect":"3\n6\n9","hidden":false},{"name":"printEvery(0, 20, 5);","stdin":"","call":"printEvery(0, 20, 5);\n","expect":"0\n5\n10\n15\n20","hidden":true},{"name":"printEvery(7, 7, 4);","stdin":"","call":"printEvery(7, 7, 4);\n","expect":"7","hidden":true},{"name":"printEvery(1, 10, 4);","stdin":"","call":"printEvery(1, 10, 4);\n","expect":"1\n5\n9","hidden":true}],"require":[],"forbid":[]}]}