35.0k views
3 votes
Given a HashMap pre-filled with student names as keys and grades as values, complete main() by reading in the name of a student, outputting their original grade, and then reading in and outputting their new grade.Ex: If the input is:Quincy Wraight73.1the output is:Quincy Wraight's original grade: 65.4Quincy Wraight's new grade: 73.1GIVEN TEMPLATES StudentGrades.javaimport java.util.Scanner;import java.util.HashMap;public class StudentGrades {public static void main (String[] args) {Scanner scnr = new Scanner(System.in);String studentName;double studentGrade; HashMap studentGrades = new HashMap(); // Students's grades (pre-entered)studentGrades.put("Harry Rawlins", 84.3);studentGrades.put("Stephanie Kong", 91.0);studentGrades.put("Shailen Tennyson", 78.6);studentGrades.put("Quincy Wraight", 65.4);studentGrades.put("Janine Antinori", 98.2); // TODO: Read in new grade for a student, output initial// grade, replace with new grade in HashMap,// output new grade }}

User NLemay
by
2.9k points

2 Answers

5 votes

Answer:

import java.util.Scanner;

import java.util.HashMap;

public class StudentGrades {

public static void main (String[] args) {

Scanner scnr = new Scanner(System.in);

String studentName;

double studentGrade;

HashMap<String, Double> studentGrades = new HashMap<String, Double>();

// Students's grades (pre-entered)

studentGrades.put("Harry Rawlins", 84.3);

studentGrades.put("Stephanie Kong", 91.0);

studentGrades.put("Shailen Tennyson", 78.6);

studentGrades.put("Quincy Wraight", 65.4);

studentGrades.put("Janine Antinori", 98.2);

// TODO: Read in new grade for a student, output initial

// grade, replace with new grade in HashMap,

// output new grade

studentName = scnr.nextLine();

studentGrade = scnr.nextDouble();

System.out.println(studentName + "'s original grade: " + studentGrades.get(studentName));

for (int i = 0; i < studentGrades.size(); i++) {

studentGrades.put(studentName, studentGrade);

}

System.out.println(studentName + "'s new grade: " + studentGrades.get(studentName));

}

}

Step-by-step explanation:

The program first reads in the entire name (scnr.nextLine()) and the next double it sees (scnr.nextDouble()). In the HashMap, it is formatted as (key, value). studentName is the key, while studentGrade is the value. To get the name when printed, use studentName. To get the grade, use studentGrades.get(studentName).

After, use a For loop that replaces the studentGrade with the scanned Double by using studentName as a key. Use the same print statement format but with different wording to finally print the new grade. Got a 10/10 on ZyBooks, if you have any questions please ask!

User Mateusz Bartkowski
by
4.0k points
5 votes
wait I’m confused what are you asking?
User Yamass
by
3.3k points