Verityprep
Question Bank · Unit 3

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.

Questions 21–30 of 43
  1. 21
  2. 22
  3. 23
  4. 24
  5. 25
  6. 26
  7. 27
  8. 28
  9. 29
  10. 30

Question 21

Free response Writing Classes
The CupcakeMachine class, which you will write, represents a cupcake vending machine, an automated machine that dispenses cupcakes. CupcakeMachine objects are…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 22

Free response Writing Classes
This question involves the SignedText class, which contains methods that are used to include a signature as part of a string of text. You will write the…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 23

Free response Writing Classes
Write the complete SummerSpecial class for a pair of FurnitureItem objects. FurnitureItem provides String getProductID() and double getPrice(). IDs have at…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 24

Free response Writing Classes
Write a complete class TemperatureLog that tracks daily temperature readings. Below is a specification table showing how the class should behave: STATEMENT…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 25

Free response Writing Classes
Design and implement a complete class Scoreboard that tracks players and their scores in a game. The table below describes the required behavior: STATEMENT…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 26

Free response Writing Classes
This question involves the implementation of a music playlist system that is represented by the Playlist class. A Playlist object is created with a parameter…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 27

Free response Writing Classes

This question involves the implementation of a vending machine system that is represented by the VendingMachine class. A VendingMachine object is created with two parameters: the price of the item in cents and the initial number of items in stock.

The VendingMachine class provides a constructor and the following methods.

  • purchase, which attempts a purchase and returns the change in cents, or -1 if the purchase fails
  • restock, which adds items to the stock
  • getRevenue, which returns the total revenue collected in cents
  • getStock, which returns the number of items remaining

The following table contains a sample code execution sequence and the corresponding results.

STATEMENTS AND EXPRESSIONSVALUE RETURNEDCOMMENT
VendingMachine vm = new VendingMachine(150, 3);Price is 150 cents per item. 3 items in stock.
vm.getStock()3
vm.getRevenue()0No purchases have been made.
vm.purchase(200)50Paid 200, price is 150, change is 50.
vm.getStock()2One item sold.
vm.getRevenue()150Revenue from one sale.
vm.purchase(100)-1Payment of 100 is less than price of 150.
vm.getStock()2No change, purchase failed.
vm.purchase(150)0Exact change, no money returned.
vm.purchase(200)50
vm.getStock()0All items sold.
vm.getRevenue()450Three successful purchases.
vm.purchase(200)-1Out of stock.
vm.getStock()0
vm.restock(2)Two items added.
vm.getStock()2
vm.purchase(175)25
vm.getRevenue()600Four total successful purchases.

Write the complete VendingMachine class, including the constructor and any required instance variables and methods. Your implementation must meet all specifications and conform to the example.

public class VendingMachine {

}
The answer and the full solution open in a window.

Question 28

Free response Writing Classes

A downtown parking garage charges a flat rate per hour for each car that parks. The garage has a fixed number of spaces. When a car enters, it occupies one space. When a car exits, the system calculates the charge based on the number of hours parked and adds it to the total revenue. A car cannot enter if no spaces are available. The following table shows an example sequence of operations on a ParkingGarage object.

STATEMENTS AND EXPRESSIONSVALUE RETURNEDCOMMENT
ParkingGarage g = new ParkingGarage(3, 5);Three spaces, $5 per hour
g.getOpenSpaces()3
g.enter()trueCar enters successfully
g.enter()trueSecond car enters
g.getOpenSpaces()1
g.enter()trueThird car enters
g.enter()falseGarage is full
g.exit(3)15Car parked 3 hours: 3 × 5 = 15
g.getOpenSpaces()1One space freed
g.exit(1)5Car parked 1 hour: 1 × 5 = 5
g.getRevenue()20Total revenue: 15 + 5

Write the complete ParkingGarage class, including the constructor and any required instance variables and methods. Your implementation must meet all specifications and conform to the example.

public class ParkingGarage {
The answer and the full solution open in a window.

Question 29

Free response Writing Classes
This question involves the implementation of a fitness tracking system that is represented by the StepTracker class. A StepTracker object is created with a…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 30

Free response Writing Classes
Dice Record Game Write a class that models a dice game played in rounds. When an object is created, it receives an initial record score. This value represents…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 21 of 43
Page 3 of 5