...

View Full Version : Help with a test score problem



sonic2012
05-02-2012, 01:55 AM
Ive been trying to write a program that will ask for 5 test scores, give you the average of the scores and also the corresponding grade that goes with them, unfortunately the grade doesn't seem to post when called. Any help would be welcome.



import javax.swing.JOptionPane; //Needed for GUI
import java.text.DecimalFormat; //needed to format the Output


public class avg_Score_Anderson
{//Begin class
public static void main(String[] args)
{//Begin main method

String inputString; //For reader's input
double Score1, //Define score1
Score2, //Define score2
Score3, //Define score3
Score4, //Define score4
Score5, //Define score5
Average ;//Define Average

DecimalFormat formatter = new DecimalFormat("#,##0.0"); //format the scores

// Get the five scores
inputString=
JOptionPane.showInputDialog("Enter the First Test Score: "); //ask user to enter the first test score
Score1 = Double.parseDouble(inputString);

inputString=
JOptionPane.showInputDialog("Enter the Second Test Score: ");//ask user to enter the second test score
Score2 = Double.parseDouble(inputString);

inputString=
JOptionPane.showInputDialog("Enter the third test score: ");//ask user to enter the third test score
Score3 = Double.parseDouble(inputString);

inputString=
JOptionPane.showInputDialog("Enter the fourth test score: ");//ask user to enter the fourth test score
Score4 = Double.parseDouble(inputString);

inputString=
JOptionPane.showInputDialog("Enter the fifth test score: ");//ask user to enter the fifth test score
Score5 = Double.parseDouble(inputString);

// Call to method calcAverage and output the 5 test average
Average = calcAverage(Score1, Score2, Score3, Score4, Score5);



// Display Average test Score and Determine the letter grade for each test and call to determineGrade

JOptionPane.showMessageDialog(null, "\t\nTest score one is : " +formatter.format(Score1) +"\t Grade: " + determineGrade(Score1)
+ "\t\nTest score two is : " +formatter.format(Score2) +"\t Grade: " + determineGrade(Score2)
+ "\t\nTest score three is : " +formatter.format(Score3) +"\t Grade: " + determineGrade(Score3)
+ "\t\nTest score four is : " +formatter.format(Score4) +"\t Grade: " + determineGrade(Score4)
+ "\t\nTest score five is : " +formatter.format(Score5) +"\t Grade: " + determineGrade(Score5)
+ "\n"
+ "\t\nYour average test score is : " +formatter.format(Average) +"\t Grade: " + determineGrade(Average),
"\tYour Test Results",JOptionPane.INFORMATION_MESSAGE);

}//end Main method

// Calculate the average of the five test scores
public static double calcAverage(double Score1, double Score2, double Score3, double Score4, double Score5)
{
double Average = ((Score1 + Score2 + Score3 + Score4 + Score5) / 5);
return Average;
}

// Determine the letter grade for the average and 5 test scores
public static double determineGrade(double Average)
{
char grade; // Define grade

// Determine letter grade
if (Average>=90)
{
grade = 'A';
}
else if (Average>=80)
{
grade = 'B';
}
else if (Average>=70)
{
grade = 'C';
}
else if (Average>=60)
{
grade = 'D';
}
else if (Average<60)
{
grade = 'F';
}
else
{
JOptionPane.showMessageDialog(null, "error\n");
}

return grade;

}//end determinegrade method
}//end class

alykins
05-02-2012, 06:11 PM
are any of the grades posting? have you set break points to step through? i read through and nothing stands out as wrong- are there any exceptions? is the double grade displaying and just the char is not?

sonic2012
05-02-2012, 10:42 PM
the only error is

C:\Desktop\avg_Score_Anderson.java:97: error: variable grade might not have been initialized
return grade;
^
1 error


The only thing that does not post is the letter grade, the actual number grades and average of them all post correctly.

Im not sure if the char is incorrect or if i just skipped a step with the public static double determineGrade(double Average) part of the code.

