# Java Help

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

Not open for further replies.

Joined:
Oct 28, 2007
Messages:
1
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

// 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
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;
{
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
counts = ((double)(count * maxLength / myCount) +.5);

// step 7:
// draw horizontal bar graph (one line per heads value)
{
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(" * ");
else
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;
}
}
}

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

Peace...

3. ### Fyzbo

Joined:
Feb 6, 2002
Messages:
1,683
PHP:
import java.util.Scanner;

public class
Histogram
{

private static final

/**
* 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

// 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

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 (
{
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

counts [i] = ((double)(count [i] * maxLength myCount) +.5);

// step 7:
// draw horizontal bar graph (one line per heads value)

{
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)

{

System.out.print ("Count " +v" ");
for (
{
if (
counts [u] >= v)

System.out.print(" * ");
else

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