Write a Java program to read n positive integers from an array and graph the inf
ID: 3762118 • 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.
Sample output:
Do you want to start(Y/N): y
Enter array size: 12
The array elements are:
8 8 10 6 9 2 8 7 10 12 10 8
Index Value Histogram
---------------------------------
0 8 ********
1 8 ********
2 10 **********
3 6 ******
4 9 *********
5 2 **
6 8 ********
7 7 *******
8 10 **********
9 12 ************
10 10 **********
11 8 ********
Do you want to continue(Y/N): y
Enter array size: 15
The array elements are:
9 6 14 1 5 5 3 14 3 6 12 3 11 9 3
Index Value Histogram
---------------------------------
0 9 *********
1 6 ******
2 14 **************
3 1 *
4 5 *****
5 5 *****
6 3 ***
7 14 **************
8 3 ***
9 6 ******
10 12 ************
11 3 ***
12 11 ***********
13 9 *********
14 3 ***
Do you want to continue(Y/N): y
Enter array size: -7
ERROR! Should be positive. REENTER: 7
The array elements are:
10 10 8 13 13 15 10
Index Value Histogram
---------------------------------
0 10 **********
1 10 **********
2 8 ********
3 13 *************
4 13 *************
5 15 ***************
6 10 **********
Do you want to continue(Y/N): n
Explanation / Answer
import java.util.Random;
import java.util.Scanner;
public class Histogram {
public static int array[] = new int[15];
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
try {
while (true) {
System.out.print(" Do you want to start(Y/N): ");
char choice = scanner.next().charAt(0);
if (choice == 'y') {
System.out.print("Enter array size: ");
int arraySize = scanner.nextInt();
if (arraySize < 0) {
System.out.print("ERROR! Should be positive. REENTER:");
arraySize = scanner.nextInt();
}
setArray(arraySize);
displayArray(arraySize);
displayHistogram(arraySize);
} else {
break;
}
}
} catch (Exception e) {
} finally {
scanner.close();
}
}
/**
* @param arraySize
* @return
*/
public static int[] setArray(int arraySize) {
int min = 1, max = 15;
Random rand = new Random();
for (int i = 0; i < arraySize; i++) {
int randomNum = rand.nextInt((max - min) + 1) + min;
array[i] = randomNum;
}
return array;
}
/**
* @param arraySize
*/
public static void displayArray(int arraySize) {
for (int i = 0; i < arraySize; i++) {
System.out.print(array[i] + " ");
}
}
/**
* @param arraySize
*/
public static void displayHistogram(int arraySize) {
System.out.println(" Index Value Histogram");
System.out.println("--------------------------------- ");
for (int i = 0; i < arraySize; i++) {
System.out.print(" " + i + " " + array[i] + " ");
for (int j = 0; j < array[i]; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
out put:
Do you want to start(Y/N): y
Enter array size: 12
13 4 3 2 7 13 11 12 9 10 5 6
Index Value Histogram
---------------------------------
0 13 *************
1 4 ****
2 3 ***
3 2 **
4 7 *******
5 13 *************
6 11 ***********
7 12 ************
8 9 *********
9 10 **********
10 5 *****
11 6 ******
Do you want to start(Y/N): y
Enter array size: 13
4 5 6 11 9 5 13 11 9 15 10 7 11
Index Value Histogram
---------------------------------
0 4 ****
1 5 *****
2 6 ******
3 11 ***********
4 9 *********
5 5 *****
6 13 *************
7 11 ***********
8 9 *********
9 15 ***************
10 10 **********
11 7 *******
12 11 ***********
Do you want to start(Y/N): y
Enter array size: -5
ERROR! Should be positive. REENTER:5
4 15 15 13 10
Index Value Histogram
---------------------------------
0 4 ****
1 15 ***************
2 15 ***************
3 13 *************
4 10 **********
Do you want to start(Y/N): n
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.