Monday, December 7, 2009

A java applet which calculates student grades....

Here is the source code for a java applet I have written that calculates any amount of student grades...The applet terminates and calculates the average when you enter -1 :


import javax.swing.*;
import java.awt.Graphics;
import java.text.DecimalFormat;

public class Average5Applet extends JApplet{

int counter = 0;
int total = 0;
int grade;

double average;





public void init()
{



String grades;

grades = JOptionPane.showInputDialog("Enter student grades as integers:");

grade = Integer.parseInt(grades);

while(grade != -1 )

{

counter = counter + 1;
total = total + grade;

grades = JOptionPane.showInputDialog("Enter student grades as integers:");

grade = Integer.parseInt(grades);

}

}

public void paint(Graphics g)
{

super.paint (g);

DecimalFormat twoDigits = new DecimalFormat("0.00");

if ( counter != 0 )

{

average = (double) total / counter;

g.drawString("The average is"+twoDigits.format(average),25,25);

}

else

g.drawString("No grades were entered!!!",25,25);

}

You need to Have JDK 1.6 to compile the applet...

Also you need to execute the applet, write a web page embedding the applet's compiled class file in it....

No comments:

Post a Comment