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:

Exam Tips

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

If you've forgotten how to tackle expressions problems, check out lab 2 for a recap!

Also, make sure you can evaluate boolean expressions.

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
4 / 5 == 0 && 3 % 2 == 1
true
4 % 2 != 0 && 5 == 5
false
3 % 2 == 0 || 6 / 3 != 4
true

Exercise : Parameter Mystery

(Review this type of problem in lab 3!) Fill in the boxes with the output that each method call produces:

public static void main(String[] args) {
    String hear = "bad";
    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 : if/else mystery

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);
}
(Review this type of problem in lab 4!) 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. (Review how to do this type of problem in lab 5!)

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

Write always/never/sometimes true at each point. If unsure how to, check out the assertions tutorial!

x > y z == 0 x == y
A
B
C
D
E
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);
}

You can also 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

If you're not sure how make/use a Random, review them here.

Exercise : seeMovie practice-it

You're thinking about going with your friends to a movie. Write a Java method seeMovie that accepts two parameters: the cost of a ticket in dollars, and the rating number of stars the movie received out of 5. The method should print how interested you are (very, sort-of, or not). Use the following criteria:

Exercise : swapPairs practice-it

Write a method named swapPairs that accepts a String as a parameter and returns that String with each pair of adjacent letters reversed. If the String has an odd number of letters, the last letter is unchanged. For example, the call swapPairs("forget") should return "ofgrte" and the call swapPairs("hello there") should return "ehll ohtree".

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!