Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi guys...
could someone check my code... It is suppose to take results from student 3 times and returns the average...
I also want to add (which is what i'm having trouble with) method inside the main method, which takes the result and returns the mark as a string... marks are either (pass,credit,fail, high distincition, distinction)Here is the program
/**
* Program Description: This program will calculate the average of a student over 3 subjects and displays their grade
*
* By Amro Qandour
* Date: 31.08.2006
*/
import javax.swing.JOptionPane ;
import java.text.DecimalFormat;
public class StudentGrades
{
public static void main(String[]args)
{
final int limit=3;
double avrg=0;
double sum=0;
double result=0;
String doItAgain;
String grades= grade(result);
do
{
String lastName=JOptionPane.showInputDialog("Enter family name:");
String firstName=JOptionPane.showInputDialog("Enter first name:");
String firstUpper=firstName.toUpperCase();
char ch;
int index=0;
ch=firstUpper.charAt(0);
for (int count=1; count<=limit; count++)
{
String subject=JOptionPane.showInputDialog("Enter subject:");
String sResult=JOptionPane.showInputDialog("Enter result:");
result=Double.parseDouble(sResult);
if (result > 0 && result < 50)
{
JOptionPane.showMessageDialog(null,"Result for" + " " + ch + "." + " " + lastName + "\n" + subject + ":" + " " + grades);
}
else
if (result >=50 && result < 60)
{
JOptionPane.showMessageDialog(null,"Result for" + " " + ch + "." + " " + lastName + "\n" + subject + ":" + " " + grades);
}
else
if (result >=60 && result < 70)
{
JOptionPane.showMessageDialog(null,"Result for" + " " + ch + "." + " " + lastName + "\n" + subject + ":" + " " + grades);
}
else
if (result >=70 && result < 80)
{
JOptionPane.showMessageDialog(null,"Result for" + " " + ch + "." + " " + lastName + "\n" + subject + ":" + " " + grades);
}
else
if (result >=80 && result <=100)
{
JOptionPane.showMessageDialog(null,"Result for" + " " + ch + "." + " " + lastName + "\n" + subject + ":" + " " + grades);
}
result=sum + result;
}
DecimalFormat fmt= new DecimalFormat(".##");
avrg=result/3;
JOptionPane.showMessageDialog(null,"Average for" + " " + ch + "." + lastName + " " + "is:" + "\n" + fmt.format(avrg) );
doItAgain=JOptionPane.showInputDialog("Another student (y/n)?");
}
while (doItAgain.equalsIgnoreCase("y"));
}
}

Your code seems fine from what I can tell. When I compile though, I get an error saying symbol 'grade' cannot be found. It looks like you went this to be a method. Did you forget to inlude this in the post?
Also, it seems that even if it had been included, your program would not work as intended anyway, and I know why. When you first declare grades, you set it equal to grade(result). Why? No grade has been input yet, so this is pointless. grades will be initialized to whatever grade(0) returns. From what I see, the string grades is not needed at all.
When you output the name and the grade, I assume you get whatever grade(0) returns. Change this to...
JOptionPane.showMessageDialog(null,"Result for" + " " + ch + "." + " " + lastName + "\n" + subject + ":" + " " + grade(result));
Also, to get the correct average, you need to keep a running total of what the total number of points is. This is what I assume 'sum' is for? This needs to be done as soon as result is input. So... changing the relative parts to...
result=Double.parseDouble(sResult);
sum+=result;
if (result > 0 && result < 50)
{
JOptionPane.showMessageDialog(null,"Result for" + " " + ch + "." + " " + lastName + "\n" + subject + ":" + " " + grade(result));...should fix the problems. Change the part where you calculate the average to...
avrg=sum/3;

thanks for your quick reply newslaker :) i appreciate it...
I'm having trouble passing the result to a new method and evaluating it and returns a string of this result called grade(could be anything) and it is evaluated according to
0-49 - > Fail
50-59 - > Pass
60-69 - > credit
70-79 - > Distinction
80-89 - > High Distinction
Would you have it like this (without writing the whole code again)...String grades=grade(Sresult)
public static grade(mark)
if result > 0 && result < 50
return("fail");
.
.
.---------------
then in the main programJOptionPane.showMessageDialog(null,"Result for" + " " + ch + "." + " " + lastName + "\n" + subject + ":" + " " + grade(Sresult));

yeah, thats fine. But still, you dont need to declare String grades. And grade should be...
return "fail";

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |