Gpa

• 06-28-2012, 03:37 AM
Yokomoko
Gpa
This is the problem:
Create a program called CalculateGPA.java that asks the user to enter grades for each course of a given semester (such as A, B, C, D, F). Enter X to stop entering grades. The program should then calculate the GPA for the semester. Assume that each course has the same number of credit hours. Use the NumberFormat class to format the GPA to two decimal places. Use the Scanner class for input and the command window for output. Entering grades should be handled by one method and displaying the GPA by a different method. Use a switch statement to assign grade points for different grades.

I have created the code:
Code:

```import java.util.Scanner; import java.text.NumberFormat; public class GPA {         public void GetGrades ( )         {                 String gradeStr;                 char grade;                 double gradetotal = 0, counter = 0, GPA;                 Scanner input = new Scanner( System.in );                 NumberFormat numberFormatter = NumberFormat.getNumberInstance();                 System.out.print ("Please enter your grades below."                 + " To exit the program enter X.");                 gradeStr = input.nextLine();                 grade = gradeStr.chartAt(0);         do         {         switch (grade)           {                 case 'A': case 'a':                 gradetotal += 4;                 counter++;                                 case 'B': case 'b':                 gradetotal += 3;                 counter++;                 case 'C': case 'c':                 gradetotal += 2;                 counter++;                 case 'D': case 'd':                 gradetotal += 1;                 counter++;                 case 'F': case 'f':                 gradetotal += 0;                 counter++;           }         }         while(grade != 'X' || grade !='x');         gpamath(GPA, gradetotal, counter, numberFormatter);         }         public static void gpamath ( double GPA, double gradetotal, double counter, NumberFormat numFormatter )         {         GPA = gradetotal/counter;                numFormatter.setMaximumFractionDigits(2);         String gradepointStr = numFormatter.format(GPA);         System.out.print("Your Grade Point Average is " + gradepointStr);         } }```
..but I keep getting an "error: cannot find symbol with the "gradeStr.charAt(0)" The "^" is under the "." before charAt(0) and I cannot figure out how to fix this error.
• 06-28-2012, 04:34 AM
Yokomoko
Re: Gpa
Update: Nevermind I found my typo... But now I cannot get the do...while statement to loop. It just lets me enter one grade and then flashes so the only thing I can do it hit the red X to close cmd.
Code:

```import java.util.Scanner; import java.text.NumberFormat; public class GPA {           public static void main( String [] args )         {                 String gradeStr;                 char grade;                 double gradetotal = 0, counter = 0, GPA;                   final int SENTINEL ='X';                 Scanner input = new Scanner( System.in );                 NumberFormat numberFormatter = NumberFormat.getNumberInstance();                 System.out.print ("Please enter your grades below.\n"                 + " To exit the program enter X.\n");                 gradeStr = input.nextLine();                 grade = gradeStr.charAt(0);         do         {         switch (grade)           {                 case 'A': case 'a':                 gradetotal += 4;                 counter++;                                 case 'B': case 'b':                 gradetotal += 3;                 counter++;                 case 'C': case 'c':                 gradetotal += 2;                 counter++;                 case 'D': case 'd':                 gradetotal += 1;                 counter++;                 case 'F': case 'f':                 gradetotal += 0;                 counter++;           }         }         while(grade != 'X' || grade !='x');         gpamath(gradetotal, counter, numberFormatter);         } public static void gpamath ( double gradetotal, double counter, NumberFormat numFormatter )         {         double GPA = gradetotal/counter;                numFormatter.setMaximumFractionDigits(2);         String gradepointStr = numFormatter.format(GPA);         System.out.print("Your Grade Point Average is " + gradepointStr);         } }```
• 06-28-2012, 04:41 AM
Fubarable
Re: Gpa
For a while loop to work well, you need to be able to change the sentinel value (here the grade variable) inside of the loop. Where do you do this?

Also you'll need to re-think your while loop logic. When is grade ever *not* going to be either not 'x' or not 'X'?
• 06-28-2012, 04:46 AM
Yokomoko
Re: Gpa
Hmm so I need to have a case 'X' and only allow one type of X, so only capital X?
• 06-28-2012, 04:49 AM
Fubarable
Re: Gpa
Quote:

Originally Posted by Yokomoko
Hmm so I need to have a case 'X' and only allow one type of X, so only capital X?

No, you need to think through the logic if using || vs &&. First change your loop so that the user can actually enter a String from within the loop (again, else how would grade be changed from within the loop?). Then you should walk through your code and imagining what would happen if this or that letter were entered, thinking in what condition will the loop ever end?
• 06-28-2012, 04:54 AM
jfabian
Re: Gpa
Is like Fubarable said, into de do...while loop, you need to ask the user again for the next input, that's the way its suppose to go. Another thing, declare a variable to control the flow of the program, you can even compare the user input before give that value into the grade variable. In that way you'll have a better control of whats going on and a unterstabdable-logic code.
• 06-28-2012, 04:57 AM
Yokomoko
Re: Gpa
Ok so I entered a case X inside the loop and also added a sentinel so that when x is entered, the sentinel changes from 0 to 1. But when I try to run this It only lets me enter one grade and then tells me the grade point average wrong. For example it says the GPA for one A is 2 when it should be 4. Sorry if these are stupid questions, I am extremely new to this and have no help outside of forums.
Code:

```                case 'X': case 'x':                 SENTINEL=1;           }         }         while(SENTINEL !=1);```
• 06-28-2012, 05:02 AM
Fubarable
Re: Gpa
no, you have to get user input. What line of code in your main program gets user input? Hint: it's where you use your Scanner object, input.

But yes to your case logic. The other way to solve that is to use && instead of ||. Again, you *must* practice walking logically through your code in your mind if you're going to do well in this class.
• 06-28-2012, 05:03 AM
Yokomoko
Re: Gpa
Update: I got it to loop correctly by moving the input section into the loop, but for some reason the calculation part is messed up. I just need it to divide the total from the grades entered by the amount of grades entered. totalgrades/counter
• 06-28-2012, 05:05 AM
Fubarable
Re: Gpa
One reason things are messing up is that your switch statement is missing a few break statements. Check out the switch tutorial for more on this.
• 06-28-2012, 05:10 AM
Yokomoko
Re: Gpa
Ok I must have skipped the break;'s but it is working correctly now. Thank you guys so much for teaching me what to do here. I really appreciate it.