Java programming project #5 – histogram

Create a Java non-GUI stand-alone application that displays a histogram.  A histogram is a kind of non-GUI 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.  This project will require the use of arrays and input.

 

Write a Java non-GUI program that will accept as input integer values, each value is a number between 1 and 25, with one value per line.  The input might contain any number of values.  The user will indicate the end of the list by signaling EOF, which on DOS/Windows systems is done by hitting a control-Z.  (On Unix and Macintosh systems EOF can be signaled by hitting a 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:

   1

   2

   4

   2

   1

   2

   control-Z 

Then the output would be:

    1: **

    2: ***

    3:

    4: *

    5:

    6:

    7:

    8:

    9:

   10:

   11:

   12:

   13:

   14:

   15:

   16:

   17:

   18:

   19:

   20:

   21:

   22:

   23:

   24:

   25:

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 and not in the utils package (where TextKit is located).

TextKit.java

package util;

/**

 * This is a utility class. It contains two utility methods and can be 

 * used in different applications.

 */

public class TextKit {

    /**

     * This method will create and return a String containing a line of asterisks (or stars).

     * If invalid argument is passed, it throws IllegalArgumentException.

     * 

     * @param n number of stars to draw.

     * @return a string containing stars.

     * @exception IllegalArgumentException if the argument is negative.

     */

    public static String lineOfStar(int n){

        //If n is negative then throw IllegalArgumentException.

        if(n < 0){

           throw new IllegalArgumentException(“Negative number”); 

        } 

        //A String object that will contain stars.

        String starLine = “”;

        //Create the string of stars.

        for(int i=1; i<=n; i++ ){

            starLine = “*” + starLine;

        }        

       return starLine; //Return the string.

    }

    

    /**

     * This method will format integers by adding spaces (called padding) to the 

     * left end, to make the resulting String a certain minimum length.If the number 

     * contains more digits than the specified width, then no padding is added.

     * If invalid argument is passed, it throws IllegalArgumentException.

     * 

     * @param num  number to format.    

     * @param minLen desired minimum String length.

     * @return the padded string.

     * @exception IllegalArgumentException if the argument is negative.

     */

    public static String pad(int num, int minLen){

        //If minLen is negative then throw IllegalArgumentException.

        if(minLen < 0){

           throw new IllegalArgumentException(“Negative number”); 

        }   

        //String object to store padded string.

        String paddedLine = String.valueOf(num);

        //If the number contains more digits than the specified width, then no 

        //padding is added and the string is returned.

        if(paddedLine.length() >= minLen){

            return paddedLine;

        }

        //Calculate the number of blank spaces to be padded.

        int padLength = minLen – paddedLine.length();

        //Pad the blank spaces.

        for(int i=1; i<=padLength; i++ ){

            paddedLine = ” ” + paddedLine;

        }

       return paddedLine;   //Return the padded string.

    }    

}

TextKitApp.java

import util.TextKit;

/**

 * This is the application class to test TextKit.

 */

public class TextKitApp {

    public static void main(String[] args){

        System.out.println(TextKit.lineOfStar(4));

        

        try{

         System.out.println(TextKit.lineOfStar(-4));

        }catch(IllegalArgumentException ex){

         System.out.println(“Argument can not be negative.”);

        }

        

        int num = 17;

        System.out.println(“*” + TextKit.pad(num, 4) + “*”);

        

        try{

         System.out.println(“*” + TextKit.pad(num, -4) + “*”);

        }catch(IllegalArgumentException ex){

         System.out.println(“Argument can not be negative.”);

        }        

    }

}

EduPacers
Calculate your paper price
Pages (550 words)
Approximate price: -

Why Work with Us

Top Quality and Well-Researched Papers

We always make sure that writers follow all your instructions precisely. You can choose your academic level: high school, college/university or professional, and we will assign a writer who has a respective degree.

Professional and Experienced Academic Writers

We have a team of professional writers with experience in academic and business writing. Many are native speakers and able to perform any task for which you need help.

Free Unlimited Revisions

If you think we missed something, send your order for a free revision. You have 10 days to submit the order for review after you have received the final document. You can do this yourself after logging into your personal account or by contacting our support.

Prompt Delivery and 100% Money-Back-Guarantee

All papers are always delivered on time. In case we need more time to master your paper, we may contact you regarding the deadline extension. In case you cannot provide us with more time, a 100% refund is guaranteed.

Original & Confidential

We use several writing tools checks to ensure that all documents you receive are free from plagiarism. Our editors carefully review all quotations in the text. We also promise maximum confidentiality in all of our services.

24/7 Customer Support

Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

Our Services

No need to work on your paper at night. Sleep tight, we will cover your back. We offer all kinds of writing services.

Essays

Essay Writing Service

No matter what kind of academic paper you need and how urgent you need it, you are welcome to choose your academic level and the type of your paper at an affordable price. We take care of all your paper needs and give a 24/7 customer care support system.

Admissions

Admission Essays & Business Writing Help

An admission essay is an essay or other written statement by a candidate, often a potential student enrolling in a college, university, or graduate school. You can be rest assurred that through our service we will write the best admission essay for you.

Reviews

Editing Support

Our academic writers and editors make the necessary changes to your paper so that it is polished. We also format your document by correctly quoting the sources and creating reference lists in the formats APA, Harvard, MLA, Chicago / Turabian.

Reviews

Revision Support

If you think your paper could be improved, you can request a review. In this case, your paper will be checked by the writer or assigned to an editor. You can use this option as many times as you see fit. This is free because we want you to be completely satisfied with the service offered.