This exercise's objective is to get you some experience in using arrays.
Show the work that you do to the GTA.
A "histogram" is a visual display of data. In a histogram, each number in a set of data is represented by a bar. The length of the bar is proportional to the number it represents. This makes it easy to see certain relationships among the data.
In the program you will work with, each bar is a line of asterisks. For example, the ten numbers 5, 10, 15, 20, 30, 40, 50, 35, 20, 5 could be displayed in a histogram as:
***** ********** *************** ******************** ****************************** **************************************** ************************************************** *********************************** ******************** *****
Write a small program that takes a set of numbers from the user and then displays a histogram based on those numbers. Your program should first obtain how many numbers the user has to enter. The array should be created based on this input. Then, the data is obtained from the user. Since all the data must be input before the histogram can be output, the data must be stored in an array as it is read. Then the numbers in the array are used to create the histogram.
You can use the code shown below to get started. You need to write the missing code.import java.util.Scanner; /* This file defines a "histogram" program. The user is asked to enter some numbers. The numbers are stored in an array. Then the data in the array are displayed in the form of a histogram. (For each number in the array, the histogram shows a line of stars. The number of stars in the i-th line is equal to the number in the i-th spot in the array.) NOTE: Your assignment is to complete the missing parts of of the program. You can use the printStars() subroutine, given below, to output each line of stars. */ public class LabExercise6 { public static void main(String[] args) { Scanner inputScanner = new Scanner(System.in); int numberOfLines = 0; int[] numbers; // Repeat the following code until the user enters a number // between 1 and 20 do { System.out.print("Enter a number between 1 and 20: "); numberOfLines = inputScanner.nextInt(); } while (numberOfLines > 20 || numberOfLines < 1); // Create an array to hold the user's numbers. The size of the // array is given by the number of numbers the user wants to enter. // ************ YOU HAVE TO DO THIS *********** // Use a for loop to read the user's numbers, one at a time, and // store them in the array: // ************ YOU HAVE TO DO THIS *********** // Output the histogram, using a for loop. Call the "printStars()" // once for each number in the array: System.out.println(); System.out.println("Here's a histogram of your data:"); System.out.println(); System.out .println(" 10 20 30 40 50"); System.out .println("|---------|---------|---------|---------|---------|-----"); // ************ YOU HAVE TO DO THIS *********** } } /** * Description: This method accepts a number and * prints out that number of stars on a single * line. * @param numberOfStars: The number of stars that * have to be printed on each line. * @author Mir Farooq Ali * */ public static void printStars(int numberOfStars) { for (int j = 0; j < numberOfStars; j++) System.out.print('*'); } }Adapted from http://math.hws.edu/eck/cs124/labs98/lab7/Histogram.java