This program is to input 20 numbers in the range 0 to 100 and display a
histogram that shows how many numbers are in the range 0 to 9, 10 to 19, 20
to 29..
public class Histogram extends Applet
{
private int [] num = {5, 7, 8, 19, 25, 30, 37, 41, 43, 50, 52, 55,
61, 63, 70, 76, 84, 91, 95, 98}; // input array
private int [] hist = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // histogram count
public void drawHist()
{
for( int i = 0; i <num.length; i++)
{
hist[num[i] / 10] += 1;
}
}
public void paint (Graphics g)
{
int x = 20;
int y = 20;
int height = 40;
for(int i = 0; i < hist.length; i++)
{
drawHist();
int width = hist[i];
g.fillRect(x, y, width * 10, height);
x = x + 10;
}
}
}
But i see a solid black rectangle that stretches when the applet window is
dragged. It doesn't look like histogram. I don't know what am i doing wrong.
Any help appreciated,
Kt
Frederik Coppens - 17 Apr 2005 18:50 GMT
You're drawing a number of horizontal lines that overlap.
That's why you see one big rectangle.
Also, you're calling drawHist 10 times. It's enough to call it once, before
the loop.
> This program is to input 20 numbers in the range 0 to 100 and display a
> histogram that shows how many numbers are in the range 0 to 9, 10 to 19,
[quoted text clipped - 39 lines]
> Any help appreciated,
> Kt