Java Project: Class Grade Book
A grade book is a program that stores students with their scores and reports grades, an average, and the top student. This project builds one grade book in a single file.
A Student class with getters
The constructor fills the private fields. The getters print the stored name and score.
public class Main {
public static void main(String[] args) {
Student first = new Student("Ada", 92);
System.out.println(first.getName());
System.out.println(first.getScore());
}
}
class Student {
private String name;
private int score;
Student(String name, int score) {
this.name = name;
this.score = score;
}
String getName() {
return name;
}
int getScore() {
return score;
}
}Turn a score into a grade
gradeFor tests the score from the top band down. The two calls print A and C.
public class Main {
static String gradeFor(int score) {
if (score >= 90) {
return "A";
} else if (score >= 80) {
return "B";
} else if (score >= 70) {
return "C";
} else if (score >= 60) {
return "D";
} else {
return "F";
}
}
public static void main(String[] args) {
System.out.println(gradeFor(95));
System.out.println(gradeFor(72));
}
}Example
import java.util.ArrayList;
public class Main {
static String gradeFor(int score) {
if (score >= 90) {
return "A";
} else if (score >= 80) {
return "B";
} else if (score >= 70) {
return "C";
} else if (score >= 60) {
return "D";
} else {
return "F";
}
}
public static void main(String[] args) {
ArrayList<Student> students = new ArrayList<Student>();
students.add(new Student("Ada", 92));
students.add(new Student("Ben", 85));
students.add(new Student("Cid", 74));
students.add(new Student("Dee", 61));
int total = 0;
Student top = students.get(0);
for (Student student : students) {
total = total + student.getScore();
if (student.getScore() > top.getScore()) {
top = student;
}
System.out.println(student.getName() + ": " + student.getScore() + " (" + gradeFor(student.getScore()) + ")");
}
double average = (double) total / students.size();
System.out.println("Average: " + average);
System.out.println("Top: " + top.getName());
}
}
class Student {
private String name;
private int score;
Student(String name, int score) {
this.name = name;
this.score = score;
}
String getName() {
return name;
}
int getScore() {
return score;
}
}Key points
- The Student class keeps its fields private.
- Getters read the name and score of a student.
- An ArrayList holds the students in entry order.
- A static method turns a score into a letter grade.
- A cast to double makes the average a decimal division.
- The top student is found by comparing each score with the best so far.
In this example, the list holds four students with scores of 92, 85, 74, and 61. The loop prints each student with the grade from gradeFor and adds each score to the total. The cast to double makes the average 78.0. The top comparison keeps Ada, whose 92 is the highest score.
Note
Every part of this project appeared earlier in the course.
Try it Yourself
Edit the code, then press Run.
Running is simulated ยท Programiz Java Compiler
Exercise
Why does the average line cast the total to double?