Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Modifying Coin Class Given Below 1. Create a new class named BiasedCoin that mod

ID: 3810493 • Letter: M

Question

Modifying Coin Class Given Below

1. Create a new class named BiasedCoin that models a biased coin (heads and tails are not equally likely outcomes of a flip). To do this modify the coin class from the Listing 5.4 of text (in the file Coin.java) as follows: Add a private data member bias of type double. This data member will be a number between 0 and 1 (inclusive) that represents the probability the coin will be HEADS when flipped. So, if bias is 0.5, the coin is an ordinary fair coin. If bias is 0.6, the coin has probability 0.6 of coming up heads (on average, it comes up heads 60% of the time). Modify the default constructor by assigning the value 0.5 to bias before the call to flip. This will make the default coin a fair one. Modify flip so that it generates a random number then assigns face a value of HEADS if the number is less than the bias; otherwise it assigns a value of TAILS. Add a second constructor with a single double parameter—that parameter will be the bias. If the parameter is valid (a number between 0 and 1 inclusive) the constructor should assign the bias data member the value of the parameter; otherwise it should assign bias a value of 0.5. Call flip (as the other constructor does) to initialize the value of face.

2. Compile your class to make sure you have no syntax errors.

3. Write a program that uses three BiasedCoin objects. Instantiate one as a fair coin using the constructor with no parameter. Read in the biases for the other two coins and instantiate those coins using the constructor with the bias as a parameter. Your program should then have a loop that flips each coin 100 times and counts the number of times each is heads. After the loop print the number of heads for each coin. Run the program several times testing out different biases.

//********************************************************************

// Coin.java       Author: Lewis/Loftus

//

// Represents a coin with two sides that can be flipped.

//********************************************************************

public class Coin

{

   private final int HEADS = 0;

   private final int TAILS = 1;

   private int face;

   //-----------------------------------------------------------------

   // Sets up the coin by flipping it initially.

   //-----------------------------------------------------------------

   public Coin ()

   {

      flip();

   }

   //-----------------------------------------------------------------

   // Flips the coin by randomly choosing a face value.

   //-----------------------------------------------------------------

   public void flip ()

   {

      face = (int) (Math.random() * 2);

   }

   //-----------------------------------------------------------------

   // Returns true if the current face of the coin is heads.

   //-----------------------------------------------------------------

   public boolean isHeads ()

   {

      return (face == HEADS);

   }

   //-----------------------------------------------------------------

   // Returns the current face of the coin as a string.

   //-----------------------------------------------------------------

   public String toString()

   {

      String faceName;

      if (face == HEADS)

         faceName = "Heads";

      else

         faceName = "Tails";

      return faceName;

   }

}

Explanation / Answer

HI, Please find my implementation.

Please let me know in case of any issue.

//********************************************************************

//BaisedCoin.java Author: Lewis/Loftus

//

//Represents a coin with two sides that can be flipped.

//********************************************************************

public class BiasedCoin

{

   private final int HEADS = 0;

   private final int TAILS = 1;

   private double bias;

   private int face;

   //-----------------------------------------------------------------

   // Sets up the coin by flipping it initially.

   //-----------------------------------------------------------------

   public BiasedCoin ()

   {

       bias = 0.5;

       flip();

   }

  

   //-----------------------------------------------------------------

       // Sets up the coin by flipping it initially based on given bais value

       //-----------------------------------------------------------------

       public BiasedCoin (double b)

       {

           if(b>=0 && b <= 1)

               bias = b;

           else

               bias = 0.5;

           flip();

       }

   //-----------------------------------------------------------------

   // Flips the coin by randomly choosing a face value.

   //-----------------------------------------------------------------

   public void flip ()

   {

       double random = (Math.random());

      

       if(random <= bias)

           face = HEADS;

       else

           face = TAILS;

   }

   //-----------------------------------------------------------------

   // Returns true if the current face of the coin is heads.

   //-----------------------------------------------------------------

   public boolean isHeads ()

   {

       return (face == HEADS);

   }

   //-----------------------------------------------------------------

   // Returns the current face of the coin as a string.

   //-----------------------------------------------------------------

   public String toString()

   {

       String faceName;

       if (face == HEADS)

           faceName = "Heads";

       else

           faceName = "Tails";

       return faceName;

   }

}

##################################################

public class BiasedCoinTest {

  

   public static void main(String[] args) {

      

       BiasedCoin fair = new BiasedCoin();

       BiasedCoin headBiased = new BiasedCoin(0.6);

       BiasedCoin tailBiased = new BiasedCoin(0.4);

      

       int headCount = 0;

       int tailCount = 0;

      

       System.out.println("Rolling fail coin 100 times");

       for(int i=1; i<=100; i++){

           fair.flip();

          

           if(fair.isHeads())

               headCount++;

           else

               tailCount++;

       }

       System.out.println("Head: "+headCount+ ", Tail: "+tailCount);

      

       headCount = 0;

       tailCount = 0;

       System.out.println("Rolling head biased coin 100 times");

       for(int i=1; i<=100; i++){

           headBiased.flip();

          

           if(headBiased.isHeads())

               headCount++;

           else

               tailCount++;

       }

       System.out.println("Head: "+headCount+ ", Tail: "+tailCount);

      

       headCount = 0;

       tailCount = 0;

       System.out.println("Rolling tail biased coin 100 times");

       for(int i=1; i<=100; i++){

           tailBiased.flip();

          

           if(tailBiased.isHeads())

               headCount++;

           else

               tailCount++;

       }

       System.out.println("Head: "+headCount+ ", Tail: "+tailCount);

   }

}

/*

sample run:

Rolling fail coin 100 times

Head: 39, Tail: 61

Rolling head biased coin 100 times

Head: 69, Tail: 31

Rolling tail biased coin 100 times

Head: 38, Tail: 62

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote