Using Java, write a GPA Calculator using methods Your program should have the fo
ID: 3595407 • Letter: U
Question
Using Java, write a GPA Calculator using methods
Your program should have the following items:
a. There should be the following two methods and both these methods should be called from (i.e., within) the main method.
Output: this method takes a string message as a parameter and outputs it on System.out. Output does not return anything.
GPACalculator: this method takes no parameter and returns a GPA value (of type double). It should prompt the user to enter information about 4 different courses and compute the GPA over these courses: The GPA is computed using these information: a grade (A, B, C, D, or F) and a corresponding number of credit hours
final_gpa = (1st grade*1st hour+…..+4th grade*4th hour)/4.00
GPACalculator must call the method Output to prompt the user for inserting the GPAs.
b. You should use if-then-else-if statements (or switch as appropriate) in both your main method and your GPACalculator method.
Explanation / Answer
Please find my implementation.
import java.util.Scanner;
public class GPACalculator {
public static void printMessage(String out) {
System.out.println(out);
}
public static double gPACalculator() {
char grade;
int hours;
int sum_g = 0;
Scanner sc = new Scanner(System.in);
printMessage("Enter grade and number of hours for 4 curces: ");
int i= 1;
while(i <= 4) {
printMessage("Enter grade for coutse "+i+": ");
grade = sc.next().charAt(0);
printMessage("Enter hours for coutse "+i+": ");
hours = sc.nextInt();
if(grade == 'A')
sum_g = sum_g + 90*hours;
else if(grade == 'B')
sum_g = sum_g + 80*hours;
else if(grade == 'C')
sum_g = sum_g + 70*hours;
else if(grade == 'D')
sum_g = sum_g + 60*hours;
else if(grade == 'F')
sum_g = sum_g + 50*hours;
i++;
}
sc.close();
double final_grade = sum_g/4.0;
return final_grade;
}
public static void main(String[] args) {
double grade = gPACalculator();
char c = 'F';
if(grade >= 90)
c = 'A';
else if(grade >= 80)
c = 'B';
else if(grade >= 70)
c = 'C';
else if(grade >= 60)
c = 'D';
else if(grade >= 50)
c = 'F';
System.out.println("Final grade : "+c);
}
}
/*
Sample run:
Enter grade and number of hours for 4 curces:
Enter grade for coutse 1:
A
Enter hours for coutse 1:
6
Enter grade for coutse 2:
B
Enter hours for coutse 2:
7
Enter grade for coutse 3:
C
Enter hours for coutse 3:
7
Enter grade for coutse 4:
A
Enter hours for coutse 4:
5
Final grade : A
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.