# Lab goals

Goals for this problem set:

• practice problems similar to what will be on a midterm exam

# Exercise 1: lastDigit

• Write a method named `lastDigit` that returns the last digit of an integer. For example, `lastDigit(3572)` should return `2`. It should work for negative numbers as well. For example, `lastDigit(-947)` should return `7`.
• (Hint: This is a short method.)

# Exercise 2: firstDigit

• Write a method named `firstDigit` that returns the first digit of an integer. For example, `firstDigit(3572)` should return `3`. It should work for negative numbers as well. For example, `firstDigit(-947)` should return `9`.
• (Hint: This problem requires a loop.)

# Exercise 3: season

• Write a method named `season` that takes two integers as parameters representing a month and day and that returns a String indicating the season for that month and day. Assume that months are specified as an integer between 1 and 12 (1 for January, 2 for February, and so on) and that the day of the month is a number between 1 and 31.
• If the date falls between 12/16 and 3/15, you should return `"Winter"`. If the date falls between 3/16 and 6/15, you should return `"Spring"`. If the date falls between 6/16 and 9/15, you should return `"Summer"`. And if the date falls between 9/16 and 12/15, you should return `"Fall"`.

# Exercise 4: makeGuesses

Write a method named `makeGuesses` that will output random numbers between 1 and 50 inclusive until it outputs one of at least 48. Output each guess and the total number of guesses made. Below is a sample execution:

```guess = 43
guess = 47
guess = 45
guess = 27
guess = 49
total guesses = 5
```

# Exercise 5: allDigitsOdd

Write a method named `allDigitsOdd` that returns whether every digit of a positive integer is odd. Your method should return `true` if the number consists entirely of odd digits and `false` if any of its digits are even. 0, 2, 4, 6, and 8 are even digits, and 1, 3, 5, 7, 9 are odd digits.

For example, `allDigitsOdd(135319)` returns `true` but `allDigitsOdd(9145293)` returns `false`.

Hint: You can pull apart a number into its digits using ```/ 10``` and `% 10`.

# Exercise 6: hopscotch

Write a method named `hopscotch` that accepts an integer parameter for a number of "hops" and prints a hopscotch board of that many hops.

For example, the call `hopscotch(3);` would produce the following output:

```   1
2     3
4
5     6
7
8     9
10
```

# Exercise 7: hasMidpoint

Write a method `hasMidpoint` that accepts three integers as parameters, and returns `true` if one of the numbers is the midpoint of the other two and returns `false` otherwise.

For example, the call `hasMidpoint(3, 7, 5)` would return `true` because one of the parameters (5) is the midpoint of the other two (3 and 7).

# Exercise 8: before

Write a method `before` that takes as parameters two month/day combinations and that returns whether or not the first date comes before the second date (`true` if the first month/day comes before the second month/day, `false` if it does not). The method will take four integers as parameters that represent the two month/day combinations.

The first integer in each pair represents the month and will be a value between 1 and 12 (1 for January, 2 for February, etc, up to 12 for December). The second integer in each pair represents the day of the month (a value between 1 and 31). One date is considered to come before another if it comes earlier in the year.

# Exercise 9: sameDashes

Write a method `sameDashes` that takes two strings as parameters and that returns whether or not they have dashes in the same places (returning `true` if they do and returning `false` otherwise). For example, below are four pairs of strings of equal length that have the same pattern of dashes. Notice that the last pair has no dashes at all.

```string 1:    "hi--there-you."    "-15-389"    "criminal-plan"    "abc"
string 2:    "12--(134)-7539"    "-xy-zzy"    "(206)555-1384"    "9.8"
```
To be considered a match, the strings must have exactly the same number of dashes in exactly the same positions. The Strings might be of different length.

# Exercise 10: flip

Write a method `flip` that takes a `Random` object as a parameter and that prints information about a coin-flipping simulation.

Your method should use the `Random` object to produce a sequence of simulated coin flips, printing whether each flip comes up "heads" or "tails". Each outcome should be equally likely. Your method should stop flipping when you see three heads in a row.

# Exercise 11: "Boolean Zen"

This attempted solution to Self-Check 5.15 (`isVowel`) has several problems:

```// Returns whether the given string represents a vowel:
// a, e, i, o, or u, case insensitively.
public static boolean isVowel(String s) {
if (s == "a") {
return true;
} else if (s == "e") {
return true;
} else if (s == "i") {
return true;
} else if (s == "o") {
return true;
} else if (s == "u") {
return true;
} else {
return false;
}
}
```

Open Practice-It from the link above, copy/paste this code into it, then see the next slide.

# Exercise - things to fix

Fix the following aspects of the code:

• It has a bug related to how strings are compared.
• It isn't case sensitive; it fails for uppercase vowels.
• It has too many unnecessary if/else statements.
• (advanced) It does not use "Boolean Zen" as described in textbook section 5.3.

```public static boolean isVowel(String s) {
s = s.toLowerCase();
if (s.equals("a") || s.equals("e") || s.equals("i")
|| s.equals("o") || s.equals("u")) {
return true;
} else {
return false;
}
}
```

The above can be improved. "Boolean Zen" version:

```public static boolean isVowel(String s) {
s = s.toLowerCase();
return s.equals("a") || s.equals("e") || s.equals("i")
|| s.equals("o") || s.equals("u");
}
```

# Exercise 12: Expressions

For each expression in the left-hand column, indicate its value in the right-hand column. Be sure to list a constant of appropriate type (e.g., 7.0 rather than 7 for a double, Strings in quotes).

 `4 * (2 + 4) - 3 * 5` `9` `54 % 10 + 8 * 3 % 9` `10` `3 * 2 + 4 + "+" + 2 + 3 * 4` `"10+212"` `2.3 * 3 + 19 / 5 / 2 + 6.0 / 5` `9.1` `108 / 20 * 3 / 4 / 2.0 + 1.0 / 2` `2.0`

# Exercise 13: 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 14: `if`/`else` mystery

Consider the following Java code.

```public static void ifElseMystery(int a, int b) {
if (a < b) {
a = a * 2;
}
if (a > b) {
a = a - 10;
} else {
b++;
}
System.out.println(a + " " + b);
}
```

Fill in the boxes with the output produced by each of the method calls.

 `ifElseMystery(10, 3);` `0 3` `ifElseMystery(6, 6);` `6 7` `ifElseMystery(3, 4);` `-4 4` `ifElseMystery(4, 20);` `8 21`

# Exercise : `while` loop mystery

Fill in the boxes at right with the output produced by each method call.

```public static void mystery4(int n) {
int x = 1;
int y = 2;
while (y < n) {
if (n % y == 0) {
n = n / y;
x++;
} else {
y++;
}
}
System.out.println(x + " " + n);
}
```
 `mystery4(2);` `1 2` `mystery4(5);` `1 5` `mystery4(24);` `4 3` `mystery4(28);` `3 7`