Help with histogram problem

 DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

# Thread: Help with histogram problem

1. Registered User
Join Date
Sep 2004
Posts
103

## Help with histogram problem

Okay I am almost complete with this project but I need help on these two methods that I have, i can't figure out how to create these methods so that they work.

The method countNumberRolls(), what it is supposed to do is count the total number of times that a pair of dice were rolled, so for example we have the numbers 2 through 12 the possible die roll combinations, So it cycles through 100 rolls, it's supposed to count how many times dice 2 through 12 were rolled, within the 100 rolls? Does that make sense, For some reason with this method It doesn't print out anything.

The next problem I'm having is displayRolls, This is supposed to cycle through the 100 and then display each one that was counted, This only displays the last dice that was rolld.

I am then supposed to pass these through in the bottom through setRollCount from the hui class.

import java.util.Random;
import java.util.*;

public class Histogram
{
private Random randomGenerator;
private int[] hundredRolls = new int[100];
private int[] countRolls = new int [13];

/**
* The Histogram constructor gets a seed value for the
* random number generator.
*
* The constructor needs to create a HistogramUI object. You
* also need an array to keep track of the dice roll values.
*/
public Histogram( long seed )
{
randomGenerator = new Random(seed);
int r = randomGenerator.nextInt();

}
/**
* Simulates the rolling of two dies and stores them in an
* array named hundredRolls.
*/
private int rollDice()
{
int result = 0;
int roll = randomGenerator.nextInt(6) + 1;
int roll2 = randomGenerator.nextInt(6) + 1;
result = roll + roll2;
return result;
}

// problem here in this method, this is supposed to display each dice that was rolled
public int displayrolls()
{
int total = 0;
for (int i = 0; i < hundredRolls.length; i ++){
total = hundredRolls[i];
countRolls[total]++;
}
}
// This is supposed to display the total number of times the dice was rolled.
public int countNumberOfRolls()
{
int toCount = 0;
for(int i = 0; i < hundredRolls.length; i++){
for(int k = 0; k < countRolls.length; k++){
if(hundredRolls[i] == countRolls[k]){
toCount ++;
}
}
}
}

public int sumOfRolls()
{
int total = 0;
for(int i = 0; i < 100; i++){
hundredRolls[i] = rollDice();
total += hundredRolls[i];
}
}

public double getMean()
{
int total = 0;
int rolls = sumOfRolls();
for(int index = 0; index < rolls; index++){
total += rollDice();
}
return (double)total / (double)rolls;
}

/**
* This method needs to call the HistogramUI methods to set up and
* display a Histogram.
*/
public void displayHistogram()
{
HistogramUI histogram = new HistogramUI( "Dice" );

histogram.setMax(20);

// Pass the number of times that a particular dice roll happened. For example, the statement:

//histogram.setRollCount( 2, 10 );

//would tell the user interface to display a bar 10 units high for dice roll 2. You need to call this method for each die roll in the range 2 through 12 so that every bar in the histogram will get displayed.

histogram.setRollCount(displayRolls(),countNumberOfRolls());

histogram.setMean(getMean());

histogram.setMedian(3.2);

histogram.setMode("23");

histogram.setVisible();
}

}

2. The HistogramUI class is missing. If you want us to find the
bug(s)/errors you should post all the code involved.

3. Registered User
Join Date
Sep 2004
Posts
103
Well The histogram is not of concern here, Just the methods, This assignment we have does not show the histogramUI class, We just pass numbers when We call on the methods of this one

// Pass the number of times that a particular dice roll happened. For example, the statement:

//histogram.setRollCount( 2, 10 );

//would tell the user interface to display a bar 10 units high for dice roll 2. You need to call this method for each die roll in the range 2 through 12 so that every bar in the histogram will get displayed.

histogram.setRollCount(displayRolls(),countNumberO fRolls());

We have to come up with m ethods that will relay this information through this class, The problems is with this method that I have created

// problem here in this method, this is supposed to display each dice that was rolled
public int displayrolls()
{
int total = 0;
for (int i = 0; i < hundredRolls.length; i ++){
total = hundredRolls[i];
countRolls[total]++;
}
}
// This is supposed to display the total number of times the dice was rolled.
public int countNumberOfRolls()
{
int toCount = 0;
for(int i = 0; i < hundredRolls.length; i++){
for(int k = 0; k < countRolls.length; k++){
if(hundredRolls[i] == countRolls[k]){
toCount ++;
}
}
}
}

By passing these methods through here

histogram.setRollCount(displayRolls(),countNumberO fRolls());

a visual graph will be displayed using this setRollCount method? I just cant' figure out these two methods in order for it to read correctly

public void setRollCount( int roll, int count )

4. Registered User
Join Date
Sep 2004
Posts
103
Originally Posted by sjalle
The HistogramUI class is missing. If you want us to find the
bug(s)/errors you should post all the code involved.

There are no bug, I just can't create the methods properly to pass the right numbers through this is where my "errors" are. Everything else within the project works correctly, just can't formulate these two methods to grab what dice was rolled and how many times each set of dice between the numbers 2 through 12 were rolled

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise