Problem 1 . (Average GPA) Write an ST client called AvgGPA that creates a symbol
ID: 3714496 • Letter: P
Question
Problem 1. (Average GPA) Write an ST client called AvgGPA that creates a symbol table mapping letter grades to numerical scores, as in the table below, then reads from standard input a list of letter grades and computes and prints the average GPA (the average of the numbers corresponding to the grades).
$ java AvgGPA
B A - A A - B A - A - B A+ B A + A - B - B B+ B+ A C A + F
<ctrl -d >
3.2835
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.ST;
public class AvgGPA {
public static void main(String[] args) {
...
}
Explanation / Answer
import java.util.HashMap;
import java.util.Scanner;
class GradeSymbolTable{
public static void main(String[] args) {
String letter;
//Creating table
HashMap<String, Double> gradeTable = new HashMap<String, Double>();
gradeTable.put("A+",4.35 );
gradeTable.put("A",4.00 );
gradeTable.put("A-",3.65 );
gradeTable.put("B+",3.35 );
gradeTable.put("B",3.00 );
gradeTable.put("B-",2.65 );
gradeTable.put("C+",2.35 );
gradeTable.put("C",2.00 );
gradeTable.put("C-",1.65 );
gradeTable.put("D+",1.35 );
gradeTable.put("D",1.0 );
gradeTable.put("D-", 0.65);
gradeTable.put("F",0.00 );
Scanner sc = new Scanner(System.in);
// taking input
System.out.println("Enter grade letter : ");
letter = sc.next();
while(!"e".equalsIgnoreCase(letter)){
// getting gpa from table
Double gpa = gradeTable.get(letter);
// if entered grade letter is not valid then table will return null
if(gpa == null)
System.out.println("Invalid Grade Symbol");
else
System.out.println("GPA: "+gpa);
System.out.println("Enter more grade letter or enter e/E for exit.");
letter = sc.next();
}
}
}
/*
Output:
Enter grade letter :
A
GPA: 4.0
Enter more grade letter or enter e/E for exit.
D
GPA: 1.0
Enter more grade letter or enter e/E for exit.
e
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.