Java code for school grade book

Here, I am going to write java code which will do following:
  • Will print the number of students along with their score.
  • The entire class average.
  • Lowest grade in the class.
  • Highest grade in the class.
So, let's get it started with the first java class.

public class GradeBook {
    private String courseName;
    private int[] grades;

    public GradeBook(String name, int[] gradesArray) {
        courseName = name;
        grades = gradesArray;
    }

    public void setCourseName(String name) {
        courseName = name;
    }

    public String getCourseName() {
        return courseName;
    }

    public void displayMessage() {
        System.out.printf("Welcome to the grade book for\n%s!\n\n",
                getCourseName());
    }

    public void processGrades() {
        outputGrades();
        System.out.printf("\nClass average is %.2f\n", getAverage());
        System.out.printf("Lowest grade is %d\nHighest grade is %d\n\n",
                getMinimum(), getMaximum());
        outputBarChart();
    }

    public int getMinimum() {
        int lowGrade = grades[0];
        for (int grade : grades) {
            if (grade < lowGrade)
                lowGrade = grade;
        }
        return lowGrade;
    }

    public int getMaximum() {
        int highGrade = grades[0];
        for (int grade : grades) {
            if (grade > highGrade)
                highGrade = grade;
        }
        return highGrade;
    }

    public double getAverage() {
        int total = 0;
        for (int grade : grades)
            total += grade;
        return (double) total / grades.length;
    }

    public void outputBarChart() {
        System.out.println("Grade distribution: ");
        int[] frequency = new int[11];
        for (int grade : grades)
            ++frequency[grade / 10];
        for (int count = 0; count < frequency.length; count++) {
            if (count == 10)
                System.out.printf("%5d: ", 100);
            else
                System.out.printf("%02d-%02d: ", count * 10, count * 10 + 9);
            for (int stars = 0; stars < frequency[count]; stars++)
                System.out.print("*");
            System.out.println();
        }
    }

    public void outputGrades() {
        System.out.println("The grades are:\n");
        for (int student = 0; student < grades.length; student++)
            System.out.printf("Student %2d: %3d\n", student + 1,
                    grades[student]);
    }
}


Now, let's create the other java class which will enable us to test the above class and methods

public class GradeBookTest {

    public static void main(String[] args) {

        // array of student grades. This is the data that we will use to
        // populate the array.
        // There are many ways of obtaining data to populate arrays, and some of
        // those ways are by user-input,
        // or by reading from a file, or in this case, we provide the data using
        // an array initializer list.
        int[] gradesArray = { 87, 68, 94, 100, 83, 78, 85, 91, 76, 87 };

        GradeBook myGradeBook = new GradeBook("Java Programming", gradesArray);

        myGradeBook.displayMessage();
        myGradeBook.processGrades();
    }
}


Output:

Welcome to the grade book for
Java Programming!

The grades are:

Student  1:  87
Student  2:  68
Student  3:  94
Student  4: 100
Student  5:  83
Student  6:  78
Student  7:  85
Student  8:  91
Student  9:  76
Student 10:  87

Class average is 84.90
Lowest grade is 68
Highest grade is 100

Grade distribution:
00-09:
10-19:
20-29:
30-39:
40-49:
50-59:
60-69: *
70-79: **
80-89: ****
90-99: **
  100: *

No comments:

Post a Comment