Class AlternatingCoin

java.lang.Object
  extended by AlternatingCoin
All Implemented Interfaces:
RandomWithParity

public class AlternatingCoin
extends java.lang.Object
implements RandomWithParity

Generates a binary coin (0 or 1) in strict alternating sequence. With every flip of the coin, the coin changes from 0 to 1 or vice versa. The first call returns a 0, then 1, then 0, and so on.

Correspondence (abstraction relation):
|history| is even <==> doneEvenFlips
Author:
paolo

Field Summary
private  boolean doneEvenFlips
          True if and only if an even number of invocations to this AlternatingCoin have been made.
 
Constructor Summary
AlternatingCoin()
          Initialially, zero flips have been performed, hence doneEvenFlips is true.
 
Method Summary
 int generateNumber(int upperBound)
          Returns a 0 or a 1 (alternating) and ignoring upperBound parameter.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

doneEvenFlips

private boolean doneEvenFlips
True if and only if an even number of invocations to this AlternatingCoin have been made.

Constructor Detail

AlternatingCoin

public AlternatingCoin()
Initialially, zero flips have been performed, hence doneEvenFlips is true.

Method Detail

generateNumber

public int generateNumber(int upperBound)
Returns a 0 or a 1 (alternating) and ignoring upperBound parameter. The first number returned is 0.

Specified by:
generateNumber in interface RandomWithParity
Parameters:
upperBound - the maximum value that can be generated by this call
Returns:
0 <= generateNumber <= upperBound
See Also:
RandomWithParity.generateNumber(int)