I have a java program homework problem, i don\'t know how to use the HashMap, pl
ID: 3539953 • Letter: I
Question
I have a java program homework problem, i don't know how to use the HashMap, please help.
// Grades.java
import java.util.HashMap;
public class Grades
{
public static void main(String[] args)
{
int N = 0; // number of students
double totalGPA = 0.0; // total GPA
HashMap<String, Double> map = new HashMap<String, Double>();
// TBD
map.put("A", new Double(4.33));
map.put("A-", new Double(3.67));
map.put("B+", new Double(3.33));
map.put("B", new Double(3.00));
map.put("B-", new Double(2.67));
map.put("C+", new Double(2.33));
map.put("C", new Double(2.00));
map.put("C-", new Double(1.67));
map.put("D", new Double(1.00));
map.put("F", new Double(0.00));
System.out.println(totalGPA / N);
}
}
Explanation / Answer
// Grades.java
import java.util.*;
public class Grades
{
public static void main(String[] args)
{
int N = 0; // number of students
double totalGPA = 0.0; // total GPA
HashMap<String, Double> map = new HashMap<String, Double>();
// TBD
map.put("A", new Double(4.33));
map.put("A-", new Double(3.67));
map.put("B+", new Double(3.33));
map.put("B", new Double(3.00));
map.put("B-", new Double(2.67));
map.put("C+", new Double(2.33));
map.put("C", new Double(2.00));
map.put("C-", new Double(1.67));
map.put("D", new Double(1.00));
map.put("F", new Double(0.00));
for(String key: map.keySet()) {
System.out.println( "Key : "+ key + " -- value: " + map.get(key));
totalGPA+= map.get(key);
N++;
}
System.out.println("average grade is " + totalGPA / N);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.