Averaging Score
Suppose you have a class of ten students who took a quiz. The grades (integers in the range 0 to 100) for this quiz are made available to you. so this program tutorial shows us how to determine the class average on the quiz.
What we need to understand about the class average is that it is equal to the sum of the grades divided by the number of students.
so the algorithm below for solving this problem must input each grade, keep track of the total of all grades input, perform the averaging calculation and print the result.
First of all we will need to use the pseudo code to list the actions to execute and specify the order in which these actions should execute.
Pseudo code is an artificial and informal language that helps you develop algorithms. It’s useful for developing algorithms that will be converted to structured Java programs. They are not executed on computers; rather, they merely help you “think out” a program before attempting to write it in a programming language such as Java.
Pseudo code statement
Set total to zero
Set grade counter to one
While grade counter is less than or equal to twenty
Input the next grade
Add the grade into the total
Add one to the grade counter
Set the class average to the total divided by ten
Print the class average
import javax.swing.JOptionPane;
public class AverageProgram{
public static void main( String args[])
{
int total;
int gradeCounter;
int grade;
int average;
String gradeString;
total = 0;
gradeCounter = 1;
while(gradeCounter <=10)
{
gradeString = JOptionPane.showInputDialog("Enter integer grade:");
grade = Integer.parseInt(gradeString);
total = total + grade; //add grade to total
gradeCounter = gradeCounter + 1 ;
}
average = total / 10;
JOptionPane.showMessageDialog(null, "Average is: " + average, "Class Average", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
Comments
Post a Comment
Please feel free to comments if you have any ideas, questions and subjection concerning our post and programs