University of Washington, CSE 142 (190)

Lab 5-5: Midterm practice

Except where otherwise noted, the contents of this document are Copyright 2010 Stuart Reges and Marty Stepp.

lab document created by Whitaker Brand and Marty Stepp

Today's lab

Goals for today:

Exercise : Expressions practice-it

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 : Parameter Mystery practice-it

Exercise : if/else mystery practice-it

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 practice-it

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

public static void mystery(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);
}
mystery(2);
1 2
mystery(5);
1 5
mystery(24);
4 3
mystery(28);
3 7

Exercise : assertions practice-it

Identify whether each assertion is always/never/sometimes true at each point.

public static void mystery(int x, int y) {
    int z = 0;

    // Point A
    while (x != y) {
        // Point B
        z++;

        if (x > y) {
            // Point C
            x = x / 10;
        } else {
            // Point D
            y = y / 10;
        }
    }

    // Point E
    System.out.println(x + " " + y + " " + z);
}

Solve this problem in Practice-It by clicking on the check-mark above.

Exercise : flip practice-it

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.

Solve this problem in Practice-It by clicking on the check-mark above.

Exercise : before practice-it

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.

Solve this problem in Practice-It by clicking on the check-mark above.

Exercise : sameDashes practice-it

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.

Solve this problem in Practice-It by clicking on the check-mark above.

If you finish them all...

If you finish all the exercises, work on other sample midterm questions in Practice-It.