Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have this hw problem: Write a program that keeps a TreeMap in which both keys and values are stringsthe names of students and their

I have this hw problem:

Write a program that keeps a TreeMap in which both keys and values are stringsthe names of students and their course grades. Prompt the user of the program to add or remove students, to modify grades, or to print all grades. Remember that if two students have the same name, then the grade value should be used as the factor to sort.

The printout should be sorted by name and formatted like this: Cara: 90 Cara: 87 John: 80 Meredith: 90

and I have this code, but I'm getting errors:

import java.util.*;

public class StudentMap implements Comparable { SortedMap> studentGrades; Scanner scnr = new Scanner(System.in); StudentMap(){

studentGrades= new TreeMap< >(); int menuChoice; do { System.out.println("Please choose an option: "); System.out.println("1: Add Student"); System.out.println("2: Remove Student"); System.out.println("3: Modify Grade"); System.out.println("4: Print All Grades"); System.out.println("Press 0 to Exit"); System.out.print("Your option: "); menuChoice = Integer.parseInt(scnr.nextLine());

switch(menuChoice) { case 1: addStudent(); break; case 2: removeStudent(); break; case 3: modifyGrade(studentGrades); break; case 4: printAllGrades(); break; case 0: return; default: System.out.println("Invalid Option"); break; } } while (menuChoice!= 0); } public static void modifyGrade(SortedMap> studentGradesMod) { String studentName; String studentGrade; Scanner scnr = new Scanner(System.in);

System.out.println("Please enter the students name: "); studentName = scnr.nextLine();

System.out.println("Please enter the students modified grade: "); studentGrade = scnr.nextLine();

if(studentGradesMod.containsKey(studentName)) { studentGradesMod.get(studentName).add(studentGrade); System.out.println(studentName + "s new grade is " + studentGrade); } else { System.out.println("Student does not exist, please re-enter students name and modified grade."); } }

public void addStudent() { Scanner scnr = new Scanner(System.in); System.out.println("Enter name of student you would like to add: "); String studentName = scnr.next(); System.out.println("Enter students grade: "); String studentGrade = scnr.next();

if (studentGrades.get(studentName) == null) { studentGrades.put(studentName, new ArrayList<>()); } else { studentGrades.comparator(); } studentGrades.get(studentName).add(studentGrade); System.out.println(studentName + " has been added."); }

public void removeStudent() { Scanner scnr = new Scanner(System.in); System.out.println("Enter name of student you would like to remove: "); String studentName = scnr.next();

if(studentGrades.containsKey(studentName)) { studentGrades.remove(studentName); System.out.println(studentName + " has been removed."); } else { System.out.println("Student does not exist, please re-enter students name."); } }

public void printAllGrades() { Iterator iterator = studentGrades.keySet().iterator(); while (iterator.hasNext()) { String studentName = iterator.next().toString(); List grades = studentGrades.get(studentName); for(String studentGrade: grades) { System.out.println(studentName + " : " + studentGrade); } } }

public static void main (String[] args) { new StudentMap(); }

public int compareTo(Object arg0) { return 0; } }

can someone please help me fix my code and tell me what I was doing wrong?

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeffrey A. Hoffer Fred R. McFadden

9th Edition

B01JXPZ7AK, 9780805360479

More Books

Students also viewed these Databases questions

Question

Compare the current team to the ideal team.

Answered: 1 week ago

Question

Are the rules readily available?

Answered: 1 week ago

Question

Have ground rules been established for the team?

Answered: 1 week ago