/* An instance of this class represents a (potentially biased) coin, ** suitable for simulating a sequence of coin tosses. ** It enhances its parent class by including methods that report, ** respectively, how many times the coin was tossed, how many of those ** tosses produced HEADS, and how many produced TAILS. ** ** Author: R. McCloskey ** Date: January 2024 */ public class BiasedCoinWithCounts extends BiasedCoin { // instance variables // ------------------ protected int headCntr; // # tosses that resulted in HEADS protected int tailCntr; // # tosses that resulted in TAILS // constructors // ------------ /* Same as corresponding constructor in parent class. */ public BiasedCoinWithCounts(int seed, double probOfHeads) { super(seed, probOfHeads); resetCounts(); // not necessary, because zero is default value } /* Same as corresponding constructor in parent class. */ public BiasedCoinWithCounts(int seed) { super(seed); resetCounts(); // not necessary, because zero is default value } /* Same as corresponding constructor in parent class. */ public BiasedCoinWithCounts(double probOfHeads) { super(probOfHeads); resetCounts(); // not necessary, because zero is default value } /* Same as corresponding constructor in parent class. */ public BiasedCoinWithCounts() { super(); resetCounts(); // not necessary, because zero is default value } // observers // --------- /* Reports how many times this coin has been tossed. */ public int tossCount() { return headsCount() + tailsCount(); } /* Reports how many tosses of this coin have resulted in HEADS. */ public int headsCount() { return headCntr; } /* Reports how many tosses of this coin have resulted in TAILS. */ public int tailsCount() { return tailCntr; } @Override public String toString() { String countsStr = String.format(" # Heads: %d, # Tails:%d", headsCount(), tailsCount()); return super.toString() + "; " + countsStr; } // mutators // -------- /* Resets the counters to zero (in effect, making this coin "forget" ** about all its past tosses). */ public void resetCounts() { headCntr = 0; tailCntr = 0; } @Override /* Tosses this coin and updates the relevant counters. */ public void toss() { super.toss(); if (isHeads()) { headCntr++; } else { tailCntr++; } } }