← All free-response questionsQuestion 2 · Class Design · 7 points
Habit Tracker
Write the complete HabitTracker class. It records, day by day, whether a habit was done, and reports streaks. A streak is a run of consecutive days on which the habit was done. The table shows how the class is used.
| Statement | Value returned | Explanation |
|---|---|---|
HabitTracker h = new HabitTracker("read"); | Tracks the habit "read"; no days logged. | |
h.logDay(true) | Day 1: done. | |
h.logDay(true) | Day 2: done. | |
h.currentStreak() | 2 | The last two days were done. |
h.logDay(false) | Day 3: missed. The current streak resets. | |
h.logDay(true) | Day 4: done. | |
h.currentStreak() | 1 | |
h.bestStreak() | 2 | The longest streak so far. |
h.summary() | "read: 3 of 4 days" | Habit name, days done, and days logged. |
Write the complete HabitTracker class.
Your class
7 pointsYour class must have a constructor and the methods logDay, currentStreak, bestStreak, and summary, and must behave as shown. Use private instance variables. You may not store every logged day; keep only the counts you need.
Tab inserts spaces. Drafts save as you type.
Self-score
0 / 7