Write a Java program to read n positive integers from an array and graph the inf
ID: 3759599 • Letter: W
Question
Write a Java program to read n positive integers from an array and graph the information in the form of a bar chart or a histogram - each number is printed, and then a number consisting of that many asterisks is printed beside the number. Have a method to fill the array with random numbers: the elements of the array should be random integers between 1 and 15. Also, have a method to print the array and another method to print the histogram, as in the sample output below. Work with dynamic arrays (ask the user to input the size of the array) and make sure you validate the input.
Explanation / Answer
The below is the program which you require. Please run the code and trace the execution of the code for easy understanding:
import java.util.Random;
import javax.swing.JOptionPane;
public class Histogram
{
/*This program will read a list of 10 integers from a user between 0 to 99 and will
generate a histogram of those numbers*/
public static void main(String[]args)
{
int [] randarray = new int [100];
Random randomNumbers = new Random();System.out.println("Histogram Chart");
System.out.println("---------");
int i ;
for ( i = 0; i<randarray.length;i++)
{
int temp = randomNumbers.nextInt(100); //random numbers up to number value 100
randarray = temp;
}
int [] histo = new int [10];
for ( i = 0; i<10; i++)
{
if (randarray <= 10) {
histo[1] = histo + 1;
//System.out.println("*");
}
else if ( randarray[1] <= 20){
histo[2] = histo[1] + 1;
}
else if (randarray[2] <= 30)
{ histo[3] = histo[2] + 1;
}
else if ( randarray[3] <= 40){
histo[4] = histo[3] + 1;
}
else if (randarray[4] <= 50){
histo[5] = histo[4] + 1;
}
else if ( randarray[5] <=60){
histo[6] = histo[5] + 1;
}
else if ( randarray[6] <=70){
histo[7] = histo[6] + 1;
}
else if ( randarray[7] <=80){
histo[8] = histo[7] + 1;
}
else if ( randarray[8] <=90){
histo[9] = histo[8] + 1;
}
else if ( randarray[9] <=100){
histo[10] = histo[9] + 1;
}
switch (randarray)
{
case 1: System.out.print("0-09 | ");
break;
case 2: System.out.print("10-19 | ");
break;
case 3: System.out.print("20-29 | ");
break;
case 4: System.out.print("30-39 | ");
break;
case 5: System.out.print("40-49 | ");
break;
case 6: System.out.print("50-59 | ");
break;
case 7: System.out.print("60-69 | ");
break;
case 8: System.out.print("70-79 | ");
break;
case 9: System.out.print("80-89 | ");
break;
case 10: System.out.print("90-99 | ");
break;
}
for (int i = 0; i < 99; i++)
{
histo[randomNumbers.nextInt(100)[10]++));
index = (randomNumber - 1) / 2;
histo[index]++;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.