Question 0-3

In CSCI 160, there are two types of grades: quiz grades (out of 30) and regular grades. The lowest quiz grade is dropped in computing a student's total score. For example, if a student scored 30/35 and 24/35 on two labs and 20/30, 15/30, and 30/30 on three quizzes, the student's total score would be (30 + 24 + 20 + 30) / (35 + 35 + 30 + 30) = 104 / 130 = 80%, since the lowest score is dropped.

Write a class Student to accumulate a single student's grade. This class should have three methods.

double getTotal()
Returns the fraction of the points the student has scored so far, neglecting the quiz score.

void addQuizScore(int score)
Inserts a score of score points of the possible 30 for a quiz.

void addRegularScore(int score, int possible)
Inserts a score of score points of the possible possible into the student's total.

I should be able to execute the following class to test your Student class.

import csbsju.cs160.*;

public class StudentTest {
    public static void main(String[] args) {
        Student stud = new Student();
        stud.addRegularScore(30, 35);
        stud.addRegularScore(24, 35);
        stud.addQuizScore(20);
        stud.addQuizScore(15);
        stud.addQuizScore(30);
        IO.println(100.0 * stud.getTotal() + "%"); // prints ``80.0%''
    }
}

Solution

public class Student {
    private static final int QUIZ_POSSIBLE = 30;

    private int total_score;
    private int total_possible;
    private boolean has_quiz;
    private int min_quiz;

    public Student() {
        total_score = 0;
        total_possible = 0;
        has_quiz = false;
        min_quiz = 0;
    }

    public double getTotal() {
        return (double) total_score / total_possible;
    }

    public void addRegularScore(int score, int possible) {
        total_score += score;
        total_possible += possible;
    }

    public void addQuizScore(int score) {
        if(has_quiz) {
            if(score < min_quiz) { // we have a new minimum
                total_score += min_quiz;
                total_possible += QUIZ_POSSIBLE;
                min_quiz = score;
            } else { // this quiz score counts
                total_score += score;
                total_possible += QUIZ_POSSIBLE;
            }
        } else {
            min_quiz = score;
            has_quiz = true;
        }
    }
}

Back to Exercise Set 0