import java.util.*; import javax.swing.JOptionPane; import javax.swing.JTextArea
ID: 3543670 • Letter: I
Question
import java.util.*;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
public class NumberHistogram
{
public static void main(String[] args)
{
int i;
int j;
final int MAX_NUMBER = 11;//numbers 0 to 10
String inputValue = JOptionPane.showInputDialog("How many random values do you want?");
int numberInput = Integer.parseInt(inputValue)
String input = "Number Value Count of Occurrences Histogram";
int[] num = new int[numberInput];
Random numberGenetor = new Random();
for (i = 0; i < num.length; i++)
{
int randomNumber = numberGenetor.nextInt(MAX_NUMBER);
num[i] = randomNumber;
System.out.println(num[i]);//testing the numbers
input += " " + i + " " + num[i] + " " + " |";
}
for (i = 0; i < MAX_NUMBER; i++)
{
for (j = 0; j < num.length; j++)
{
input += "*";
}
input += " ";
}
//collecting all the inputHeader putting into the JtextArea
//for the JoptionPance MessageDialog box
JTextArea inputList = new JTextArea();
inputHeaderList.setText(input);
JOptionPane.showMessageDialog(
null, inputList, "Message",
JOptionPane.INFORMATION_MESSAGE);
}
}
How many random values do you want?Explanation / Answer
import java.util.*;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
public class NumberHistogram
{
public static void main(String[] args)
{
int i;
int j;
final int MAX_NUMBER = 11;//numbers 0 to 10
String inputValue = JOptionPane.showInputDialog("How many random values do you want?");
int numberInput = Integer.parseInt(inputValue);
String input = "Number Value Count of Occurrences Histogram";
int num ;
Random numberGenetor = new Random();
int numCount[]={0,0,0,0,0,0,0,0,0,0,0};
for (i = 0; i <numberInput; i++)
{
int randomNumber = numberGenetor.nextInt(MAX_NUMBER);
num = randomNumber;
for(int k=0;k<11;k++)
{
if(num==k)numCount[k]++;
}
}
//System.out.println(num[i]);//testing the numbers
for(int l=0;l<11;l++)
{
String str="";
for (j = 0; j < numCount[l]; j++)
{
str +="*";
}
input += " " + l + " " + numCount[l]+ " " + " |"+str;
}
//collecting all the inputHeader putting into the JtextArea
//for the JoptionPance MessageDialog box
JTextArea inputList = new JTextArea();
inputList.setText(input);
JOptionPane.showMessageDialog(
null, inputList, "Message",
JOptionPane.INFORMATION_MESSAGE);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.