Question 1: Methods and Control Structures
Rewards card constructor and top item
- Units 1, 2, 3 and 4
- 7 points
- About 25 minutes
You write two methods, or a constructor and a method, of a class you're given. Part A needs loops and/or if statements plus calls to the class's own methods; Part B needs String methods. New since the course was revised for 2025–26 (first tested in May 2026): it's worth 7 points instead of 9, and points are no longer taken off as penalties. On the exam: Question 1 of 4, 7 points (Part A 4 points, Part B 3 points). Section II has 4 free-response questions in 90 minutes (45% of the score), all assessing Practice 2 (Develop Code). The exam is fully digital in Bluebook: you type your Java, and the Java Quick Reference is provided. No calculator. Suggested time is our split of the 90 minutes by points; College Board gives no per-question timing.
The question and its sources
A café gives customers reward points. Each visit adds a fixed number of points, and then the café adds a bonus that depends on the customer's new total. You will write a constructor and a method of the RewardsCard class.
The RewardsCard class
public class RewardsCard
{
/** The number of visits needed to reach the point goal */
private int visitsNeeded;
/**
* Returns the bonus points earned when a card's total is points.
* Precondition: points > 0
* Postcondition: the value returned is >= 0
*/
public static int bonusPoints(int points)
{ /* implementation not shown */ }
/** Described in part (a) */
public RewardsCard(int goal, int perVisit)
{ /* to be implemented in part (a) */ }
/** Returns the number of visits needed to reach the point goal. */
public int getVisitsNeeded()
{ return visitsNeeded; }
/** Described in part (b) */
public static String topItem(String receipt)
{ /* to be implemented in part (b) */ }
// There may be instance variables, constructors, and methods that are not shown.
}Example for part (a): new RewardsCard(100, 20), assuming bonusPoints(points) returns points / 10
| Visit | Total after adding 20 | Bonus | Total after the bonus |
|---|---|---|---|
| 1 | 20 | 2 | 22 |
| 2 | 42 | 4 | 46 |
| 3 | 66 | 6 | 72 |
| 4 | 92 | 9 | 101 |
Source: Hypothetical example
Suggested time: 25 minutes
Your answers are saved in this browser as you type.
Part (a)
4 pointsWrite the constructor for RewardsCard. A new card starts with 0 points. On each visit, perVisit points are added to the total, and then the value of bonusPoints for that new total is added as well. The constructor sets visitsNeeded to the number of visits it takes for the total to become greater than or equal to goal.
In the example above, the total first reaches 100 on visit 4, so visitsNeeded is set to 4. Assume goal > 0 and perVisit > 0.
You must use bonusPoints appropriately to receive full credit.
Complete the RewardsCard constructor.public RewardsCard(int goal, int perVisit)
Tab indents. Press Esc, then Tab, to move on.
0 lines · 0 / 2,500 characters
Part (b)
3 pointsA receipt String lists items and the points each one earned. Entries are separated by commas, and each entry is an item name, a colon and a whole number, with no spaces. For example: "latte:40,bagel:15,juice:40,muffin:25".
Write the static method topItem, which returns the name of the item that earned the most points. If two or more items tie for the most points, return the one that appears first. For the receipt above, topItem returns "latte". Assume receipt has at least one entry.
Complete method topItem.public static String topItem(String receipt)
Tab indents. Press Esc, then Tab, to move on.
0 lines · 0 / 2,500 characters
Checking scoring…
Scoring it yourself shows you the rubric, examples and a model answer. Try writing your answer first.