I am trying to create a histogram class that utilizes an application to run the program.
public class Histogram
{
String name;
int max;
public Histogram (String name, int max)
{
this.name = name;
this.max = max;
}
int[] count = new int[max+1];
public void add(int numbers)
{
// Handles out of bounds case
if (numbers < 0 || numbers > max)
return;
count[numbers]++;
}
public String toString()
{
String result = name + "\n";
for (int index = 0; index < count.length; index++)
{
result = result + count[index] + ": ";
for (int indexStar = 0; indexStar < count[index]; indexStar++)
result = result + "*";
result = result + "\n";
}
return result;
}
}
My application:
public class HistogramDataCollector
{
public static void main (String[] args)
{
Histogram h = new Histogram ("Favorite Number Survey", 11);
h.add(1); h.add(0); h.add(9); h.add(7); h.add(7);
h.add(3); h.add(4); h.add(6); h.add(5); h.add(2);
h.add(2); h.add(8);
System.out.println(h);
}
}
I am struggling to figure out how to add the int numbers to get the frequency of their occurrences to formulate a histogram created with asterisks. Thanks!
The first thing you need to do is get rid of that extraneous for
loop code that is outside of all of your methods in your class. This won't allow your code to compile. However, once you remove that code, this class should work straight up. You can also get rid of the numbers
array as it is not being used in your class. Just use the count
variable, and you'll see why.
Also another note, you need to change your constructor and your variable definitions so that they look like this:
String name;
int max;
int[] count;
public Histogram(String name, int max)
{
this.name = name;
this.max = max;
this.count = new int[max+1];
}
The reason why is because max
is assumed to be 0 when you create an object of this class. As such, this would declare an array of size 1, and it would not change when you read in the max
variable in your constructor. As such, you need to leave this as unallocated, then allocate the array inside the constructor.
As you mentioned, you are confused with how to get the frequency of their occurrences from this histogram class. Remember the definition of what a histogram is. The histogram is the frequency of numbers that you see in your data. The count
array in your class will automatically store the frequency or how many times you see that particular number in your data set. You use the add()
method and the input into this method is the number you are observing to be placed in your histogram.
As an example with how a histogram works, if count[0] = 1, count[1] = 2, count[2] = 4
, this means that the number of times you have seen 0
is 1 time, 1
2 times, and 2
4 times in your dataset. As such, you simply have to iterate over every element of your count array to retrieve the frequencies of each number. Given your example above in your tester class, after you call the various add methods, your frequency counts for each number would be: count[0] = 1, count[1] = 1, count[2] = 2, count[3] = 1, count[4] = 1, count[5] = 1, count[6] = 1, count[7] = 2, count[8] = 1, count[9] = 1, count[10] = 0
.
Each location in count
is how many times you see that number. As such, for the add()
method, numbers
is the number that you are seeing. As such, you access that particular slot that belongs to that number, and increment the count by 1. For example, doing h.add(0);
would then be equivalent to count[0]++;
which is the same as count[0] = count[0] + 1;
. Access slot 0
, and increment by 1. We have seen this 1
time. For subsequent calls to h.add
, we would see that particular number an additional time.
However, your add method does not perform error checking. Specifically, should you specify a number that is less than 0 or larger than max
, you will get an OutOfBoundsException
when you try to increment the bins of your histogram. As such, I would recommend you simply return from the method without doing anything should this happen. In other words:
public void add(int numbers) {
// Handles out of bounds case
if (numbers < 0 || numbers > max)
return;
count[numbers]++;
}
In your class definition, doing int count[] = new int[max+1];
will automatically assign all elements to this array to 0. max
would be the highest possible number you see in your dataset. Make sure you add 1
to account for the 0 index, which is why it's allocating for max+1
slots. We do this to ensure that we are able to log the frequency of numbers for every possible number that is seen in your dataset. When you create an object from this class, all of those bins will be reset to 0, and so you can start using the object right away.
One thing I would like to note is that you said you wanted to implement this histogram by showing asterisks to represent the bin counts. As such, you would need to modify your toString()
method to be like this:
public String toString()
{
String result = name + "\n";
for (int index = 0; index < count.length; index++) {
result = result + index + ": "; // New
for (int indexStar = 0; indexStar < count[index]; indexStar++) // New
result = result + "*"; // New
result = result + "\n"; // New
}
return result;
}
What the above code will do is that for each bin that is in your histogram, it will show the frequency count of the number, place a colon after, a space, then place as many asterisks as there are for the frequency of that particular number. Therefore, if you consider my first example, you should see:
0: *
1: **
2: ****
Note that I'm just showing what the frequency counts look like. I haven't properly created an object of this class, so the name of the histogram is undefined. However, this is what you should see for the histogram frequency counting.
Hope this solves your question!
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments