The final picture is how the program run should look. So far here is my code to
ID: 3648303 • Letter: T
Question
The final picture is how the program run should look. So far here is my code to show you that I have attempted it.
import java.util.Random;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
public class NumberHistogram
{
// Declare the integer range
public static final int MAX_INT = 10;
public static final int MIN_INT = 0;
public static final int RANGE = 11;
public static void main(String[] args)
{
String output = "Number Value Count of Occurrences Histogram ";
// Prompt user and ask how many random values they want to generate
String input = JOptionPane.showInputDialog("How many random values do you want?");
int numValues = Integer.parseInt(input);
// Standard one-dimensional array to represent the integers from 0 to 10
int[] list = new int[RANGE];
// Use a random object within a loop to produce the random integer
Random generator = new Random();
for (int i = 0; i < numValues; i++)
{
int randNum = generator.nextInt(MAX_INT);
System.out.println(randNum); // useful to see the numbers generated
list[i] = randNum;
output += " " + i + " " + list[i] + " " + " |";
// print bar of asteriks
for (int j = 0; j < list[i]; j++ )
{
output += "*";
}
output += " " + i + " " + list[i] + " " + " |";
}
JTextArea outputArea = new JTextArea();
outputArea.setText(output);
JOptionPane.showMessageDialog(null, outputArea,
"Message", JOptionPane.INFORMATION_MESSAGE);
}
}
Explanation / Answer
I think you may have misunderstood what the array is being used for. The assignment wording - "to represent the integers from 0 to 10" - is a little odd. What is really meant is that that the array is meant to store the counts for each of the integers. Bearing this in mind, the for loop looks wrong. Java Code: 1 for (int i = 0; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.