Free response

Parking meter charges and masked plates

Methods and Control Structures, 7 pointsAbout 23 minutes · 7 points

A city parking meter charges by the hour. The price of an hour depends on the time of day, and no single stay costs more than a daily maximum. The meter also prints receipts that hide most of a car's license plate. You will write two methods of the ParkingMeter class.public class ParkingMeter { /** Returns the price, in cents, of parking during the * hour that starts at hour (0 to 23). */ public int rateFor(int hour) { /* implementation not shown */ } /** Returns the most a driver pays for a stay, in cents. */ public int getDailyMax() { /* implementation not shown */ } /** Returns the cost, in cents, of parking for the given * number of whole hours starting at startHour, as * described in part (a). * Precondition: 0 <= startHour <= 23, 1 <= hours <= 24 */ public int costOfStay(int startHour, int hours) { /* to be implemented in part (a) */ } /** Returns plate with every character except the last * two replaced by "*", as described in part (b). * Precondition: plate.length() >= 2 */ public static String maskPlate(String plate) { /* to be implemented in part (b) */ } /* There may be instance variables, constructors, * and methods that are not shown. */ }Examples for part (a): the meter in these examples has the rates below and a daily maximum of 1500 cents.

HoursValue of rateFor(hour)
0 to 750
8 to 17300
18 to 21150
22 and 2350
CallHours chargedReturn value
costOfStay(16, 3)16, 17, 18750
costOfStay(22, 4)22, 23, 0, 1200
costOfStay(7, 8)7 to 141500 (the total, 2150, is above the maximum)

Examples for part (b):

CallReturn value
maskPlate("KX4729")"****29"
maskPlate("7TRC118")"*****18"
maskPlate("AB")"AB"