public class Student { private String name; private String year; private double
ID: 3538125 • Letter: P
Question
public class Student {
private String name;
private String year;
private double gpa;
// 1) Make a HashMap called history that maps String (course name) to Character (grade attained)
/*
2) Make a constructor that takes 5 parameters: a String (name of student), a String (year of
Student), a double (gpa of Student), an array of String (courses taken) and an array of
Character (grades attained in courses) and use the parameters to set the name, year, gpa
and to add the entries of the HashMap.
*/
// 3) Make a getter for the Student name.
/*
4) Make a getter called getGrade that has a parameter of type String (the course) and that returns
a Character (the grade attained on that course) from the HashMap.
*/
// 5) Make a toString method to return: Name: xxxxxx Year: xxxxx
}
Explanation / Answer
import java.util.HashMap;
/**
*
* @author Admin
*/
public class Student {
private String name;
private String year;
private double gpa;
private HashMap<String,Character> history;
public Student(String name,String year,double gpa,String[] courses,char[] grade){
this.name = name;
this.year = year;
this.gpa = gpa;
history = new HashMap<>();
for(int i = 0; i < courses.length;i++){
history.put(courses[i], grade[i]);
}
}
public String getName() {
return name;
}
public char getGrade(String course){
return history.get(course);
}
public String toString(){
String result = "";
result += "Name : "+name+" Year : "+year;
return result;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.