1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Java Help

Discussion in 'Web Design & Development' started by mizzlea, Oct 28, 2007.

Thread Status:
Not open for further replies.
  1. mizzlea

    mizzlea Thread Starter

    Oct 28, 2007
    I'm trying to complete a project and i'm getting an error message at the end where it says public static int factorial (int n). Does anyone know what I'm doing wrong?

    import java.util.Scanner;

    public class Histogram {

    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 arrayindex = MIN_HEADS; arrayindex <= maxHeads; arrayindex ++)
    counts[arrayindex] = 0;

    // step 3:
    // flip the coin for n sets with nFlips flips per set
    // count the number of heads in each set (heads = 1, tails = 0)

    // Using the value nSets (the number of sets of flips) and nFlips
    // (the number of coin flips per set) from the user, execute a loop nSets times.
    // Each time through this outer loop, your code starts with sum equal to zero and
    // loops for nFlips times calling the flip method of myCoin, adding either one for a
    // head or zero for a tail to the sum. After the inner loop completes, the outer loop
    // uses the sum calculated by the inner loop as an array index, and increments the
    // value of that element in the counts array.

    for (int i = 0; i < nSets; i++) {
    int sum = 0;
    for (int j = 0; i < nFlips; j++)
    sum = sum + (myCoin.flip()? 1: 0);

    // step 4:
    // print out the estimated probabilities of all heads and all tails
    System.out.println ((float) counts [MIN_HEADS] / nSets);

    // step 5:
    // find largest number in counts array for scaling length of bars

    int maxCount = 0;
    for (int i = MIN_HEADS; i < maxHeads +1; i++)
    if (maxCount <= counts )
    maxCount = counts ;

    // step 6:
    // scale data in counts array to maximum length of bars
    // add 0.5 to round off instead of truncate in conversion to int
    for (int i = MIN_HEADS; i < (maxHeads +1); i++)
    counts = ((double)(count * maxLength / myCount) +.5);

    // step 7:
    // draw horizontal bar graph (one line per heads value)
    for (int i = MIN_HEADS; i <= maxHrads +2; i++)
    System.out.print("Heads Counts " + i);
    for (int j = 0; j < counts ; j++)
    System.out.print ("x");

    System.out.println (counts );
    // step8:
    // draw vertical bar graph (one column per heads value)
    for (int v = maxHeads; v >= 1; v--)
    System.out.print ("Count " +v+ " ");
    for (int u = 0; u < maxHeads +1; u++)
    if (counts >= v)
    System.out.print(" * ");
    System.out.print (" ");

    System.out.println ("");

    // function to calculate factorial of n
    public static int factorial(int n)
    int factorial = 1;

    // write the code for a loop to calculate factorial of n here

    //**while (counter > 1)
    //**factorial *= --counter; // Multiply the decremented number.

    return factorial;
  2. tomdkat

    tomdkat Retired Trusted Advisor

    May 6, 2006
    This really belongs in the "Development" forum. What is the error message you're getting?

  3. Fyzbo


    Feb 6, 2002
    import java.util.Scanner;

    public class 

        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 arrayindex MIN_HEADSarrayindex <= maxHeadsarrayindex ++)
    counts[arrayindex] = 0;
    // step 3:
            // flip the coin for n sets with nFlips flips per set
            // count the number of heads in each set (heads = 1, tails = 0)
            // Using the value nSets (the number of sets of flips) and nFlips
            // (the number of coin flips per set) from the user, execute a loop nSets times.
            // Each time through this outer loop, your code starts with sum equal to zero and
            // loops for nFlips times calling the flip method of myCoin, adding either one for a
            // head or zero for a tail to the sum. After the inner loop completes, the outer loop
            // uses the sum calculated by the inner loop as an array index, and increments the
            // value of that element in the counts array.
    for (int i 0nSetsi++)
    int sum 0;
                for (
    int j 0nFlipsj++)
    sum sum + (myCoin.flip()? 10);
    // step 4:
                // print out the estimated probabilities of all heads and all tails
    System.out.println ((float) counts [MIN_HEADS] / nSets);
    // step 5:
                // find largest number in counts array for scaling length of bars
    int maxCount 0;
                for (
    int i MIN_HEADSmaxHeads +1i++)
                    if (
    maxCount <= counts [i])
    maxCount counts [i];
    // step 6:
                    // scale data in counts array to maximum length of bars
                    // add 0.5 to round off instead of truncate in conversion to int
    for (int i MIN_HEADS< (maxHeads +1); i++)
    counts [i] = ((double)(count [i] * maxLength myCount) +.5);
    // step 7:
                    // draw horizontal bar graph (one line per heads value)
    for (int i MIN_HEADS<= maxHrads +2i++)
    System.out.print("Heads Counts " i);
                        for (
    int j 0counts [i]; j++)
    System.out.print ("x");
    System.out.println (counts [i]);
    // step8:
            // draw vertical bar graph (one column per heads value)
    for (int v maxHeads>= 1v--)
    System.out.print ("Count " +v" ");
                for (
    int u 0maxHeads +1u++)
                    if (
    counts [u] >= v)
    System.out.print(" * ");
    System.out.print (" ");
    System.out.println ("");
    // function to calculate factorial of n
    public static int factorial(int n)
    int factorial 1;

    // write the code for a loop to calculate factorial of n here

            //**while (counter > 1)
            //**factorial *= --counter; // Multiply the decremented number.

    return factorial;
    Note: [PHP /PHP] tags help make code readable.

    The first thing I have to ask is why there are an extra set of brackets for that function {{}}
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/645065

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice