Java Reference
In-Depth Information
// Point C
if (x > y) {
// Point D
x = x / 10;
} else {
// Point E
y = y / 10;
}
// Point F
}
// Point G
System.out.println("common prefix = " + x);
System.out.println("digits discarded = " + z);
}
This method finds the longest sequence of leading digits that two numbers have in
common. For example, the numbers 32845 and 328929343 each begin with the prefix
328. This method will report that prefix and will also report the total number of digits
that follow the common prefix and that are discarded.
We will examine the program to check whether various assertions are always true,
never true, or sometimes true and sometimes false at various points in program execu-
tion. The comments in the method indicate the points of interest. The assertions we
will consider are:
x > y
x == y
z == 0
Normally computer scientists write assertions in mathematical notation, as in z =
0 , but we will use a Java expression to distinguish this assertion of equality from the
practice of assigning a value to the variable.
We can record our answers in a table with the words “always,” “never,” or “some-
times.” Our table will look like the following one:
x > y
x == y
z == 0
Point A
Point B
. . .
. . .
. . .
. . .
Let's start at point A, which appears near the beginning of the method's execution:
public static void printCommonPrefix(int x, int y) {
int z = 0;
// Point A
 
Search WWH ::




Custom Search