University of Washington, CSE 142

Lab 5-5: midterm practice

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

lab document created by Marty Stepp, Stuart Reges and Whitaker Brand

Basic lab instructions

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

12/5 + 8/4
4
2.5 * 2 + 17/4
9.0
41 % 15 % 7 + 17 % 3
6
21/2 + "7 % 3" + 17 % 4
"107 % 31"
46/3/2.0/3 * 4/5
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 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

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

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

Try solving this problem in Practice-It! from the link above.

Exercise : printFactors practice-it

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.

Exercise : allDigitsOdd practice-it

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

Exercise : firstDigit practice-it

If you finish them all...

If you finish all the exercises, try out our Practice-It web tool. It lets you solve Java problems from our Building Java Programs textbook.

You can view an exercise, type a solution, and submit it to see if you have solved it correctly.

Choose some problems from the book and try to solve them!