Assertions
Category: Assertions
Author: Stuart Reges
Book Chapter: 5.5
Problem: Assertions
You will identify various assertions as being either always true, never true or sometimes true/sometimes false at various points in program execution. The comments in the method below indicate the points of interest.
public static int numOddDigits(int n) {
int x = 0;
<span class="assertionpoint">// Point A
while (n != 0) {
<span class="assertionpoint">// Point B
if (n % 2 == 1) {
x++;
<span class="assertionpoint">// Point C
}
n = n / 10;
<span class="assertionpoint">// Point D
}
<span class="assertionpoint">// Point E
return x;
}
1) n == 0
2) n % 2 == 1
3) x == 0