import java.io.*;
//-------------------------------------------------------------------
// Program P3 Q4
//
// This program prompts users for a series of positive integers between
// 0 and 100 inclusive, and display a text-based histogram of the data
// entered.
//
// Author : Wei Tsang Ooi
// Date : 19 July 1999
//-------------------------------------------------------------------
class Histogram {
final static int NUM_OF_HISTOGRAM_BUCKET = 10;
final static int HISTOGRAM_BUCKET_SIZE = 10;
//---------------------------------------------------------------
// intro
//
// print an introduction message.
//---------------------------------------------------------------
private static void intro()
{
System.out.print("I will ask for a series of integers, and " +
"display a histogram representing the numbers.");
}
//---------------------------------------------------------------
// getInput
//
// input : stdin - BufferedReader to read input from
// return : a double value input by user, guranteed to be
// greater than zero.
//---------------------------------------------------------------
private static int getInput(BufferedReader stdin)
throws IOException
{
int input;
String msg = "Enter an integer between 0 and 100 [0 to quit] :";
System.out.print(msg);
input = Integer.parseInt(stdin.readLine());
// keep asking for input until the input is within range.
while (input < 0 || input > 100) {
System.out.println("ERROR : input must be between 0 and 100");
System.out.print(msg);
input = Integer.parseInt(stdin.readLine());
}
return input;
}
//---------------------------------------------------------------
// initHistogram
//
// This method takes in a histogram and initialize the elements
// to zero.
//---------------------------------------------------------------
private static void initHistogram(int histogram[])
{
for (int i = 0; i < NUM_OF_HISTOGRAM_BUCKET; i++)
{
histogram[i] = 0;
}
}
//---------------------------------------------------------------
// printHistogram
//
// This method takes in a histogram of integer and prints out a
// histogram.
//---------------------------------------------------------------
private static void printHistogram(int histogram[])
{
for (int i = 0; i < NUM_OF_HISTOGRAM_BUCKET; i++)
{
// print the label
System.out.print((i*HISTOGRAM_BUCKET_SIZE + 1) + "-" +
((i+1)*HISTOGRAM_BUCKET_SIZE) + "|");
// print the histogram bar
for (int j = 0; j < histogram[i]; j++)
System.out.print("*");
// go to a new line
System.out.println();
}
}
//---------------------------------------------------------------
// main
//
// Keep reading values from user, until a 0 is entered. Then
// print out the average, min and max value of the values entered.
//---------------------------------------------------------------
public static void main(String argv[]) throws IOException
{
// create and initialize the histogram
int histogram[] = new int[NUM_OF_HISTOGRAM_BUCKET];
initHistogram(histogram);
// print an introductory message
intro ();
BufferedReader stdin = new BufferedReader
(new InputStreamReader(System.in));
// Keep getting input until the user enter 0.
int input = getInput(stdin);
while (input != 0) {
// add 1 to the appropiate bucket in the histogram.
histogram[(input - 1) / HISTOGRAM_BUCKET_SIZE]++;
input = getInput(stdin);
}
printHistogram(histogram);
}
}