[an error occurred while processing this directive] common/conf
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
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); }
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
:
x > y, x == y
: x
and y
are equal to whatever was passed in as a parameters (any integers!) Are there any restrictions on the values that x
and y
could hold?z == 0
:z
= 0 executes immediately before Point A
. Is it possible for z == 0
to be false at A?x > y |
z == 0 |
x == y |
|
---|---|---|---|
Point A | SOMETIMES | ALWAYS | SOMETIMES |
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); }
Now let's skip to Point E, where we're going to need to do a little bit more reasoning.
x == y
:while
loop that loops while x != y
. This means that when we break out of the loop, x == y
. Since Point E
is after the while loop, what do we know about x == y
there?x > y
:x == y
must be true, is it possible for x > y
to be true?z == 0
:z == 0
is tricky. z
is initially equal to 0. However, as soon as we go into the while loop, we execute z++
(which would mean z != 0
). So z == 0
is true at Point E
if we don't go into the while
loop, and untrue if we do go into the while
loop. Will we necessarily go through the while
loop?x > y |
z == 0 |
x == y |
|
---|---|---|---|
Point E | NEVER | SOMETIMES | ALWAYS |
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); }
Okay, now we need to figure out what's going on inside the while
loop! Let's take a look at Point B
first.
x == y
:while
loop while its test is true. So we only encounter Point B
immediately after verifying that x != y
is still true. Can x or y have changed values by Point B
?
x > y
: sometimes
true, we just need to be able to think of a case where it's true, and a case where it's false. x
and y
could have any initial values (since they're just whatever was passed in as parameters). Is there any reason that x > y
must be true or false?z == 0
: z
is initially equal to 0, and on our first iteration through the while
loop, we hit Point B
before we alter z
's value. However, after our first pass over Point B
, we execute z++ (and z != 0
). So will z == 0
always, sometimes, or never be true at Point B
?
x > y |
z == 0 |
x == y |
|
---|---|---|---|
Point B | SOMETIMES | SOMETIMES | NEVER |
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); }
Now let's take a look at Point C
!
x > y
: Point C
, we entered the if
statement: if (x > y)
. This means that we will only reach Point C
when x > y!
Is it possible for x > y
to be false at Point C
?x == y
: x > y
must be true, this means that x != y
. Additionaly, to enter/continue looping through the while
loop, x != y
must be true. x
and y
don't change values between the while
loop test and Point C
. Is it possible for x == y
to be true at Point C
?z == 0
: Point C
, we must pass z++
. Is it possible for z == 0
to be true at Point C
?x > y |
z == 0 |
x == y |
|
---|---|---|---|
Point C | ALWAYS | NEVER | NEVER |
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); }
Finally, we need to figure out what's going on with Point D
!
x > y
: x > y
were true, then we would go into the if
branch. But Point D
is in the else
branch. So what do we know about x > y
at Point D
?
x == y
: Not going into if (x > y)
tells us that x <= y
. But can x == y
? Recall that to enter/continue looping through the while
loop, x != y
must be true. Also x and y are not changed between the while
loop test and Point D
. So what do we know about x == y
at Point D
?
z == 0
: z++
. Is it possible for z == 0
to be true when we get to Point D
?x > y |
z == 0 |
x == y |
|
---|---|---|---|
Point D | NEVER | NEVER | NEVER |
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); }
Nice work! Assertions are tough. Make sure to practice some more problems before the midterm!