Question 4-3

Define a class PassCount to track whether all the students of a class have passed a test. It should support the following methods.

PassCount()
(Constructor method) Constructs an object representing an empty class.

void addGrade(double grade)
Adds grade into the class.

boolean isAnyFailing()
Returns true only if there is somebody in the class who received a grade of less than 60.

For example, if you defined this class properly, I should be able to write the following class to test it.
public class PassCountTest {
    public static void run() {
        PassCount a = new PassCount();
        a.addGrade(45.0);
        a.addGrade(76.0);
        IO.println(a.isAnyFailing()); // should print ``true''

        PassCount b = new PassCount();
        b.addGrade(60.0);
        IO.println(b.isAnyFailing()); // should print ``false''
    }
}
Note that, to accomplish this, a PassCount object need not remember every single grade --- that is, you do not need to use arrays: It just needs to remember whether any of the ones it has seen were below 60.

Solution

public class PassCount {
    private boolean found_fail;

    public PassCount() {
        found_fail = false;
    }

    public void addGrade(double grade) {
        if(grade < 60.0) {
            found_fail = true;
        }
    }

    public boolean isAnyFailing() {
        return found_fail;
    }
}

Back to 8:00 Quiz 4