Java Arena
Step 3 of 5

Parameters

A method can take parameters: values it gets from the call. They're declared in the parentheses, each with a type and a name, and inside the method they work like variables:

public class Main {
    public static void main(String[] args) {
        greet("Ada");
        greet("Linus");
        String name = "Grace";
        greet(name);
    }

    public static void greet(String name) {
        System.out.println("Hello, " + name + "!");
    }
}
Output
Hello, Ada!
Hello, Linus!
Hello, Grace!
  • String name in greet(String name) is the parameter. Each call gives it a value: greet("Ada") runs the method with name holding "Ada".
  • The value in the call is the argument. It can be a literal, a variable or a calculation, such as greet(first + " " + last).
  • The argument must fit the parameter's type: greet(42) doesn't compile, because 42 isn't a String.

The same method now does something different on each call, depending on what it's given.

Challenges that call your method

From here on, some challenges ask for a method, and the check calls it directly. The task card shows the call it makes, such as countdown(3);, and what that call must print. Your own main can call the method however you like: that's what Run with my input runs.

Your task

Challenge 1 of 3

Fill in the parameter of shout, and use it in the print line. The check calls shout("hey"); and expects:

hey!!!

Challenge 2

Write a method countdown(int start) that prints the numbers from start down to 1, one per line, and then Liftoff!. The check calls countdown(3); and expects:

3
2
1
Liftoff!

Challenge 3

Write a method printSquares(int n) that prints the squares of the numbers from 1 to n, one per line: 1 times 1, then 2 times 2, and so on. The check calls printSquares(4); and expects:

1
4
9
16