AP® Computer Science A Class Creation FRQ
Work through AP Computer Science A free-response questions on Class Creation, then check each answer against a worked solution.
Free · no sign-up Open questions need no account. Sign up or log in to open the rest.
Subtopic: Writing Classes Clear filter
Question 42
A ReadingTracker object records pages read toward a personal reading goal. A newly constructed tracker is given a positive page goal. The tracker starts with 0 pages read and 0 recorded reading days.
The method read takes a number of pages. If the argument is positive, those pages are added to the total pages read and the number of recorded reading days increases by one. If the argument is not positive, the tracker is unchanged. The method pagesLeft returns the number of additional pages needed to reach the goal, or 0 if the goal has already been reached. The method averagePages returns the average number of pages read per recorded reading day, or 0.0 if there have been no recorded reading days. The method metGoal returns whether the total pages read is at least the goal.
| Statements and Expressions | Value Returned (blank if no value) | Comment |
|---|---|---|
ReadingTracker rt = new ReadingTracker(120); | A tracker with goal 120 is created. | |
rt.pagesLeft() | 120 | No pages have been recorded. |
rt.read(35); | Total pages becomes 35; recorded days becomes 1. | |
rt.read(0); | Non-positive input is ignored. | |
rt.read(50); | Total pages becomes 85; recorded days becomes 2. | |
rt.averagePages() | 42.5 | The average is 85 * 1.0 / 2. |
rt.metGoal() | false | The goal has not yet been reached. |
rt.read(40); | Total pages becomes 125; recorded days becomes 3. | |
rt.pagesLeft() | 0 | No more pages are needed. |
rt.metGoal() | true | The total is at least the goal. |
Write the complete ReadingTracker class, including all instance variables, a constructor, and all methods described above.
Main Solution
Sample Solution
public class ReadingTracker
{
private int goal;
private int totalPages;
private int days;
public ReadingTracker(int pageGoal)
{
goal = pageGoal;
totalPages = 0;
days = 0;
}
public void read(int pages)
{
if (pages > 0)
{
totalPages = totalPages + pages;
days++;
}
}
public int pagesLeft()
{
if (totalPages >= goal)
{
return 0;
}
return goal - totalPages;
}
public double averagePages()
{
if (days == 0)
{
return 0.0;
}
return totalPages * 1.0 / days;
}
public boolean metGoal()
{
return totalPages >= goal;
}
}
Scoring (9 pts): Points are earned for correct private fields, constructor initialization, ignoring non-positive readings, updating both total pages and days for valid readings, computing pages remaining without returning a negative value, avoiding integer division in the average, handling zero reading days, and returning the goal status correctly.
Answer images
AP® and Advanced Placement® are registered trademarks of the College Board, which is not affiliated with, and does not endorse, Verityprep. Names are used for identification purposes only.

