I apologize for my newbiness, I'm in basic java right now and I have a project due tomorrow. I have the code written down but it does not match/add up to the output
Could anyone help fix it up?
There are 2 class files.
CoinToss.java
import java.util.Scanner; public class CoinToss { private static final int MIN_HEADS = 0; // minimum number of heads per set /** * main method: * ask the user how many sets of coin flips and how many flips per set * count occurances of heads in user specified number of sets of coin flips * draw a horizontal and vertical histogram of the number of heads */ public static void main(String[] args) { // set up to get user input Scanner scan = new Scanner(System.in); // ask user for the number of sets of coin flips System.out.println("How many sets of coin flips do you want?"); int nSets = scan.nextInt(); // ask user for the number of coin flips per set System.out.println("How many coin flips per set do you want?"); int nFlips = scan.nextInt(); // maximum number of heads that can occur is number of flips int maxHeads = nFlips; // calculate maximum length of histogram bars based on number of flips per set // based on finding the number of combinations for half heads / half tails // n! // formula is ----------- with r = n/2 // r! (n-r)! int maxLength = factorial (nFlips) / (factorial (nFlips / 2) * factorial (nFlips - nFlips / 2)); // instantiate a coin object named myCoin Coin myCoin = new Coin(); // step 1: // declare an int array named "counts" to count coin flip occurences // make its size one larger than maxHeads int counts[] = new int [maxHeads + 1]; // step 2: // initialize all of the values in the array to 0 for(int x = 0; x < counts.length; x++) { counts[x] = 0; } // step 3: // instantiate an array of Coin objects of size nFlips and // instantiate a Coin object in each element of the array Coin[] coinArray = new Coin[nFlips]; for(int x = 0; x < coinArray.length; x++) { coinArray[x] = new Coin(); } // step 4: // flip each coin in the array once per set nSets times and // count the number of heads in each set (heads = 1, tails = 0) for (int x = 0; x < nSets; x++) { int sum = 0; for (int i = 0; i <nFlips; i++) { sum = sum +(myCoin.flip()? 1:0); } } // step 5: // print out the estimated probabilities of all heads and all tails System.out.println((float) counts [MIN_HEADS] / nSets); // step 6: // instantiate an object of the Histogram class with // the array to be drawn, the indices of valid data, // and the maximum length of the bars in the histogram // // call its two draw methods to draw the two histograms int maxCount = 0; for (int x = MIN_HEADS; x < maxHeads +1; x++) { if(maxCount <= counts [x]) { maxCount = counts [x]; } break; } } // function to calculate factorial of n private static int factorial(int n) { int factorial = 1; // write the code for a loop to calculate factorial of n here for(int x = 1; x <= n; x++) { factorial *= x; } return factorial; } } /* 201420 */
Histogram.java
public class Histogram { private int [] values; private int minIndex; private int maxIndex; private int maxLength; /** constructor for histogram drawing class * @param values the array of integer values to draw * @param minIndex the lowest index in the array for the range to draw * @param maxIndex the highest index in the array for the range to draw * @param maxLength the length of line to draw for the largest value */ public Histogram(int [] values, int minIndex, int maxIndex, int maxLength) { // initialize the values of the instance variables from the constructor parameters this.values = new int [maxIndex + 1]; // allocate memory for a copy of the values array this.minIndex = minIndex; this.maxIndex = maxIndex; this.maxLength = maxLength; // step 7: // find largest number in values array for scaling length of bars int MaxValue = values[0]; for(int x=minIndex; x<=maxIndex; x++) { if (values[x] > MaxValue) MaxValue = values[x]; } // step 8: // copy data from values array to this.values array while scaling to maximum length of bars System.arraycopy(values,0,this.values,0,values.length); } /** draw a horizontal bar graph */ public void drawHor() { // step 8: // draw horizontal bar graph (one line per roll value) for (int i = 1; i<values.length; i++) System.out.print("Value " + i + ":" + "\t" + values[i]); System.out.println("*"); } /** draw a vertical bar graph */ public void drawVer() { // step 10: // draw vertical bar graph (one column per roll value) for (int i = 1; i<values.length; i++) System.out.print("Value " + i + ":" + "\t" + values[i]); System.out.println("*"); } }/*201420*/
And this is what the output is supposed to be
> java Histogram How many sets of coin flips do you want? 1000 How many coin flips per set do you want? 6 Estimated probabilities for: All Heads: 0.015 All Tails: 0.011 Heads Count 0: * 1 Heads Count 1: ****** 6 Heads Count 2: *************** 15 Heads Count 3: ******************** 20 Heads Count 4: **************** 16 Heads Count 5: ****** 6 Heads Count 6: * 1 Count 20 * Count 19 * Count 18 * Count 17 * Count 16 * * Count 15 * * * Count 14 * * * Count 13 * * * Count 12 * * * Count 11 * * * Count 10 * * * Count 9 * * * Count 8 * * * Count 7 * * * Count 6 * * * * * Count 5 * * * * * Count 4 * * * * * Count 3 * * * * * Count 2 * * * * * Count 1 * * * * * * * 0 1 2 3 4 5 6 >
This is my output when I run it
> run CoinToss How many sets of coin flips do you want? [1000] How many coin flips per set do you want? [6] 0.0 >