# Lab goals

Goals for this problem set:

• practice declaring and passing parameters to methods

# Parameters

A parameter allows you to pass in a value to a method as you call it.

```public static void name(type name) {   // declare
```
```methodName(expression);                // call
```

Example:

```public static void squared(int num) {
System.out.println(num + " times " + num + " is " + (num * num));
}
...
squared(3);          // 3 times 3 is 9
squared(8);          // 8 times 8 is 64
```

# Exercise : Parameters output

• What two values are printed by each call below? Write them in the boxes provided.
```public class MysteryNums {
public static void main(String[] args) {
int x = 15;
sentence(x, 42);      // 15 42

int y = x - 5;
sentence(y, x + y);   // 10 25
}

public static void sentence(int num1, int num2) {
System.out.println(num1 + " " + num2);
}
}
```

# Solving "Parameter Mystery" problems

• Note the values passed, the order in which they are passed, and the order the method prints them.
```public class MysterySoda {
public static void main(String[] args) {
String soda = "coke";
String pop = "pepsi";
String pepsi = "soda";

// #1 = "coke", #2 = "pepsi", #3 = "soda"
carbonated(soda, pop, pepsi);
}
//                                    #1           #2           #3
public static void carbonated(String coke, String soda, String pop) {
//                           #2               #3             #1
System.out.println("say " + soda + " not " + pop + " or " + coke);
}
}
```
• output: `// say#2pepsi not#3soda or#1coke`

# Exercise 2: Parameter Mystery

• Fill in the boxes with the output that each method call would produce:
```public class MysterySoda {
public static void main(String[] args) {
String soda = "coke";
String pop = "pepsi";
String coke = "pop";
String pepsi = "soda";
String say = pop;

carbonated(soda, pop, pepsi);        // say pepsi not soda or coke
carbonated(coke, soda, pop);         // say coke not pepsi or pop
carbonated(pop, pepsi, pepsi);       // say soda not soda or pepsi
carbonated("pop", pop, "koolaid");   // say pepsi not koolaid or pop
carbonated(say, "say", pop);         // say say not pepsi or pepsi
}
public static void carbonated(String coke, String soda, String pop) {
System.out.println("say " + soda + " not " + pop + " or " + coke);
}
}
```

# Exercise 3: Parameter Mystery

• Fill in the boxes with the output that each method call would produce:
```public class MysteryNumbers {
public static void main(String[] args) {
String one = "two";
String two = "three";
String three = "1";
int number = 20;

sentence(one, two, 3);                  // three times two = 6
sentence(two, three, 14);               // 1 times three = 28
sentence(three, three, number + 1);     // 1 times 1 = 42
sentence(three, two, 1);                // three times 1 = 2
sentence("eight", three, number / 2);   // 1 times eight = 20
}
public static void sentence(String three, String one, int number) {
System.out.println(one + " times " + three + " = " + (number * 2));
}
}
```

# Exercise 4: Parameter Mystery

• Fill in the boxes with the output that each method call would produce:
```public class Mystery {
public static void main(String[] args) {
String song = "good";
String good = "hear";
String walk = "talk";
String talk = "feel";
String feel = "walk";
claim(feel, song, feel);       // to walk the walk is good
claim(good, hear, song);       // to hear the good is bad
claim(talk, "song", feel);     // to feel the walk is song
claim("claim", talk, walk);    // to claim the talk is feel
}

public static void claim(String hear, String good, String song) {
System.out.println("to " + hear + " the " + song + " is " + good);
}
}
```

# Exercise : Syntax errors

• The following Java program has 9 errors. Can you find all of them?  ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ``` ```public class Parameters { public static void main() { double bubble = 867.5309; double x = 10.01; printer(double x, double y); printer(x); printer("barack", "obama"); System.out.println("z = " + z); } public static void printer(x, y double) { int z = 5; System.out.println("x = " + double x + " and y = " + y); System.out.println("The value from main is: " + bubble); } } ```
• Copy and paste the code into jGrasp and see if you can fix the errors.

1. line 5: cannot use variable `y` without declaring and initializing it
2. line 5: cannot declare the type of `y` in the method call
3. line 6: cannot call `printer` without the correct number of parameters (2, in this case)
4. line 7: cannot call `printer` by passing the correct type of parameters (double, in this case)
5. line 8: cannot refer to the variable `z`: it is in scope inside `printer`, not `main`
6. line 11: must provide a type for `x`
7. line 11: must provide a variable name for the second parameter
8. line 12: must refer to the parameters using the exact same spelling
9. line 13: cannot refer to variables in `main` that were not passed into `printer` as a parameter

# Exercise - Corrected version

• Here is a corrected version of the program:
```public class Parameters {
public static void main(String[] args) {
double bubble = 867.5309;
double x = 10.01;
double y = 5.3;
printer(double x, double y);
printer(x, 0.0);
printer("barack", "obama");
int z = 5;
System.out.println("z = " + z);
}

public static void printer(double x, double y) {
System.out.println("x = " + x + " and y = " + y);
System.out.println("The value from main is: " + bubble);
int z = 5;
}
}
```

# Exercise 6: `printGrid`

• Write a method named `printGrid` that accepts two integers representing a number of rows and columns and prints a grid of integers from 1 to (rows*columns) in column-major order.
• For example, the call:
```printGrid(4, 6);
```
should produce the following output:
```1 5 9 13 17 21
2 6 10 14 18 22
3 7 11 15 19 23
4 8 12 16 20 24
```
•

# Exercise 7: `printSquare`

• Write a method `printSquare` that accepts min and max integers and prints a square of lines of increasing numbers. The first line should start with the minimum; each line that follows should start with the next-higher number. The numbers on a line wrap back to the minimum after it hits the maximum. For example, the call:
```printSquare(3, 6);
```
should produce the following output:
```3456
4563
5634
6345
```
• If the maximum passed is less than the minimum, the method produces no output.
• Hint: remember the mod operator or think of each lines as having two parts...