Calculating GPA Students and advisors commonly need to find the GPA for specific
ID: 3908528 • Letter: C
Question
Calculating GPA Students and advisors commonly need to find the GPA for specific groups of courses taken. Examples include finding the "technical GPA” or the GPA for prerequisite courses for a specific major. Write a Java program that takes in letter grades and credit pairs and computes the GPA. The number of pairs to read will be entered as a command line argument (args[0]). For the letter grades and credit pairs themselves, however, instead of taking them in as arguments via the command line, we will use the Scanner class. Use the following chart for grade point values:Explanation / Answer
import java.util.Scanner;
class GPA
{
public static void main(String ar[])
{
Scanner sc=new Scanner(System.in);
int i,c,s=0;//s is to keep track of how many grades are entered if a 4 b4 is input then s=8
double sum=0; //sum is calculate numerator of gpa calculation formula given
String gr,dummy; //dummy is useful when string inputs in loop
for (i=0;i<Integer.parseInt(ar[0]);i++)//convert cmd arguement to int
{
gr=sc.nextLine();
c=sc.nextInt();
if (gr.equals("a"))
sum=sum+(c*4.0);
else if(gr.equals("a-"))
sum=sum+(c*3.667);
else if(gr.equals("b+"))
sum=sum+(c*3.333);
else if(gr.equals("b"))
sum=sum+(c*3.0);
else if(gr.equals("b-"))
sum=sum+(c*2.667);
else if(gr.equals("c+"))
sum=sum+(c*2.333);
else if(gr.equals("c"))
sum=sum+(c*2.0);
else if(gr.equals("c-"))
sum=sum+(c*1.667);
else if(gr.equals("d+"))
sum=sum+(c*1.333);
else if(gr.equals("d"))
sum=sum+(c*1.0);
else if(gr.equals("f"))
sum=sum+(c*0.0);
s=s+c;
dummy=sc.nextLine();
}
double result= Math.round((sum/s) * 100.0) / 100.0;//rounding result to 2 decimal points
System.out.println("The GPA is:");//priting result
System.out.println(result);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.