Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    May 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Help with a test score problem

    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.

    Code:
    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
    Last edited by sonic2012; 05-03-2012 at 01:43 AM.

  • #2
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,717
    Thanks
    41
    Thanked 191 Times in 190 Posts
    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?

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #3
    New to the CF scene
    Join Date
    May 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,717
    Thanks
    41
    Thanked 191 Times in 190 Posts
    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...
    Code:
    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.

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #5
    New to the CF scene
    Join Date
    May 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile

    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.

    Code:
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •