
The University wants to make a basic graphical display to show how many people received different grades for a piece of work on a module. You are required to write a program in Java that achieves this. The program is in a number of parts.
0-29 *
30-39 *
40-69 **
70-100 **
20 students in total.
â?¢ As the tutor enters each mark, a counter should count the number of studentâ??s marks which have been entered. â?¢ Use the same 4 category ranges shown here. â?¢ Make sure the display is neatly formatted as above. â?¢ Your program should make use of â??loopsâ?? for the display of each category.
Extras: 2. After the histogram, a variety of statistics should be displayed (e.g. average mark awarded, number of students passing, highest mark, and lowest mark) 3. The histogram shows each category horizontally across the screen. Copy your original solution and make changes to the copy to display the histogram vertically (the stars in a category should go downwards and not across the screen). 4. An extra for the very brave!

Here is the answer for this question. I have done it for 20 students you can make it for any number of students by removing the condition i>=20 from the statement if((marks[i]>100)||(i>=20)). The java file is as follows
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import javax.swing.*;
import javax.swing.BorderFactory;
import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.border.Border;
public class ClassStatistics1 extends JApplet implements ActionListener
{
float marks[]=new float[20];
int i=0,ctr=0,ctr1=0,ctr2=0,ctr3=0,passed=0,num=0;
float highest=0, lowest=101,sum=0,avg=0;
JFrame f1=new JFrame();
JLabel label1=new JLabel("Enter marks");
JTextField tf1=new JTextField(10);
private JButton b = new JButton("Add");
public void init()
{
Container cp = getContentPane();
tf1.setEditable(true);
b.setEnabled(true);
cp.setLayout(new FlowLayout());
// Create Borders for components:
Border brd = BorderFactory.createMatteBorder(1, 1, 2, 2, Color.BLACK);
tf1.setBorder(brd);
cp.add(label1);
cp.add(tf1);
cp.add(b);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
marks[i]=Float.parseFloat(tf1.getText());
if(marks[i]>=0&&marks[i]<=29)
ctr++;
else if(marks[i]>=30&&marks[i]<=39)
ctr1++;
else if(marks[i]>=40&&marks[i]<=69)
ctr2++;
else if(marks[i]>=70&&marks[i]<=100)
ctr3++;
if((marks[i]>100)||(i>=20))
{
f1.getContentPane();
f1.setSize(250, 375);
JPanel p=new JPanel();
p.setSize(250,375);
p.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
int k=0,m=6;
JLabel l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,l11,l12;
JLabel []lab=new JLabel[20];
l1=new JLabel("0-29 ");
c.fill=GridBagConstraints.HORIZONTAL;
c.gridx=0;
c.gridy=0;
p.add(l1,c);
while(ctr!=0)
{
m=m+3;
lab[k]=new JLabel("");
lab[k].setText("*");
c.gridx=m;
c.gridy=0;
p.add(lab[k],c);
k++;
ctr--;
}
m=6;
l2=new JLabel("30-39 ");
c.gridx=0;
c.gridy=5;
p.add(l2,c);
while(ctr1!=0)
{
m=m+3;
lab[k]=new JLabel("*");
c.gridx=m;
c.gridy=5;
p.add(lab[k],c);
k++;
ctr1--;
}
m=6;
l3=new JLabel("40-69 ");
c.gridx=0;
c.gridy=10;
p.add(l3,c);
while(ctr2!=0)
{
m=m+3;
lab[k]=new JLabel("*");
c.gridx=m;
c.gridy=10;
p.add(lab[k],c);
k++;
ctr2--;
}
m=6;
l4=new JLabel("70-100 ");
c.gridx=0;
c.gridy=15;
p.add(l4,c);
while(ctr3!=0)
{
m=m+3;
lab[k]=new JLabel("*");
c.gridx=m;
c.gridy=15;
p.add(lab[k],c);
ctr3--;
}
avg=sum/num;
l11=new JLabel("Average marks awarded: ");
c.gridx=0;
c.gridy=20;
p.add(l11,c);
l12=new JLabel("");
l12.setText(Float.toString(avg));
c.gridx=28;
c.gridy=20;
p.add(l12,c);
l8=new JLabel("Passed: ");
c.gridx=0;
c.gridy=25;
p.add(l8,c);
l5=new JLabel("");
l5.setText(Integer.toString(passed));
c.gridx=28;
c.gridy=25;
p.add(l5,c);
l9=new JLabel("Highest: ");
c.gridx=0;
c.gridy=30;
p.add(l9,c);
l6=new JLabel("");
l6.setText(Float.toString(highest));
c.gridx=28;
c.gridy=30;
p.add(l6,c);
l10=new JLabel("Lowest: ");
c.gridx=0;
c.gridy=35;
p.add(l10,c);
l7=new JLabel("");
l7.setText(Float.toString(lowest));
c.gridx=28;
c.gridy=35;
p.add(l7,c);
f1.add(p);
f1.setVisible(true);
}
sum=sum+marks[i];
if(marks[i]>=50)
passed++;
if(marks[i]>=highest)
highest=marks[i];
if(marks[i]<=lowest)
lowest=marks[i];
i++;
tf1.setText("");
num++;
}
}
The html file is as follows
<html>
<applet code="ClassStatistics1.class" width="350" height="350">
</applet>
</html>
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.