← Unit 3 · all exercises
Saved in this browser only

A running average

Write a GradeBook that tracks a total and a count, both starting at zero. record adds a score. average returns the total divided by the count as a double, or 0.0 when nothing has been recorded.

class GradeBook { GradeBook(); void record(int score); double average(); }

GradeBook.java
Loading editor…
Results

Examples

  • GradeBook g = new GradeBook(); g.record(80); g.record(90); g.average() → 85
  • new GradeBook().average() → 0nothing recorded yet

There are also 3 hidden tests you can't see. Solve the problem, not the examples.