ACSA Exam Prep
← All free-response questions
Question 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.

StatementValue returnedExplanation
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()2The 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()2The 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 points

Your 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