Write a program to calculate a students GPA.
What I cant figure is how to calculate ( Total= credit * grade + credit * grade...etc)

or

(GPA = Total/grade +grade etc...)

If you run this program, you will see my calculation off by a lot.
Please help.

import java.util.Scanner;
 import java.io.*;


  public class GPA
  {

 private int total;
 private int credits;
 private int grades;
 private double gpa;
 private int gradeCounter1;
 private int creditCounter2;



  public static void main(String[] args)throws IOException
  {
 	Scanner key = new Scanner(System.in);
 	System.out.println("How many classes will you enter?");
 	int amount = key.nextInt();

 	key.nextLine();

 	int number;
 	int total = 0;
 	int credits = 0;
 	double gpa = 0;
 	int gradeCounter1 = 0;
 	int creditCounter2 = 0;
 	String className;




 	for
 		(number = 1;number <=amount;number++)
 		{

 		System.out.println("Enter Class Name and Course Number:");
 		 className = key.nextLine();

 		System.out.println("Enter Grade:" );

 		String grade;
 		grade = key.nextLine();
 		char letter = grade.charAt(0);
 		gradeCounter1++;

 		System.out.println("How many Credits is this class?");
 		 credits = key.nextInt();
 		 creditCounter2++;
 		 

 		key.nextLine();
 		System.out.println("Class\tGrade \tCredits");

 		System.out.println(className  + "\t"+ letter + "\t" + credits);

 		total += creditCounter2*gradeCounter1 ;
 		gpa  = total/(number*letter);

 		 if((letter =='Z') || (letter  == 'z'))
		 		{
		 		System.out.println("Try Again");
		 	    }
		 		else if((letter == 'A') || (letter == 'a'))
		 		{
		 		gpa = (gpa + 4.0) / number;
		 		}
		 		else if((letter == 'B') || (letter == 'b'))
		 		{
		 		gpa = (gpa + 3.0) / number;
		 		}
		 		else if((letter == 'C') || (letter == 'c'))
		 		{
		 		gpa = (gpa + 2.0) / number;
		 		}
		 		else if((letter == 'D') || (letter == 'd'))
		 		{
		 		gpa = (gpa + 1.0) / number;
		 		}
		 		else if((letter == 'F') || (letter == 'f'))
		 		{
		 		gpa = (gpa + 0.0) / number;
		 		}

		 		}





 		 System.out.println("Total grade points: " + total);
 		 System.out.println("Your GPA is a: " +gpa);


 	 }

	 }

Here is a small portion from a GPA calculator that I wrote. I believe it should be able to answer your question about performing the actual calculations.

hours = scan.nextDouble();
                        hoursSum += hours;        
                       
                        gradeLtrTemp = scan.next();
                        gradeLtr = gradeLtrTemp.toUpperCase();

                        if(gradeLtr.equals("A"))
                            gradeNum = 4.0;
                        else if(gradeLtr.equals("B"))
                            gradeNum = 3.0;
                        else if(gradeLtr.equals("C"))
                            gradeNum = 2.0;
                        else if(gradeLtr.equals("D"))
                            gradeNum = 1.0;
                        else if(gradeLtr.equals("F"))
                            gradeNum = 0.0;
                        else
                        {
                                System.out.println("Invalid grade.");
                                System.exit(0);
                        }
                
                        qualGrade += (gradeNum * hours);

 gpa = (qualGrade / hoursSum);

Edited 6 Years Ago by JavaNewbieEK: n/a

This article has been dead for over six months. Start a new discussion instead.