Java Arena
Step 4 of 5

Several parameters

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.

public class Main {
    public static void main(String[] args) {
        printSum(3, 4);
        printSum(10, -2);
    }

    public static void printSum(int first, int second) {
        System.out.println(first + " + " + second + " = " + (first + second));
    }
}
Output
3 + 4 = 7
10 + -2 = 8

A call must give exactly as many arguments as there are parameters, and of fitting types. With one argument missing, it doesn't compile:

public class Main {
    public static void main(String[] args) {
        printSum(3);
    }

    public static void printSum(int first, int second) {
        System.out.println(first + " + " + second + " = " + (first + second));
    }
}
What javac prints
Main.java:3: error: method printSum in class Main cannot be applied to given types;
        printSum(3);
        ^
  required: int,int
  found:    int
  reason: actual and formal argument lists differ in length
1 error

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).

Your task

Challenge 1 of 3

Write a method printProduct(int a, int b) that prints the calculation and its result. The check calls printProduct(3, 4); and expects:

3 * 4 = 12

Challenge 2

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:

The average is 3.5

Challenge 3

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:

3
6
9