To install StudyMoose App tap and then “Add to Home Screen”
Save to my list
Remove from my list
In this lab assignment, we are tasked with working on a Java program that involves a HashMap containing student names as keys and their corresponding grades as values. The objective is to read in the name of a student, output their original grade, and then read in and output their new grade. The user will be prompted to enter the student's name and grade, which can be separated by either a space or a new line.
The assignment involves several steps:
Below is the Java code for implementing the aforementioned steps:
import java.util.Scanner;
import java.util.HashMap;
public class Lab7 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String studentName;
double studentGrade;
HashMap<String, Double> studentGrades = new HashMap<>();
// Pre-entered student grades
studentGrades.put("Harry Rawlins", 84.3);
studentGrades.put("Stephanie Kong", 91.0);
studentGrades.put("Shallen Tennyson", 78.6);
studentGrades.put("Quincy Wraight", 65.4);
studentGrades.put("Janine Antinori", 98.2);
// Prompt the user to enter a student's name
System.out.print("Enter a student's name: ");
studentName = scanner.nextLine();
// Retrieve and output the original grade
if (studentGrades.containsKey(studentName)) {
double originalGrade = studentGrades.get(studentName);
System.out.println(studentName + "'s original grade: " + originalGrade);
// Prompt the user to enter the new grade
System.out.print("Enter the new grade: ");
studentGrade = scanner.nextDouble();
// Update the student's grade in the HashMap
studentGrades.put(studentName, studentGrade);
// Output the new grade
System.out.println(studentName + "'s new grade: " + studentGrade);
} else {
System.out.println("Student not found in the database.");
}
}
}
When you run the provided Java program, it will prompt the user to enter a student's name.
If the student is found in the HashMap, it will display their original grade.
Then, the user will be prompted to enter a new grade for the student. After entering the new grade, the program will update the student's grade in the HashMap and display the new grade. If the student is not found in the database, a message indicating that the student was not found will be displayed.
This lab assignment has provided hands-on experience in working with HashMaps in Java. It demonstrates how to store and retrieve data efficiently using key-value pairs. By allowing the user to input and update student grades, the program showcases the flexibility and usefulness of data structures like HashMaps in real-world applications.
Below is the Java class used in this assignment:
import java.util.Scanner;
import java.util.HashMap;
public class Lab7 {
public static void main(String[] args) {
// ... (Refer to the Methodology section for the complete code)
}
}
Lab Assignment Report - Student Grades (Hash Map). (2023, Aug 04). Retrieved from https://studymoose.com/document/comp-182-lab-assignment-7-student-grades-hash-map
👋 Hi! I’m your smart assistant Amy!
Don’t know where to start? Type your requirements and I’ll connect you to an academic expert within 3 minutes.
get help with your assignment