Results 1 to 16 of 16
  1. #1
    rodjav is offline Member
    Join Date
    Aug 2012
    Posts
    8
    Rep Power
    0

    Post Java Histogram program

    So I have to create a program that generates an arbitrary number of random integers in the range of 0 to 10 inclusive and counts how many occurrences of each value have been generated.

    Example: The user requests that 10 values be generated. The program generates 10 random integer values which are in the range of 0 to 10, such as: 0, 2, 5, 2, 2, 8, 3, 5, 6, and 5. The array stores the count of occurrences (the number of times each value was found).


    Requirements:
    1. Your program should have one class: NumberHistogram.
    2. NumberHistogram class should contain a main method that meets the following requirements:
    a. Use a standard one-dimensional array (not an array list) to represent the integers from 0 to 10. The count of occurrences will be stored in this array.
    b. Use JOptionPane to accept the number of random values to be generated. (See Figure 1 below)
    c. Use a Random object within a loop to produce the random integer values. (Note: You must use the Random class in the java.util package, not the Math.Random method. Examples of using the Random class can be found in Chapter 6 of your textbook.)
    d. Update the count in the appropriate array element when each random integer value is generated.
    e. Use JOptionPane or JTextField to print out the integers generated, their counts, and the histogram as shown in Figure 2 below. Use the String.Format method to line up the columns (see Chapter 4 in your textbook for the formatting characters to use with String.Format).
    3. You may not use any “magic numbers” in this program. Use constants for any values that will not change.

    Java Code:
    import java.util.Random;
    import javax.swing.JOptionPane;
    
    public class NumberHistogram 
    {
        public static void main(String[] args)
        {
            
            // Declare the integer range
            final int MAX_INT = 10;
            final int MIN_INT = 0;
            final int RANGE = 10;
        
            // Standard one-dimensional array to represent the integers from 0 to 10
            int[] list = new int[MAX_INT]; 
        
            // Prompt user and ask how many random values they want to generate
            String input = JOptionPane.showInputDialog("How many random values do you want?");
            int numValues = Integer.parseInt(input);
            
            // Use a random object within a loop to produce the random integer 
            Random generator = new Random();
            
            for (int i = 0; i < list.length; i++)
            {
                list[i]=0;
                int randNum = generator.nextInt();
                System.out.println(randNum);
            }
                 
    
        
        }
        
    }

    Problems I am having so far:
    1) I think i need to create more arrays to store the number of value and the count of occurences?
    2) When I run the program the numbers are not within the range. This has to do with my for loop i assume.

    Any help is appreciated
    I'll be honest and I have been rusty since I haven't programmed in a while.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Java Histogram program

    I think you may have misunderstood what the array is being used for. The assignment wording - "to represent the integers from 0 to 10" - is a little odd. What is really meant is that that the array is meant to store the counts for each of the integers.

    Bearing this in mind, the for loop looks wrong.

    Java Code:
    for (int i = 0; i < list.length; i++)
    This will loop list.length times (ie 10 times). But what you are supposed to do is loop the number of times the user wants: ie, numValues.

    Inside the loop you are not supposed to print anything. (You may just be doing this to see what is going on which is OK) Rather you should include a line that updates the value in the array that corresponds to the count of the random number that has just been generated.

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Java Histogram program

    Carefully consider the requirement of generating and counting random numbers in range 0 to 10 inclusive. For example, you've allocated an array with MAX_INT elements. But that will have indexes from 0 to MAX_INT - 1. Also carefully read the Javadoc for Random (Java Platform SE 6).
    Get in the habit of using standard Java naming conventions!

  4. #4
    rodjav is offline Member
    Join Date
    Aug 2012
    Posts
    8
    Rep Power
    0

    Default Re: Java Histogram program

    Alright so I have been working on the program and so far I think I have most of it right except the range of the numbers. When I run the program it does not run it within the range ( from numbers 0 to 10) . If you guys can help me with that?

    This is my new revised code

    Java Code:
    import java.util.Random;
    import javax.swing.JOptionPane;
    import javax.swing.JTextArea;
    
    public class NumberHistogram 
    {
        public static void main(String[] args)
                
        {
            
            // Declare the integer range
            final int MAX_INT = 11;
            final int MIN_INT = 0;
            final int RANGE = 10;
        
            // Standard one-dimensional array to represent the integers from 0 to 10
            int[] list = new int[MAX_INT]; 
            
            String output = "Number Value\tCount of Occurrences\tHistogram";
            
            // Prompt user and ask how many random values they want to generate
            String input = JOptionPane.showInputDialog("How many random values do you want?");
            int numValues = Integer.parseInt(input);
            
            // Use a random object within a loop to produce the random integer 
            Random generator = new Random();
            
            for (int i = 0; i < list.length; i++)
            {
                output += "\n" + i + "\t" + list[i] + "\t";
                
                // print bar of asteriks
                for (int j = 0; j < list[i]; j++ )
                {
                    output += "*";
                }
            }
            
            JTextArea outputArea = new JTextArea();
            outputArea.setText(output);
            
            JOptionPane.showMessageDialog(null, outputArea,
                    "Message", JOptionPane.INFORMATION_MESSAGE);
    
        }
        
    }
    So i'm still having problems with getting 10 random numbers that are within range from 0 to 10 for my for loop.

    Any help on that guys? It's frustrating me and I'm pretty sure i'm overlooking something simple..


    Use a Random object within a loop to produce the random integer values. Note: You must use the Random class in the java.util package

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Java Histogram program

    Where are you trying to generate random numbers? I see the code creates an instance of the Random class but it doesn't use it anywhere. You have to call one of its methods to get random numbers. Which of the Random class's methods have you tried?
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    rodjav is offline Member
    Join Date
    Aug 2012
    Posts
    8
    Rep Power
    0

    Default Re: Java Histogram program

    Quote Originally Posted by Norm View Post
    Where are you trying to generate random numbers? I see the code creates an instance of the Random class but it doesn't use it anywhere. You have to call one of its methods to get random numbers. Which of the Random class's methods have you tried?

    I am trying to generate the random numbers withing the first for loop. In line 28

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Java Histogram program

    am trying to generate the random numbers withing the first for loop
    Does it work? Add a println statement that prints out the random numbers as they are generated so you can see their values.

    See the code in post#1. What did it print out when it executed?
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    rodjav is offline Member
    Join Date
    Aug 2012
    Posts
    8
    Rep Power
    0

    Default Re: Java Histogram program

    Quote Originally Posted by Norm View Post
    Does it work? Add a println statement that prints out the random numbers as they are generated so you can see their values.

    See the code in post#1. What did it print out when it executed?

    I tried it with the code in post #1 and this was what I got:

    -1075058639
    708275956
    777336319
    -1154017311
    1881461374
    1655607060
    1357858709
    459279807
    1084986981
    -988042753
    -1832815585

  9. #9
    rodjav is offline Member
    Join Date
    Aug 2012
    Posts
    8
    Rep Power
    0

    Default Re: Java Histogram program

    Actually i tried it with this now:
    Java Code:
    import java.util.Random;
    import javax.swing.JOptionPane;
    import javax.swing.JTextArea;
    
    public class NumberHistogram 
    {
           // Declare the integer range
           public static final int MAX_INT = 11;
           public static final int MIN_INT = 0;
           public static final int RANGE = 10;
         
        public static void main(String[] args)
                
        {
           
            // Standard one-dimensional array to represent the integers from 0 to 10
            int[] list = new int[MAX_INT]; 
            
            String output = "Number Value\tCount of Occurrences\tHistogram";
            
            // Prompt user and ask how many random values they want to generate
            String input = JOptionPane.showInputDialog("How many random values do you want?");
            int numValues = Integer.parseInt(input);
            
            // Use a random object within a loop to produce the random integer 
            Random generator = new Random();
            
            for (int i = 0; i < list.length; i++)
            {
                list[i]=0;
                int randNum = generator.nextInt(numValues);
                System.out.println(randNum);
                output += "\n" + i + "\t" + list[i] + "\t" + "\t|";
                
                // print bar of asteriks
                for (int j = 0; j < list[i]; j++ )
                {
                    output += "*";
                }
            }
            
            JTextArea outputArea = new JTextArea();
            outputArea.setText(output);
            
            JOptionPane.showMessageDialog(null, outputArea,
                    "Message", JOptionPane.INFORMATION_MESSAGE);
    
        }
        
    }
    now I get the random numbers within range. My question is how can I display it as the following:

    Java Histogram program-histo.png

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Java Histogram program

    What output does the program generate now when you execute it?

    The assignment says:
    program that generates an arbitrary number of random integers in the range of 0 to 10 inclusive and counts
    Are the random numbers that are printed out in the range 0 to 10?

    You are confusing the range of numbers(0 to 10) with the number of numbers to generate (given by the user).

    You can't generate the report until AFTER all the numbers are generated and counted. The posted code tries to do it all in the same loop.
    Last edited by Norm; 08-30-2012 at 05:58 AM.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    rodjav is offline Member
    Join Date
    Aug 2012
    Posts
    8
    Rep Power
    0

    Default Re: Java Histogram program

    This:

    Java Histogram program-histo2.png

    I need to update the count of occurrences and print the stars.

    For example, if this were my 10 values:
    5
    7
    3
    2
    2
    5
    3
    1
    2
    5
    5


    For the number value 5 I would need to have 4 under the label "number of occurrences" and print four stars.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Java Histogram program

    Are the random numbers that are printed out in the range 0 to 10?

    You are confusing the range of numbers(0 to 10) with the number of numbers to generate (given by the user).

    You can't generate the report until AFTER all the numbers are generated and counted. The posted code tries to do it all in the same loop.


    What is the logic for the program for it to do what you want? Can you explain the steps the program must do to get the report you want? Make a list of the steps needed before writing any more code. You need a good design for the code before writing it,
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    rodjav is offline Member
    Join Date
    Aug 2012
    Posts
    8
    Rep Power
    0

    Default Re: Java Histogram program

    Quote Originally Posted by Norm View Post
    Are the random numbers that are printed out in the range 0 to 10?

    You are confusing the range of numbers(0 to 10) with the number of numbers to generate (given by the user).

    You can't generate the report until AFTER all the numbers are generated and counted. The posted code tries to do it all in the same loop.


    What is the logic for the program for it to do what you want? Can you explain the steps the program must do to get the report you want? Make a list of the steps needed before writing any more code. You need a good design for the code before writing it,
    These are the requirements:
    1. Your program should have one class: NumberHistogram.
    2. NumberHistogram class should contain a main method that meets the following requirements:
    a. Use a standard one-dimensional array (not an array list) to represent the integers from 0 to 10. The count of occurrences will be stored in this array.
    b. Use JOptionPane to accept the number of random values to be generated. (See Figure 1 below)
    c. Use a Random object within a loop to produce the random integer values. (Note: You must use the Random class in the java.util package, not the Math.Random method. Examples of using the Random class can be found in Chapter 6 of your textbook.)
    d. Update the count in the appropriate array element when each random integer value is generated.
    e. Use JOptionPane or JTextField to print out the integers generated, their counts, and the histogram as shown in Figure 2 below. Use the String.Format method to line up the columns (see Chapter 4 in your textbook for the formatting characters to use with String.Format).
    3. You may not use any “magic numbers” in this program. Use constants for any values that will not change.

    The random numbers are printed in the range of 0 to 10. The random numbers needed to be generated are 10 values.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Java Histogram program

    These are the requirements:
    Now you need to define how your program is going to meet those requirements.
    What are the steps the code needs to do? The requirements are pretty close to being a list of the steps, but there are some hles that need to be filled in.

    That's it for tonight. Back tomorrow.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    rodjav is offline Member
    Join Date
    Aug 2012
    Posts
    8
    Rep Power
    0

    Default Re: Java Histogram program

    Quote Originally Posted by Norm View Post
    Now you need to define how your program is going to meet those requirements.
    What are the steps the code needs to do? The requirements are pretty close to being a list of the steps, but there are some hles that need to be filled in.

    That's it for tonight. Back tomorrow.

    How would i have to modify my first for loop to generate the amount of random numbers the user wants?

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Java Histogram program

    modify my first for loop to generate the amount of random numbers the user wants?
    Look at how the for statement works. It loops until the control variable exceeds a limiting variable.
    See the tutorial: The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Help with histogram
    By Conan in forum New To Java
    Replies: 5
    Last Post: 11-02-2011, 04:51 AM
  2. need help with name histogram
    By cachico12 in forum New To Java
    Replies: 1
    Last Post: 05-09-2011, 09:27 AM
  3. histogram program in java
    By deepaktomar in forum New To Java
    Replies: 3
    Last Post: 03-05-2011, 05:27 PM
  4. histogram
    By little_man in forum New To Java
    Replies: 4
    Last Post: 11-13-2010, 02:38 AM
  5. Replies: 8
    Last Post: 11-24-2009, 10:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •