#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    5
    Rep Power
    0

    Need help displaying GPA in Java


    Need help displaying GPA correctly in JAVA?
    This is my code I just want to display the GPA at the end

    Code:
    import java.util.Scanner;
    
    public class Grades
    {
    
    public void showPoints()
    {
    Scanner input = new Scanner( System.in );
    
    System.out.print( "Enter grades (-1 to quit): ");
    int inputNumber = input.nextInt();
    
    while ( inputNumber != -1 )
    {
    if ( inputNumber >= 0 && inputNumber <= 100 )
    System.out.printf( " Quality Point is: %d\n",
    qualityPoints( inputNumber ));
    else
    System.out.println( "Invalid number entered." );
    
    System.out.print( "Enter grades (-1 to quit): " );
    inputNumber = input.nextInt();
    
    
    }
    
    }
    
    public int qualityPoints( int grade )
    {
    if ( grade >= 90 )
    return 4;
    else if ( grade >= 80 )
    return 3;
    else if ( grade >= 70 )
    return 2;
    else if ( grade >=60 )
    return 1;
    else
    return 0;
    
    }

    Example of how I want my output to look:

    Enter grade: 90
    4
    Enter grade: 89
    3
    Enter grade: 70
    2
    Enter grade: 60
    1
    Enter grade: -1


    GPA 2.5.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    103
    Rep Power
    3
    That's a really good start. You have everything you need to calculate GPA in there. You just need to make it overlap between the user entering input. Keep a running total of quality points and the number of grades entered. Then, when you want to display GPA just output TotalQualityPoints/NumberOfGrades.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    5
    Rep Power
    0
    I changed it up a bit but I am still getting errors. can you look at this one and tell what I did wrong.

    Code:
    import java.util.Scanner;
    public class AverageGrade
    {
       public static void main(String args[])
       {
         Scanner input = new Scanner(System.in);
         int testScore, total, count;
         double average;
         total = 0;
         count = 0;
         System.out.print("Enter a percentage score for the course");
         System.out.print(" (-1 to quit) : ");
         testScore = input.nextInt();
    
         while (testScore != -1)
         {  
         if ( testScore <= 0 && testscore <= 100 )
    	   System.out.printf("Quality Point is: %d\n", qualityPoints( testScore ));
            
    		total += testScore;
    		++count;
    		System.out.print("Enter a percentage score for the course");
    		System.out.print(" (-1 to quit): ");
    		testScore = input.nextInt();	
         }
         public int qualityPoints(testScore)
        {
        if ( grade >= 90 )
    		return 4;
    	else if ( grade >= 80 )
    		return 3;
    	else if ( grade >= 70 )
    		return 2;
        else if ( grade >=60 )
    		return 1;
    	else
    		return 0;
        }
      
    }
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    103
    Rep Power
    3
    Let's deal with your qualityPoints method before even touching the Main method. Your not specifying a type of variable that is being passed in as a parameter for your method. Java requires you to specify which type of variable your method is expecting to receive.

    e.g. Notice how there is a variable type before each parameter in the methods I declare.
    Code:
    public void methodA(String parameter) {
    
    }
    
    public void methodB(int parameter) {
    
    }
    The other problem is you make reference to a variable called grade in your method. However, there is no grade variable available to you in that method. There is something called scope that says that a variable is only available to you between the parenthesis { } that it is declared. There are exceptions to this, but you don't need to worry about them.

    e.g.
    Code:
    public static void main(String[] args) {
      int grade;
      System.out.println(grade);
      // works fine because I have access to grade (its in scope)
    }
    
    public static void printGrade() {
      System.out.println(grade);
      // Error here because I don't have access to grade (outside of scope)
    }
    There is another problem with your QualityPoints method, but I am hoping you will realize what it is after understanding this post. As a helpful hint to solving the problem: What variable do you have access to in your QualityPoints method?
    Last edited by Cameron0960; July 10th, 2013 at 08:53 AM.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    5
    Rep Power
    0
    I am getting 9 errors on this one.
    Code:
    import java.util.Scanner;
    public class GradePoint2
    {
       public void addPoints()
       {
    
        int grade, total, count;
        double average = 0.0;
        total = 0;
        count = 0;
        Scanner input = new Scanner (System.in);
        System.out.print("Enter grade (-1 to quit): ");
        int inputGrade = input.nextInt();
        while (inputGrade != -1)
        {
         if (inputGrade >= 0 && inputGrade <= 100)
     System.out.printf("Point is: %d\n", returnPoints
    
    (inputGrade));
         else
     System.out.println("Invalid input.");
        System.out.print("Enter grade (-1 to quit): "); 
        inputGrade = input.nextInt();
        total += grade;
        ++count;
       }
    }
    public int returnPoints(int grade)
    {
       if (grade >=90)
     return 4;
       else if (grade >=80)
     return 3;
       else if (grade >= 70)
     return 2;
       else if (grade >= 60)
     return 1;
       else
     return 0;
    }
    
    average = total / count;
    System.out.printf("\nThe GPA is : " + average );
    }
    These are the errors I am getting now.

    GradePoint2.java:40: error: <identifier> expected
    average = total / count;
    ^
    GradePoint2.java:41: error: <identifier> expected
    System.out.printf("\nThe GPA is : " + average );
    ^
    GradePoint2.java:41: error: illegal start of type
    System.out.printf("\nThe GPA is : " + average );
    ^
    GradePoint2.java:41: error: ')' expected
    System.out.printf("\nThe GPA is : " + average );
    ^
    GradePoint2.java:41: error: ';' expected
    System.out.printf("\nThe GPA is : " + average );
    ^
    GradePoint2.java:41: error: illegal start of type
    System.out.printf("\nThe GPA is : " + average );
    ^
    GradePoint2.java:41: error: <identifier> expected
    System.out.printf("\nThe GPA is : " + average );
    ^
    GradePoint2.java:41: error: ';' expected
    System.out.printf("\nThe GPA is : " + average );
    ^
    GradePoint2.java:42: error: reached end of file while parsing
    }
    ^
    9 errors

  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    103
    Rep Power
    3
    Code:
    average = total / count;
    System.out.printf("\nThe GPA is : " + average );
    These lines need to be wrapped within a method. You created and are referring to the variable average in the addPoints method.

    Nice job on fixing the returnPoints method
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    5
    Rep Power
    0
    Thank you so much for your help. You are awesome. The program runs good but my calculations for the GPA is off..This is the output I am receiving:

    java GradePoint2Test
    Enter grade (-1 to quit): 98
    Point is: 4
    Enter grade (-1 to quit): 100

    The GPA is : 100.0Point is: 4
    Enter grade (-1 to quit): 97

    The GPA is : 98.0Point is: 4
    Enter grade (-1 to quit): 95

    The GPA is : 97.0Point is: 4
    Enter grade (-1 to quit): 99

    The GPA is : 97.0Point is: 4
    Enter grade (-1 to quit): -1

    The GPA is : 78.0
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    103
    Rep Power
    3
    In your original post you said you wanted to calculate the GPA using points, not grades. Take a look at what information you are adding to your total variable whenever the user enters input.

    You already have your returnPoints method giving back your desired value. *hint*
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    5
    Rep Power
    0

    Thanks


    Thank so much for your help. I was missing another calculation in my program and that was the number of credits per each course. It all worked out thanks again for helping notice the error of my ways.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    103
    Rep Power
    3
    Originally Posted by angoods0544
    Thank so much for your help. I was missing another calculation in my program and that was the number of credits per each course. It all worked out thanks again for helping notice the error of my ways.
    No worries I'm glad it worked out for ya.

IMN logo majestic logo threadwatch logo seochat tools logo