Skip Top Navigation Bar

AP Computer Science A Free Response Practice Using Loops

Parking Meter

This single part question is worth 4 points.

AP CSA Alignment

Unit Alignment Learning Objectives Skills
Unit 1: Selection and Iteration 1.14.A Develop code to call instance methods and determine the result of these calls. 2.C Write program code involving procedural abstractions.
Unit 2: Selection and Iteration 2.3.A Develop code to respresent branching logical processes by using selection statements and determine the result of these processes. 2.A Write program code to implement an algorithm.
Unit 2: Selection and Iteration 2.7.B Develop code to represent iterative processes using `while` loops and determine the result of these processes. 2.A Write program code to implement an algorithm.
Unit 2: Selection and Iteration 2.9.A Develop code for standard and original algorithms (without data structures) and determine the result of these algorithms. 2.A Write program code to implement an algorithm.

A parking meter collects coins as payment for parking a car in a parking spacing. The ParkingMeter class simulates the parking meter.

The parking meter accepts coins in increments of 5 cents, 10 cents, or 25 cents. A meter starts with a zero balance and each coin inserted adds its value to the current balance. There is no maximum limit to the amount of money that can be deposited.

The getACoin method returns a coin that is worth 5 cents, 10 cents, or 25 cents.

public class ParkingMeter
{
    /** The current balance in cents */
    private int balance;

    /** Constructs an empty ParkingMeter 
     */
    public ParkingMeter() { 
        balance = 0;
    } 

    /**
     * Returns a random coin with the value 5, 10, or 25. 
     */
    public int getACoin() {
        // Your code goes here
    }

    /**
     * Adds enough coins to the meter to cover the cost of
     * parking in the parking space for the provided number
     * of minutes, as described below, and returns the 
     * number of coins required to pay for the parking.
     * precondition: minutes > 0
     */
    public int payForParking(int minutes) {

    }

    // Other methods not shown
}

Part a. Write a method called payForParking that takes a positive integer parameter minutes, representing the number of minutes a car will be parked in the parking space. Each 15 minutes of parking costs 25 cents. The method should continuously call getACoin method until enough money has been paid to cover the cost for the desired parking time. The method returns the number of coins deposited into the meter in the process.

Write Your Response

  • In order to test your program, we have provided some values for the getACoin method to return, rather than it being truly random. You can modify these values to create additional tests if you want.
  • Write code to complete the payForParking() method in the Java Playground below.
  • Uncomment each test individually and run the code in the Java Playground. All four test should return true if your code is correct.