Assertions
Category: Assertions
Author: Benson Limketkai
Book Chapter: 5.5
Problem: Assertions
For each of the five points labeled by comments, identify each of the following assertions as being either always true, never true or sometimes true / sometimes false. public static int mystery(int x) { Scanner console = new Scanner(System.in); System.out.print("Enter a number GREATER than " + x + ": "); int y = console.nextInt(); y = y * 2; <span class="assertionpoint">// Point A while (y > x) { <span class="assertionpoint">// Point B if (x % 2 == 1) { <span class="assertionpoint">// Point C x++; y--; } else if (y % 2 == 0) { y /= 2; } else { y++; x = x - 2; <span class="assertionpoint">// Point D } } <span class="assertionpoint">// Point E return x; }
1) x % 2 == 0
2) y > x
3) y % 2 == 0