There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
acer black screen blue screen blue screen of death boot bsod computer connection crash css dell display driver drivers email error explorer firefox firefox 3 hard drive internet internet explorer itunes laptop lcd malware monitor network networking outlook outlook 2003 outlook express printer problem problems ram router security slow software sound sprtcmd.exe trojan usb video virus vista windows windows xp wireless
Web Design & Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Internet & Networking > Web Design & Development >
Java Help


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

Closed Thread
 
Thread Tools
mizzlea's Avatar
Junior Member with 1 posts.
 
Join Date: Oct 2007
28-Oct-2007, 11:07 PM #1
Java Help
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 [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; i < (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; i <= maxHrads +2; i++)
System.out.print("Heads Counts " + i);
{
for (int j = 0; j < counts [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; v >= 1; v--)
{
System.out.print ("Count " +v+ " ");
for (int u = 0; u < maxHeads +1; u++)
{
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;
}
}
}
tomdkat's Avatar
Computer Specs
Distinguished Member with 3,716 posts.
 
Join Date: May 2006
Location: S.F. Bay Area, CA
Experience: Intermediate
29-Oct-2007, 02:00 PM #2
This really belongs in the "Development" forum. What is the error message you're getting?

Peace...
Fyzbo's Avatar
Senior Member with 1,777 posts.
 
Join Date: Feb 2002
Location: North Carolina, USA
Experience: Programming-Advanced|EVER
29-Oct-2007, 02:12 PM #3
PHP Code:
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_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(" * ");
                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 {{}}
Closed Thread

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who help people like you solve computer problems. See our Welcome Guide to get started.



Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 12:55 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.