Part 1 Create a program in Eclipse to Calculate the overall

Part 1:
Create a program in Eclipse to:
-Calculate the overall GPA for a student (rounding two decimal places)
-Assume the user took four classes in the semester
-For each of the four classes (just copy the steps below 4 times)
   -Ask user for the credit hours of the course
   -Ask user for the letter grade received in the course
*Make sure code works for upper and lower case grades*
Part 2:
-Add a simple error check so only the grades A,B,C,D, or F will be accepted. Otherwise, an error message is produced (\"Invalid Grade\") and the program will exit.
Please make sure the program is compatible with Eclipse software

Solution

GPACalculator.java

import java.text.DecimalFormat;
import java.util.Scanner;

public class GPACalculator {

   public static void main(String[] args) {

       //Declaring variables
       double credit_hours1,credit_hours2,credit_hours3,credit_hours4;
       char grade_letter1,grade_letter2,grade_letter3,grade_letter4;
       double grade_points1,grade_points2,grade_points3,grade_points4;
       double tot_grade_points,gpa, tot_credits;
      
       //DecimalFormat class is used to format the output
       DecimalFormat df=new DecimalFormat(\"#.##\");
      
       //Scanner object is used to get the inouits entered by the user
       Scanner sc=new Scanner(System.in);
      
       //Getting the credit hours and grade letter entered by the user of class#1
       System.out.println(\"Class#1\");
       System.out.print(\"Enter the Credit Hours :\");
       credit_hours1=sc.nextDouble();
       System.out.print(\"Enter the Grade Letter :\");
       grade_letter1=sc.next(\".\").charAt(0);
      
       //Calling the method by passing the inputs as arguments
       grade_points1=calGradePoints(credit_hours1,grade_letter1);
      
       //Getting the credit hours and grade letter entered by the user of class#2
       System.out.println(\"Class#2\");
       System.out.print(\"Enter the Credit Hours :\");
       credit_hours2=sc.nextDouble();
       System.out.print(\"Enter the Grade Letter :\");
       grade_letter2=sc.next(\".\").charAt(0);
      
       //Calling the method by passing the inputs as arguments
       grade_points2=calGradePoints(credit_hours2,grade_letter2);
      
       //Getting the credit hours and grade letter entered by the user of class#3
       System.out.println(\"Class#3\");
       System.out.print(\"Enter the Credit Hours :\");
       credit_hours3=sc.nextDouble();
       System.out.print(\"Enter the Grade Letter :\");
       grade_letter3=sc.next(\".\").charAt(0);
      
       //Calling the method by passing the inputs as arguments
       grade_points3=calGradePoints(credit_hours3,grade_letter3);
      
       //Getting the credit hours and grade letter entered by the user of class#4
       System.out.println(\"Class#4\");
       System.out.print(\"Enter the Credit Hours :\");
       credit_hours4=sc.nextDouble();
       System.out.print(\"Enter the Grade Letter :\");
       grade_letter4=sc.next(\".\").charAt(0);
      
       //Calling the method by passing the inputs as arguments
       grade_points4=calGradePoints(credit_hours4,grade_letter4);
      
       //calculating the total grade points
       tot_grade_points=grade_points1+grade_points2+grade_points3+grade_points4;
      
       //calculating the total credits of 4 classes
       tot_credits=credit_hours1+credit_hours2+credit_hours3+credit_hours4;

       //calculating the GPA
       gpa=tot_grade_points/tot_credits;
      
       //Displaying the output
       System.out.println(\"The overall GPA for a student :\"+df.format(gpa));

   }

   //This method is used to find out the grade points
   private static double calGradePoints(double credit_hours1, char grade_letter1) {
      
       double points = 0;
      
       //Based on the credit hours and Grade letter calculate the grade points and return
       switch(grade_letter1)
       {
       case \'a\':
       case \'A\':{
           points=4.0*credit_hours1;
           break;
       }
       case \'b\':
       case \'B\':{
           points=3.0*credit_hours1;
           break;
       }
       case \'c\':
       case \'C\':{
           points=2.0*credit_hours1;
           break;
       }
       case \'d\':
       case \'D\':{
           points=2.0*credit_hours1;
           break;
       }
       case \'f\':
       case \'F\':{
           points=1.0*credit_hours1;
           break;
       }
       default:
       {
           //if the user enters and invalid invalid grade letter ,displaying the error message
           System.out.println(\":: Invalid Grade ::\");

System.out.println(\"** Program Exit **\");
           System.exit(0);
       }
       }
       return points;
   }

}

_____________________

output:

Class#1
Enter the Credit Hours :3
Enter the Grade Letter :A
Class#2
Enter the Credit Hours :3
Enter the Grade Letter :B
Class#3
Enter the Credit Hours :2
Enter the Grade Letter :A
Class#4
Enter the Credit Hours :1.5
Enter the Grade Letter :A
The overall GPA for a student :3.68

________________

Output2:

Class#1
Enter the Credit Hours :3
Enter the Grade Letter :A
Class#2
Enter the Credit Hours :2
Enter the Grade Letter :H
:: Invalid Grade ::
** Program Exit **

_____________Thank You

Part 1: Create a program in Eclipse to: -Calculate the overall GPA for a student (rounding two decimal places) -Assume the user took four classes in the semeste
Part 1: Create a program in Eclipse to: -Calculate the overall GPA for a student (rounding two decimal places) -Assume the user took four classes in the semeste
Part 1: Create a program in Eclipse to: -Calculate the overall GPA for a student (rounding two decimal places) -Assume the user took four classes in the semeste

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site