Assertions
Category: Assertions
Author: Marty Stepp
Book Chapter: 5.5
Problem: Assertions
For each of the five points labeled by comments, identify each of the following assertions as either being always true, never true, or sometimes true / sometimes false. // Counts number of "coin tosses" until we get heads 3 times in a row. public static int threeHeads() { Random rand = new Random(); int flip = 1; int heads = 0; int count = 0; <span class="assertionpoint">// Point A while (heads < 3) { <span class="assertionpoint">// Point B flip = rand.nextInt(2); // flip coin if (flip == 0) { // heads heads++; <span class="assertionpoint">// Point C } else { // tails <span class="assertionpoint">// Point D heads = 0; } count++; } <span class="assertionpoint">// Point E return count; }
1) flip == 0
2) heads == 0
3) flip > heads