Simple GPA calculator in Java
Posted by Samath
Last Updated: January 17, 2024

For research purposes and to assist students, the admissions office of your University wants to determine the performance of certain students in any 5 courses. You are asked to calculate your gpa. Your program should accept any 5 course letter grades and 5 course credits for those courses. Each line has one entry. Your program should compute and output your GPA. Format your results to two decimal places.

Solution:

import java.util.Scanner;
import java.text.DecimalFormat;
public class GpaCalculator {

    public static void main(String[] args) {
        
                String lettergrd;
		        double credit;
                double caltimes = 0;
                double totalcal = 0;
                double totalcredit = 0;
                double finalgpa = 0;
		        final double A = 4.0;
		        final double AMINUS = 3.67;
		        final double BPLUS = 3.33;
		        final double B = 3.0;
		        final double BMINUS = 2.67;
		        final double CPLUS = 2.33;
		        final double C = 2.0;
                final double CMINUS = 1.67;
                final double D = 1.00;
                final double F = 0.0;
                DecimalFormat df = new  DecimalFormat("0.##");
		        Scanner grd = new Scanner(System.in);
                
                for(int i = 0; i < 5; i++)
                {
                    
                System.out.println("Please enter the letter grade: ");
		        lettergrd = grd.next();
                
		        System.out.println("Please enter the course credit: ");
		        credit = grd.nextDouble();
                
                switch(lettergrd)
                {
                  case "A": caltimes = credit * A;
                  case "a": caltimes = credit * A;
                    break;
                    case "A-": caltimes = credit * AMINUS;
                    case "a-": caltimes = credit * AMINUS;
                    break;
                    case "B+": caltimes = credit * BPLUS;
                    case "b+": caltimes = credit * BPLUS;
                    break;
                    case "B": caltimes = credit * B;
                    case "b": caltimes = credit * B;
                    break;   
                    case "B-": caltimes = credit * BMINUS;
                    case "b-": caltimes = credit * BMINUS;
                    break;
                    case "C+": caltimes = credit * CPLUS;
                    case "c+": caltimes = credit * CPLUS;
                    break; 
                    case "C": caltimes = credit * C;
                    case "c": caltimes = credit * C;
                    break;
                    case "C-": caltimes = credit * CMINUS;
                    case "c-": caltimes = credit * CMINUS;
                    break;   
                    case "D": caltimes = credit * D;
                    case "d": caltimes = credit * D;
                    break;
                    case "F": caltimes = credit * F;
                    case "f": caltimes = credit * F;
                    break;
                        
                }
                 
                totalcredit = totalcredit + credit;
                totalcal = totalcal + caltimes;
                
                }
                
                finalgpa = totalcal/totalcredit;
                System.out.println("GPA: " + df.format(finalgpa));
    }
    
}

 

Related Content
GPA Calculator using C#
GPA Calculator using C#
Samath | Jan 01, 2021
GPA Calculator in C++
GPA Calculator in C++
Samath | Jan 17, 2024
GPA Calculator using Python
GPA Calculator using Python
Samath | Jan 20, 2024
C Program that calculates GPA
C Program that calculates GPA
Samath | Jan 20, 2024
Simple Calculator using C#
Simple Calculator using C#
Samath | Dec 30, 2020
Calculator Program in Java
Calculator Program in Java
Samath | Jan 07, 2021
Simple C++ Calculator
Simple C++ Calculator
Samath | Jan 05, 2017
Simple Calculator using Python
Simple Calculator using Python
Samath | Jan 09, 2017
Scientific Calculator using C#
Scientific Calculator using C#
Samath | Jan 08, 2021