Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can you modify the Gradebook.JAVA to make printout be sorted by name and formatted like the example below: Example Sample Run: A)dd R)emove M)odify P)rint

Can you modify the Gradebook.JAVA to make printout be sorted by name and formatted like the example below:

Example Sample Run:

A)dd R)emove M)odify P)rint Q)uit

a

Enter the student's name: Robert

Enter the student's grade: A+

A)dd R)emove M)odify P)rint Q)uit

a

Enter the student's name: Emily

Enter the student's grade: B

A)dd R)emove M)odify P)rint Q)uit

a

Enter the student's name: Max

Enter the student's grade: D

A)dd R)emove M)odify P)rint Q)uit

p

Emily B

Max D

Robert A+

Gradebook.JAVA:

import java.util.*;

public class Gradebook

{

private Map grades;

public Gradebook(){

grades = new HashMap();

};

public void addEntry(String name,String grade)

{

grades.put(name,grade);

}

public void removeEntry(String name)

{

grades.remove(name);

}

public void modifyEntry(String name,String grade)

{

if(grades.containsKey(name))

grades.put(name,grade);

else

System.out.println("Entered name is not present.");

}

public String[] getEntries()

{

String str[]=new String[grades.size()];

int i=0;

for (Map.Entry mapElement : grades.entrySet()) {

String key

= (String)mapElement.getKey();

String value

= (String)mapElement.getValue();

str[i]=key+" "+value;

i++;

}

return str;

}

}

GradebookUI.java:

import java.util.Scanner;

/**

* A program to add, remove, modify or print student names and grades.

*/

public class GradebookUI {

public static void main(String[] args) {

Gradebook gradebook = new Gradebook();

Scanner in = new Scanner(System.in);

boolean done = false;

while (!done) {

System.out.println("A)dd R)emove M)odify P)rint Q)uit");

String input = in.next().toUpperCase();

if (input.equals("Q"))

done = true;

else if (input.equals("A")) {

System.out.print("Enter the student's name: ");

String name = in.next();

System.out.print("Enter the student's grade: ");

String grade = in.next();

gradebook.addEntry(name, grade);

} else if (input.equals("R")) {

System.out.print("Enter the student's name: ");

String name = in.next();

gradebook.removeEntry(name);

} else if (input.equals("M")) {

System.out.print("Enter the student's name: ");

String name = in.next();

System.out.print("Enter the student's grade: ");

String grade = in.next();

gradebook.modifyEntry(name, grade);

} else if (input.equalsIgnoreCase("P")) {

String[] entries = gradebook.getEntries();

for (String entry : entries)

System.out.println(entry);

} else

done = true;

}

}

}

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

Unity From Zero To Proficiency Beginner A Step By Step Guide To Coding Your First Game

Authors: Patrick Felicia

1st Edition

1091872023, 978-1091872028

More Books

Students also viewed these Programming questions

Question

Week 3: Moment of force: scalar formulation...explain it

Answered: 1 week ago