Results 1 to 11 of 11
Like Tree2Likes
  • 1 Post By Fubarable
  • 1 Post By Yokomoko

Thread: Gpa

  1. #1
    Yokomoko is offline Member
    Join Date
    Jun 2012
    Posts
    29
    Rep Power
    0

    Default 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:
    Java 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.

  2. #2
    Yokomoko is offline Member
    Join Date
    Jun 2012
    Posts
    29
    Rep Power
    0

    Default 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.
    Java 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);
    	}
    }

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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'?
    jfabian likes this.

  4. #4
    Yokomoko is offline Member
    Join Date
    Jun 2012
    Posts
    29
    Rep Power
    0

    Default Re: Gpa

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

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Gpa

    Quote Originally Posted by Yokomoko View Post
    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?

  6. #6
    jfabian is offline Member
    Join Date
    Jun 2012
    Posts
    22
    Rep Power
    0

    Default 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.

  7. #7
    Yokomoko is offline Member
    Join Date
    Jun 2012
    Posts
    29
    Rep Power
    0

    Default 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.
    Java Code:
    		case 'X': case 'x':
    		SENTINEL=1;
    	   }
    	 }
    	while(SENTINEL !=1);

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  9. #9
    Yokomoko is offline Member
    Join Date
    Jun 2012
    Posts
    29
    Rep Power
    0

    Default 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

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  11. #11
    Yokomoko is offline Member
    Join Date
    Jun 2012
    Posts
    29
    Rep Power
    0

    Default 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.
    Vinx likes this.

Posting Permissions

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