Thanks again for any help.

alykins
05-02-2012, 11:15 PM
I am surprised it is letting you compile- I found it; errors out in C# :P
you function "determineGrade" is a double- yet you are expecting it to be a char, and you are trying to return a char- you can't implicitly convert a char to a double.

also the variable grade is not necessarily getting initialized- well it is (as it is the end result of a set of if/elseif statements) but in C# anyways it doesn't know that for sure- I was able to modify it by changing all those if/elseif's to a switch and default returning 'E' for error. this also then eliminates the need for the variable grade- or you could set grade to a variable and then return it but it is redundant...


char grade;
.....
grade = 'E';
return grade;
.... same as ....
return 'E';

in short- I would make the set of if's be a switch (you will need to change up your logic a little bit) and the bottom line issue is you are trying to on-the-fly convert a char to a double.

sonic2012
05-03-2012, 02:42 AM
ended up just removing the last else option and making it the letter F, and also changing the base of that statement to a char and seems to run perfect now.

Finished code for anyone interested and thanks for all the help.



import javax.swing.JOptionPane; //Needed for GUI
import java.text.DecimalFormat; //needed to format the Output


public class avg_Score_Anderson
{//Begin class
public static void main(String[] args)
{//Begin main method

String inputString; //For reader's input
double Score1, //Define score1
Score2, //Define score2
Score3, //Define score3
Score4, //Define score4
Score5, //Define score5
Average ;//Define Average

DecimalFormat formatter = new DecimalFormat("#,##0.0"); //format the scores

// Get the five scores
inputString=
JOptionPane.showInputDialog("Enter the First Test Score: "); //ask user to enter the first test score
Score1 = Double.parseDouble(inputString);

inputString=
JOptionPane.showInputDialog("Enter the Second Test Score: ");//ask user to enter the second test score
Score2 = Double.parseDouble(inputString);

inputString=
JOptionPane.showInputDialog("Enter the third test score: ");//ask user to enter the third test score
Score3 = Double.parseDouble(inputString);

inputString=
JOptionPane.showInputDialog("Enter the fourth test score: ");//ask user to enter the fourth test score
Score4 = Double.parseDouble(inputString);

inputString=
JOptionPane.showInputDialog("Enter the fifth test score: ");//ask user to enter the fifth test score
Score5 = Double.parseDouble(inputString);

// Call to method calcAverage and output the 5 test average
Average = calcAverage(Score1, Score2, Score3, Score4, Score5);



// Display Average test Score and Determine the letter grade for each test and call to determineGrade



JOptionPane.showMessageDialog(null, "Test score Grade "
+ "\n----------------------------------"
+ "\n" +formatter.format(Score1) + " " + determineGrade(Score1)
+ "\n" +formatter.format(Score2) + " " + determineGrade(Score2)
+ "\n" +formatter.format(Score3) + " " + determineGrade(Score3)
+ "\n" +formatter.format(Score4) + " " + determineGrade(Score4)
+ "\n" +formatter.format(Score5) + " " + determineGrade(Score5)
+ "\n\n"
+ "Your average test score is : \n" +formatter.format(Average) + " "
+ determineGrade(Average));



}//end Main method

// Calculate the average of the five test scores
public static double calcAverage(double Score1, double Score2, double Score3, double Score4, double Score5)
{
double Average = ((Score1 + Score2 + Score3 + Score4 + Score5) / 5);
return Average;
}

// Determine the letter grade for the average and 5 test scores
public static char determineGrade(double Average)
{
char grade; // Define grade

// Determine letter grade
if (Average>=90)
{
grade = 'A';
}
else if (Average>=80)
{
grade = 'B';
}
else if (Average>=70)
{
grade = 'C';
}
else if (Average>=60)
{
grade = 'D';
}
else
{
grade = 'F';
}


return grade;

}//end determinegrade method
}//end class



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum