A parking meter class stores the minutes remaining and counts rejected requests:
public class ParkingMeter { private int minutes = 30; private int rejected = 0; public int getMinutes() { return minutes; } public int getRejected() { return rejected; } public boolean setMinutes(int newMinutes) { if (newMinutes < 0 || newMinutes > 120) { rejected++; return false; } minutes = newMinutes; return true; } public void addMinutes(int extra) { if (!setMinutes(minutes + extra)) { setMinutes(120); } } }
A client runs these statements in order:
ParkingMeter m = new ParkingMeter(); m.addMinutes(45); // statement 1 m.setMinutes(-10); // statement 2 m.addMinutes(60); // statement 3 boolean ok = m.setMinutes(m.getMinutes() - 50); // statement 4 m.addMinutes(m.getMinutes()); // statement 5
Find the value of minutes right after statement 4, the final value of minutes, the final value of rejected, and the value stored in ok. Give the three counts as whole numbers and ok as true or false.
Unlock full course notes by signing up
Repair the treadmill setter
A treadmill stores its speed in the field shown below, but its intended mutator leaves that field unchanged. The supplied parameter has the same name as the field.
Keep the right-hand side as speed. Replace only the left-hand side with ________ so that the assignment updates the object's field.
Separate displayed points from stored points
A rewards account begins with 82 points and provides the methods below. After the calling code finishes, determine the local variable displayed and the account's stored points. Give both answers as exact whole numbers of points.
The following statements run on that account:
Match accessor and mutator vocabulary
Each term below describes part of how a class lets other code read and update its stored values. Match every term with the description of its role in a getter or setter.
Diagnose a getter that changes the count
A step counter starts with steps equal to 6. Its programmer intended getSteps() to be an accessor, but the implementation contains an unintended update. Read both methods and the calling code, then select every true statement.
Starting from the initial value of 6, the following statements run once: