Mini Project Set 4: Student Score Report¶
Name: ________
Time: 40 minutes
Write a Java program that stores five students as objects in an ArrayList, then prints a report card summary. Use a HashMap to count how many students earned each letter grade.
Learning Goals¶
- Create a class with private fields, a constructor, and getter methods.
- Store objects in an
ArrayList. - Count occurrences with a
HashMap. - Search a list and remember the index of a best value.
- Combine loops, conditionals, strings, and formatted output.
Requirements¶
- Create a
Studentclass with these parts: - private fields
String nameandint score - a constructor that receives both values
getName()returns the namegetScore()returns the scoregetGrade()returns the letter grade for the score:- 90-100:
"A" - 80-89:
"B" - 70-79:
"C" - 60-69:
"D" - below 60:
"F"
- 90-100:
- In
main, build thisArrayList<Student>with exactly these five objects:
ArrayList<Student> students = new ArrayList<>();
students.add(new Student("Mina", 88));
students.add(new Student("Jin", 72));
students.add(new Student("Hana", 95));
students.add(new Student("Leo", 95));
students.add(new Student("Sara", 54));
- Use one loop over
studentsto find: - the total of all scores
- the index of the first student with the highest score
- the number of passing students (score 60 or higher)
- While looping, update a
HashMap<String, Integer>namedgradeCountsso each letter grade maps to the number of students who earned it. - Print one line for each student in this format:
Mina: 88 (B)
- Then print the summary. After that, print the grade counts in the order A, B, C, D, F. Only print a grade that appears at least once. The average must have two decimal places.
- When two students have the same highest score, keep the first one. Do not replace the best index for an equal score.
Expected Output¶
Mina: 88 (B)
Jin: 72 (C)
Hana: 95 (A)
Leo: 95 (A)
Sara: 54 (F)
Average score: 80.80
Top student: Hana
Passing students: 4
A grades: 2
B grades: 1
C grades: 1
F grades: 1
Starter Code¶
import java.util.ArrayList;
import java.util.HashMap;
class Student {
// TODO: Add private fields, a constructor, getName(), getScore(), and getGrade().
}
public class Main {
public static void main(String[] args) {
ArrayList<Student> students = new ArrayList<>();
students.add(new Student("Mina", 88));
students.add(new Student("Jin", 72));
students.add(new Student("Hana", 95));
students.add(new Student("Leo", 95));
students.add(new Student("Sara", 54));
HashMap<String, Integer> gradeCounts = new HashMap<>();
// TODO: Initialize summary variables.
// TODO: Use one loop to print each student, update the summary values,
// and count the letter grades in gradeCounts.
// TODO: Print the summary.
// TODO: Print the grade counts in the order A, B, C, D, F.
}
}
Before You Submit¶
- Every
Studentobject stores its ownnameandscore; usethis.in the constructor. - Use
students.size()instead of a hard-coded limit. - Start
bestIndexat0because the first student is initially the best candidate. - Use
>instead of>=when comparing a score to the current highest score. - Before counting a grade, check whether it is already in
gradeCountswithcontainsKey. - Iterate the list with an index so you can remember the best student's index.