[an error occurred while processing this directive] common/conf CSE 142 Lab Resources: Assertions

University of Washington, CSE 142

Lab Resources: Assertions

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

Assertions

Assertion problems test your ability to reason through code. For example, consider the following:

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);
}

Assertions: Addressing Point A

For each point, we want to figure out whether z == 0, x > y, and x == y are always true, sometimes true, or never true. Let's first think about Point A. We know a few things about Point A:

Assertions: Addressing Point A

x > y z == 0 x == y
Point A
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);
}
	

Assertions: Engaging Point E

Now let's skip to Point E, where we're going to need to do a little bit more reasoning.

Assertions: Engaging Point E

x > y z == 0 x == y
Point 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);
}
	

Assertions: Battling Point B

Okay, now we need to figure out what's going on inside the while loop! Let's take a look at Point B first.

Assertions: Battling Point B

x > y z == 0 x == y
Point B
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);
}
	

Assertions: Calculating Point C

Now let's take a look at Point C!

Assertions: Calculating Point C

x > y z == 0 x == y
Point C
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);
}
	

Assertions: Deducing Point D

Finally, we need to figure out what's going on with Point D!

Assertions: Deducing Point D

x > y z == 0 x == y
Point D
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're done!

Nice work! Assertions are tough. Make sure to practice some more problems before the midterm!