Computing.Net > Forums > Programming > Java Code Please Check

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Java Code Please Check

Reply to Message Icon

Name: eMra__
Date: September 7, 2006 at 05:54:27 Pacific
OS: .
CPU/Ram: .
Product: HP Pavilion
Comment:

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"));


}
}



Sponsored Link
Ads by Google

Response Number 1
Name: newslacker
Date: September 7, 2006 at 06:36:49 Pacific
Reply:

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;


0

Response Number 2
Name: eMra__
Date: September 7, 2006 at 07:10:05 Pacific
Reply:

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 program

JOptionPane.showMessageDialog(null,"Result for" + " " + ch + "." + " " + lastName + "\n" + subject + ":" + " " + grade(Sresult));



0

Response Number 3
Name: newslacker
Date: September 8, 2006 at 16:26:56 Pacific
Reply:

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

return "fail";


0

Response Number 4
Name: zoned87
Date: September 15, 2006 at 16:58:04 Pacific
Reply:

You better get it right or else you're ganna fail. :( haha JK. GL!


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Java Code Please Check

Unable to Download Java www.computing.net/answers/programming/unable-to-download-java/14936.html

test java code online www.computing.net/answers/programming/test-java-code-online/12093.html

How EXE java compiler in Java Code? www.computing.net/answers/programming/how-exe-java-compiler-in-java-code/2492.html