import java.io.*;
/*---------------------------------------------------------------------------------------------
CS100 P3: Q4
Program reads in an arbitrary range of integers from the range 1 to 100 inclusive.
Entering '0' signals that the user is finished inputting data. A chart is then produced
that indicates how many input values fell in the ranges 1-10, 11-20, etc.
Author : Alan Renaud (ajr5@cornell.edu)
Date : 23 July 1999
------------------------------------------------------------------------------------------------*/
class Histogram {
static TokenReader in = new TokenReader(System.in);
public static void main(String[] args)
{
intro();
double val = getInput(">");
if (val !=0)
{
int[] freq = enterVals(val);
printOutput(freq);
}
else
bye();
}
/*---------------------------------------------------------------------------------------------
getInput
input : message string
return : value input by user
Returns value of user's input if the value lies in the interval [0-100].
------------------------------------------------------------------------------------------------*/
private static double getInput(String message)
{
System.out.print(message);
System.out.flush();
double val = in.readDouble();
while ((val < 0)||(val > 100))
{
System.out.print("Number must be [1-100] : ");
System.out.flush();
val = in.readDouble();
}
return val;
}
/*---------------------------------------------------------------------------------------------
enterVals
input : double value from [1-100]
return : int [10]
Prompts user for values. For each value from [1-100], adds 1 to the appropriate
bin in the array. Ends when user types 0. Returns the array.
------------------------------------------------------------------------------------------------*/
private static int[] enterVals(double val)
{
int[] f = new int[10];
while (val > 0)
{
f[(int) ((val - 0.5)/10)]++;
val = getInput(">");
}
return f;
}
/*---------------------------------------------------------------------------------------------
printOutput
input : int [10]
return : none
Prints a histogram of the values typed in by user.
------------------------------------------------------------------------------------------------*/
private static void printOutput(int[] f)
{
for (int i=1; i<=f.length; i++)
{
System.out.print("\n" + ((i==1) ? " " : ""+ (i-1)) + "1-" + i + "0| ");
for (int j=1; j<=f[i-1]; j++)
System.out.print("*");
System.out.flush();
}
System.out.println();
in.waitUntilEnter();
}
/*---------------------------------------------------------------------------------------------
bye
input : none
return : none
Prints goodbye message when user elects not to play.
------------------------------------------------------------------------------------------------*/
private static void bye()
{
System.out.println("\nYou didn't enter any values!\nCan't make a histogram out of nothing now, can we?");
in.waitUntilEnter();
}
/*---------------------------------------------------------------------------------------------
intro
input : none
return : none
Message to introduce the game
------------------------------------------------------------------------------------------------*/
private static void intro()
{
System.out.println("Welcome to the histogram program.\nPlease enter in a series of numbers from 1-100.");
System.out.println("When finished, enter 0 to see your results.\n");
}
}
/*
SAMPLE OUTPUT:
Welcome to the histogram program.
Please enter in a series of numbers from 1-100.
When finished, enter 0 to see your results.
>40
>42
>43
>44
>45
>10
>30
>89
>81
>83
>99
>0
1-10| *
11-20|
21-30| *
31-40| *
41-50| ****
51-60|
61-70|
71-80|
81-90| ***
91-100| *
Press Enter to continue.
*/