Results 1 to 16 of 16
Thread: Java Histogram program
- 08-27-2012, 07:42 AM #1
Member
- Join Date
- Aug 2012
- Posts
- 8
- Rep Power
- 0
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.
- 08-27-2012, 08:03 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
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.Java Code:for (int i = 0; i < list.length; i++)
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.
- 08-27-2012, 11:36 AM #3
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!
- 08-28-2012, 10:37 PM #4
Member
- Join Date
- Aug 2012
- Posts
- 8
- Rep Power
- 0
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
So i'm still having problems with getting 10 random numbers that are within range from 0 to 10 for my for loop.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); } }
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
- 08-28-2012, 11:38 PM #5
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.
- 08-30-2012, 03:45 AM #6
Member
- Join Date
- Aug 2012
- Posts
- 8
- Rep Power
- 0
- 08-30-2012, 04:06 AM #7
Re: Java Histogram program
Does it work? Add a println statement that prints out the random numbers as they are generated so you can see their values.am trying to generate the random numbers withing the first for loop
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.
- 08-30-2012, 04:14 AM #8
Member
- Join Date
- Aug 2012
- Posts
- 8
- Rep Power
- 0
- 08-30-2012, 04:22 AM #9
Member
- Join Date
- Aug 2012
- Posts
- 8
- Rep Power
- 0
Re: Java Histogram program
Actually i tried it with this now:
now I get the random numbers within range. My question is how can I display it as the following: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); } }
- 08-30-2012, 04:50 AM #10
Re: Java Histogram program
What output does the program generate now when you execute it?
The assignment says:
Are the random numbers that are printed out in the range 0 to 10?program that generates an arbitrary number of random integers in the range of 0 to 10 inclusive and counts
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 04:58 AM.
If you don't understand my response, don't ignore it, ask a question.
- 08-30-2012, 04:54 AM #11
Member
- Join Date
- Aug 2012
- Posts
- 8
- Rep Power
- 0
- 08-30-2012, 05:03 AM #12
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.
- 08-30-2012, 05:13 AM #13
Member
- Join Date
- Aug 2012
- Posts
- 8
- Rep Power
- 0
Re: Java Histogram program
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.
- 08-30-2012, 05:31 AM #14
Re: Java Histogram program
Now you need to define how your program is going to meet those requirements.These are the 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.
- 08-30-2012, 02:02 PM #15
Member
- Join Date
- Aug 2012
- Posts
- 8
- Rep Power
- 0
- 08-30-2012, 02:26 PM #16
Re: Java Histogram program
Look at how the for statement works. It loops until the control variable exceeds a limiting variable.modify my first for loop to generate the amount of random numbers the user wants?
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
-
Help with histogram
By Conan in forum New To JavaReplies: 5Last Post: 11-02-2011, 03:51 AM -
need help with name histogram
By cachico12 in forum New To JavaReplies: 1Last Post: 05-09-2011, 08:27 AM -
histogram program in java
By deepaktomar in forum New To JavaReplies: 3Last Post: 03-05-2011, 04:27 PM -
histogram
By little_man in forum New To JavaReplies: 4Last Post: 11-13-2010, 01:38 AM -
Java Histogram program.. any adivce or hints wouls be great!!
By Chewart in forum New To JavaReplies: 8Last Post: 11-24-2009, 09:03 PM


LinkBack URL
About LinkBacks
Reply With Quote


Bookmarks