A java question. Write a class StudentManager which uses a Map to associate stud
ID: 3835925 • Letter: A
Question
A java question. Write a class StudentManager which uses a Map to associate students's names and their letter grades. Both the keys (names) and the values (letter grades) are Strings.
The constructor initializes an empty map.
public StudentManager()
Provide methods:
1. public void add(String name, String grade) adds the student name and grade to the map
2. public void remove(String name) removes the association of this name and gpa
3. public int getClassSize() gets the number of students in the class
4. public String getPrintableRoster() gets the names of the students in alphabetical order and their grades in the format below
Anisa: A
Carlos: B+
James: A-
------------------------------------------------------------------------------------------
The StudentManagerRunner is given as follow:
StudentManagerRunner.java
Explanation / Answer
Below is the implementation: -
StudentManager.java
import java.util.Map;
import java.util.TreeMap;
public class StudentManager {
private TreeMap<String, String> students;//using sorted type of map i.e. TreeMap
public StudentManager() {
students = new TreeMap<>();//instantiation of map
}
public void add(String name, String grade) {
students.put(name, grade);//adding in the map
}
public void remove(String name) {
students.remove(name);//removing from map
}
public int getClassSize() {
return students.size();//count of entries
}
public String getPrintableRoster() {
String ps = "";
//Iterating and creating a string of output.
for (Map.Entry<String, String> entry : students.entrySet()) {
ps = ps+entry.getKey()+": "+entry.getValue()+" ";
}
return ps;
}
}
StudentManagerRunner.java
public class StudentManagerRunner
{
public static void main(String[] args)
{
StudentManager students = new StudentManager();
students.add("Anisa", "A");
students.add("Carlos", "B+");
students.add("James", "A-");
System.out.println(students.getPrintableRoster());
}
}
Sample Output: -
Anisa: A
Carlos: B+
James: A-
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.