Skip to main content

Question 2: Class Design

Parking meter

  • Units 2 and 3
  • 7 points
  • About 25 minutes

You write a whole class from a description and a table of example calls and results: the class header, private instance variables, a constructor and the required method. 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 2 of 4, 7 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

The ParkingMeter class, which you will write, represents a coin-operated parking meter.

A ParkingMeter object is created with an int parameter that gives the most minutes the meter can hold. Assume this value is greater than 0. A new meter shows 0 minutes.

The class has two methods.

- addQuarters has an int parameter, the number of quarters a driver puts in (assume it is greater than 0). Each quarter adds 15 minutes, but the time on the meter never goes above the most minutes it can hold. The method returns the number of minutes on the meter after the quarters are added.

- passTime has an int parameter, the number of minutes that go by (assume it is greater than 0). It takes that many minutes off the meter, but the time on the meter never goes below 0. It doesn't return a value.

The table shows a sample sequence of statements and their results. The statements are in a class other than ParkingMeter.

Sample statements and results

StatementValue returned (— if none)Explanation
ParkingMeter m = new ParkingMeter(60);—A meter that holds at most 60 minutes is created. It shows 0 minutes.
m.addQuarters(2)302 quarters add 30 minutes.
m.passTime(10);—10 minutes go by, so 20 minutes are left.
m.addQuarters(3)6020 + 45 = 65 is more than the meter holds, so it shows 60.
m.passTime(75);—75 minutes go by. The meter can't go below 0, so it shows 0.
m.addQuarters(1)150 + 15 = 15.
ParkingMeter small = new ParkingMeter(20);—A meter that holds at most 20 minutes.
small.addQuarters(1)1515 minutes.
small.addQuarters(1)2030 is more than 20, so the meter shows 20.

Source: Hypothetical example

Suggested time: 25 minutes

Your answers are saved in this browser as you type.

Something wrong with this question?

What's wrong?

Please don't include personal details.

Part (a)

7 points

Write the complete ParkingMeter class. Your implementation must meet all specifications and conform to the examples shown in the table.

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.