COP 2800 (Java Programming) Project #5 — Histogram


Due: by the start of class on the date shown on the syllabus


Using or as examples, create a Java non-GUI stand-alone application that displays a histogram.  A histogram is a kind of non-GUI bar chart (see example bar chart) that shows how many times a given value occurs in an input sample.  The more times a given value appears, the longer the bar becomes for that value.  With a histogram, you draw the chart sideways.

This program can be used to examine all sorts of frequency data.  In the example below, I used this histogram program to analyze the scores of a previous COP 2800 Exam #1.  I entered in all the question numbers that were answered incorrectly, for each and every student exam.  (For instance, on the first student answered questions 1, 3, 4, and 19 incorrectly, so I input: 1 3 4 19.  The next student had questions 4, 17, 19, and 22 incorrect, so I continued by entering 4 17 19 22 this time.  I kept this up for each student's exam.)  The output shows 8 questions were much harder than the rest, and that two questions were so trivial nobody answered them incorrectly.  (I have used this information to update my test bank.)

To actually draw the horizontal lines of the histogram, you must use the utils.TextKit.lineOfStars method you created in TextKit Project.  If you don't have a working utils package, I can supply one, but not using your original one will cost you some credit.  This project will require the use of arrays and input.

Your application should be packaged as a runnable Jar file.


Write a Java non-GUI program that will accept as input integer values from the standard input, each value is a number between 1 and 25.  The input might contain any number of values separated by white-space.  The user will indicate the end of the list by signaling EOF, which on Windows systems is done by hitting control+Z.  (On Unix and Macintosh systems EOF can be signaled by hitting control+D instead.)

The output shall consist of 25 lines of stars (asterisks), one line for each of the possible input values (the numbers 1 to 25).  The number of stars drawn shows how many times each value was entered as input.  Each line should be labeled with the value it is showing the bar for.  So, if the input was:

    C:\TEMP>  java Histogram 
    Enter integers <= 25, one per line, hit control-Z when done:

Then the output would be:

     1: **
     2: ***
     4: *

Your Java program must use the method called lineOfStars to create the stars for each line of output.  This method must take a single int parameter which says how many stars to draw.  This method must be a static method of a class called TextKit, which must be in a package called utils.  This should be the method you created for the previous project.  You are not allowed to modify your utils.TextKit class in any way from what you completed in the previous project without the approval of your instructor.

Your program will have its class in the default, nameless package, or any other packege you want to create, except in the utils package (where TextKit is located).

Sample Output:   COP 2800 Exam I Analysis

(Note only a few of the more than 100 values actually typed by me are shown here.)

C:\TEMP>  java Histogram 
Enter integers <= 25, one per line, hit control-Z when done: 

	 Histogram showing how many students
	 answered each question wrong: 

  1: **
  2: *****
  3: *****
  5: ***
  6: **
  7: *****
  9: **
 10: ***************
 11: *****
 12: ********
 13: *************
 14: ********
 15: *******
 16: ***************
 17: ***
 18: **************
 19: *******************
 20: **********
 21: *******
 22: ********
 23: *************
 24: **************
 25: ****************


Make sure you understand the histogram: each entered value is represented by one star, so the total number of stars equals the number of input values.  The length of each line of stars represents the number of times that input value was entered.  For example, for the input value of 17, you can see three stars, which means that number was input three times.

When done, bundle your class file (or package) as a runnable Jar file.  This means I should be able to run your program with (assuming the name is “histogram.jar”):

java -jar histogram.jar


Start this project by creating a skeleton class with a stub main method.  Then think about what the program should do, and add comments for that.  You should be asking yourself “does the program need to store the input?” and “what kind of loop(s) should be used?”.  Don't start writing any code until you understand what your code should do.

Many students make the mistake of thinking the input data is the length of each bar.  Take another look at the sample data and the resulting output.  The input is not the lengths of the 25 bars!

It may help to think of the input as a series of “bar numbers” instead.  For the first part of your program you must read these numbers one at a time, and increment a counter, (that stores that bar's length), for each one.  You repeat this until you run out of input data.  The second part of your program needs to print each of the 25 bars.

Java 5 provides input methods (the Scanner class) that make reading the input (and converting to Integers) very easy.  You are encouraged to use them.

Any program that reads input from standard input can read input data from a file too.  This technique is known as input redirection and is very useful when testing.  To read data from a file instead of the keyboard, run your program using a command line such as:

C:\TEMP>  java Histogram < data.txt

To practice using the same data I used, download HistogramData.txt and use that as the input, as shown above.

You program should be easily read and well commented, including your name.  Remember the best programs result when you start with the design, then make a skeleton class containing your design as comments, and finally fill in the code below each comment, one piece at a time.  If you wish you can send me your design “skeleton” program before starting to write any code, to make sure you're on the right track.

Ask me for help if you get stuck!

To be turned in:

Email to me a copy of the source code for your Histogram class, as well as the final runnable Jar file.  You do not need to send your utils package.  Send projects to .  Please use a subject such as "Java Histogram Project" so I can tell which emails are submitted projects. 

Send project questions to .  Please use a subject such as "Java Histogram Project Questions" so I can tell which emails are questions about the project.

Please review Submitting Assignments and Project Grading Criteria from your class syllabus for further details.