# Thread: Assignment has me on edge....

1. Member
Join Date
Dec 2016
Posts
6
Rep Power
0

## Assignment has me on edge....

Modifying the Coin Class
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.

We haven't covered this in class and are being expected to hand this in. It is so confusing and I have tried asking for help, but no one seems to understand this in the GA's office or tutor center.

2. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
26

## Re: Assignment has me on edge....

What haven't you covered?

You apparently have a start point (it mentions a Coin class) that you need to rename and use to add the new functionality.
Step 1 explains in quite a lot of detail what to change.

3. ## Re: Assignment has me on edge....

Moved to New section.

4. ## Re: Assignment has me on edge....

You can almost, to the letter, follow the instructions. It's all in there. Let me give you a start:

Create a new class named BiasedCoin that models a biased coin
Java Code:
class BiasedCoin {
// here is the content of the Coin class you were supposed to copy and modify
}
Add a private data member bias of type double
Java Code:
class BiasedCoin {
private double bias;

//other content
}
Modify the default constructor by assigning the value 0.5 to bias
Java Code:
class BiasedCoin {
private double bias;

public BiasedCoin() {
bias = 0.5;
}

//other content
}
I know I am giving away pretty much here, but this should enable you to get started and finish this easily. Let us know how it goes.

5. ## Re: Assignment has me on edge....

My crystal ball tells me the Coin class looks like this, is this correct?

Java Code:
import java.util.Random;

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.
//-----------------------------------------------------------------
{
}

//-----------------------------------------------------------------
//  Returns the current face of the coin as a string.
//-----------------------------------------------------------------
public String toString()
{
String faceName;

else
faceName = "Tails";

return faceName;
}
}

6. ## Re: Assignment has me on edge....

Omg, all the professors are stealing this from eachother. It's all over the internet, literally :)

7. Member
Join Date
Dec 2016
Posts
6
Rep Power
0

## Re: Assignment has me on edge....

So I was able to write the code for everything that was the beginning portion of it. Now I am having trouble understanding the last part which is the "Write a program that uses three BiasedCoin...."

Am I suppose to create another class or project. it is unclear to me. and should I be calling the BiasedCoin class? if so, how?

8. ## Re: Assignment has me on edge....

calling the BiasedCoin class? if so, how?
Use the new statement to create an instance of the class, save the reference to that instance and use it to call the class's methods for testing.

9. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
26

## Re: Assignment has me on edge....

I would create another class, called whatever you want (TestBiasedCoin?), int he same package (place) as the BiasedCoin class.
That will have a main() method (your notes should tell you what that is if you've ever run a Java program before) that will do the work.

10. ## Re: Assignment has me on edge....

Your program, like Tolls says, probably lives in a main method somewhere. It can be in the BiasedCoin class itself, but for clarity sake it's probably better to create that TestBiasedCoin class. You will create three new objects with the new keyword:
Java Code:
BiasedCoin coin1 = new BiasedCoin();
BiasedCoin coin2 = new BiasedCoin(0.6);
BiasedCoin coin3 = new BiasedCoin(0.9);
You can now execute the methods on each object.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•