public class Histogram
{
private int lo_;
private int hi_;
private int[] frequency_;
public Histogram(int lo, int hi)
{
lo_ = lo;
hi_ = hi;
int range = hi_-lo_+1;
frequency_ = new int[range];
for(int i =0; i <range; range++)
frequency_[i] = 0;
}
public void ReadValue()
{
Scanner in = new Scanner(System.in);
int value= in.nextInt();
while(value != -1)
{
if(value >= lo_ && value <= hi_)
{
frequency_[value - lo_]++;
value = in.nextInt();
}
}
}
private String starPrinter(int value)
{
String star = "*";
for(int i = 0; i <= value ;i++)
{
star +="*";
}
return star;
}
public String Printer()
{
String print = new String();
int range = hi_-lo_+1;
int i = 0;
while(i<range)
{
print += (lo_+i)+" : "+ starPrinter(i)+ "\n";
i++;
}
return print;
}
public int query(int value)
{
if (value >= lo_ && value <= hi_)
{
value -= lo_;
return starPrinter(value).length();
}
else
return -1;
}
public static void main(String[] args)
{
Histogram test = new Histogram(3, 9);
test.ReadValue();
}
}
I need help on this Histogram.
The constructor is generated by low number and high number (so if I put 3 to 9: these are all the numbers it expects, anything else is ignored)
the readValue method will keep looping until the user types -1. Meaning that if I type 3, 4, 6, 4, 6, 9 , 5, 9, 4, 10 -1... then it will store all that in the frequency[]. How do I make it so that each value can be tracked in the frequency[]?
3 occurs once, 4 occurs three times, 7 never occurs, 9 occurs twice
Printer() will make me a Histogram diagram that looks like this (Using the numbers inputted before...)
3: *
4: ***
5: *
6: **
7:
8:
9: **
How do I use the numbers the frequency has to print the number of stars the numbers occured in?
the query method will ask the user for what number they want and tells them how many times it occurs:
types 3 "3 occurs 2 times"
types 10 "10 is out of range."
I have most of the code, I just need help implementing some parts.