Marks barchart

In this example a text field is used to enter marks between 0 and 100. As they are entered the marks is converted to an award and the total number of each award is displayed as a bar chart.

  • How many marks are needed for each award?
  • There is no data verification. Can you change the code so that only marks in the correct range are accepted?

import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;

public class Calculate extends Applet implements ActionListener
{

int a,b,c,d,studentMark;
TextField mark;

public void init()
{

mark=new TextField (3);
add(mark);
mark.addActionListener(this);

}
public void paint(Graphics g)
{

g.drawString("Awards Tally", 30, 50);
g.drawString ("A: ",50,70);
g.fillRect(62, 60,a*10,10);
g.drawString ("B: ",50,90);
g.fillRect(62, 80,b*10,10);
g.drawString ("C: ",50,110);
g.fillRect(62, 100,c*10,10);
g.drawString ("D: ",50,130);
g.fillRect(62, 120,d*10,10);

}

public void actionPerformed(ActionEvent event)
{

studentMark = Integer.parseInt(mark.getText());
if (studentMark>84)
a=a+1;

else
if (studentMark>69)
b=b+1;

else
if (studentMark>49)
c=c+1;

else
d=d+1;

repaint();

}

}

The output looks like this:

barchart.JPG
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License