Lab Assignment Report - Student Grades (Hash Map)

Categories: Technology

Introduction

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.

Methodology

The assignment involves several steps:

  1. Initialize a HashMap named "studentGrades" with pre-entered student names as keys and their corresponding grades as values.
  2. Prompt the user to enter a student's name and grade.
  3. Retrieve and output the original grade of the student from the HashMap.
  4. Update the student's grade in the HashMap with the new grade provided by the user.
  5. Output the new grade of the student.

Code Implementation

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.");
    }
}
}

Results

When you run the provided Java program, it will prompt the user to enter a student's name.

Get to Know The Price Estimate For Your Paper
Topic
Number of pages
Email Invalid email

By clicking “Check Writers’ Offers”, you agree to our terms of service and privacy policy. We’ll occasionally send you promo and account related email

"You must agree to out terms of services and privacy policy"
Write my paper

You won’t be charged yet!

If the student is found in the HashMap, it will display their original grade.

Get quality help now
Dr. Karlyna PhD
Dr. Karlyna PhD
checked Verified writer

Proficient in: Technology

star star star star 4.7 (235)

“ Amazing writer! I am really satisfied with her work. An excellent price as well. ”

avatar avatar avatar
+84 relevant experts are online
Hire writer

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.

Conclusion

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.

Appendix

Below is the Java class used in this assignment:

Lab7.java


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)
}
}
Updated: Jan 03, 2024
Cite this page

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

Lab Assignment Report - Student Grades (Hash Map) essay
Live chat  with support 24/7

👋 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