I need help with this java program!! Please!?
-
Here are the instructions: 1 Add a private data member bias of type double. This data member will be a number between 0 and 1 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). Override the default constructor by assigning the value 0.5 to bias after the call to superclass constructor. This will make the default coin a fair one. Override flip method so that it generates a random floating point number between 0 and 1 (use Math.random() method), 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 method from the superclass to initialize the value of face. 2. Compile your class to make sure you have no syntax errors. 3. Write a program that uses one Coin object and two BiasedCoin objects. Read in the biases for the two BiasedCoins and instantiate those coins using the constructor with the bias as a parameter. Your program should then have a loop that flips each coin 1000 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 And here is what I have: //************************************… // Coin.java // Represents a coin with two sides that can be flipped. //************************************… public class Coin { protected final int HEADS = 0; protected final int TAILS = 1; protected 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; } } // ****************************************… // BiasedCoin.java // // Author: Shekeara Singleton // // Represents a coin with two sides that can be flipped // ****************************************… public class BiasedCoin extends Coin { public final int HEADS = 0; public final int TAILS = 1; private int face; private double bias; public BiasedCoin() { bias = 0.5; flip(); } public BiasedCoin(double d) { if ( d >=0 && d <= 1 ) bias = d; else bias = 0.5; flip(); } public void flip() { if ( Math.random() < bias ) face = HEADS; else face = TAILS; } public int getFace() { return face; } public String toString() { String faceName; if ( face == HEADS) faceName = "Heads"; else faceName = "Tails"; return faceName; } } import java.util.Scanner; public class TestBiasedCoin { public static void main(String[] args) { int countHeads = 0; int countTales = 0; double bias1; double bias2; Coin coin = new Coin(); BiasedCoin Bcoin = new BiasedCoin(); BiasedCoin Bcoin2 = new BiasedCoin(); Scanner scan = new Scanner(System.in); System.out.println(); bias1 = scan.nextDouble(); System.out.println(); bias2 = scan.nextDouble(); coin.flip(); Bcoin = new BiasedCoin(bias1); Bcoin2 = new BiasedCoin(bias2); Bcoin.flip(); Bcoin2.flip(); for(int i=0; i<1000; i++) { } System.out.println(coin); System.out.println(Bcoin); System.out.println(Bcoin2); } }
-
Answer:
The 2 big mistakes are 1) over-riding the member face in BiasedCoin. 2) not counting heads after you flip each coin (should occur inside the test loop). I recommend that you delete the declaration of face from the class BiasedCoin, and changing the test function as follows: System.out.print("Enter Bias 1:"); bias1 = scan.nextDouble(); System.out.println(); System.out.print("Enter Bias 2:"); bias2 = scan.nextDouble(); System.out.println(); Bcoin = new BiasedCoin(bias1); Bcoin2 = new BiasedCoin(bias2); int [] count = new int [3]; for(int i=0; i<1000; i++) { coin.flip(); if (coin.isHeads()) count[0]++; Bcoin.flip(); if (Bcoin.isHeads()) count[1]++; Bcoin2.flip(); if (Bcoin2.isHeads()) count[2]++; } System.out.println(count[0]); System.out.println(count[1]); System.out.println(count[2]); =======================================…
fun_girl... at Yahoo! Answers Visit the source
Other answers
Well, you showed us the assignment and showed us your program. What specifically do you need help with?
Related Q & A:
- I need help on what I need to buy or do.Best solution by Yahoo! Answers
- I did something really bad and now i need help please help me.Best solution by Yahoo! Answers
- I need help with writing up the Java Code.Best solution by Yahoo! Answers
- I want to download full encyclopedia program, please help.Best solution by Yahoo! Answers
- I need help with my camera, please.Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.