A Java ticket office uses the class below. In a fresh run, the shown statements execute inside main, with a and b referring to distinct objects. Select every correct statement about transfer and the resulting state.
class TicketCounter { int sold = 0; static int total = 0; void sell(int amount) { sold = sold + amount; total = total + amount; } static int transfer(TicketCounter from, TicketCounter to, int amount) { from.sold = from.sold - amount; to.sold = to.sold + amount; return total; } }
TicketCounter a = new TicketCounter(); TicketCounter b = new TicketCounter(); a.sell(6); b.sell(4); int reported = TicketCounter.transfer(a, b, 2);
a.sold becomes 4 and b.sold becomes 6 because the transfer changes their separate stored counts.
transfer can update from.sold because its parameter identifies an object whose field can be selected.
reported becomes 12 because adding two tickets to to.sold also increases the class's shared total.
transfer can replace from.sold with this.sold because the caller supplies the first object argument.
Unlock full course notes by signing up
Tracking cart-line quantities
A Java shopping program uses the class below. In a fresh run, the statements shown execute inside main in order. Complete the table after each call using exact integer numbers of units.
Resolving two meanings of points
A Java game uses the following class to preview a player's score. Inside main, Score score = new Score(); has executed. What does score.projected(3) return? Give your answer in points, to the nearest whole point.
When does the meter class get ready?
A utility company models its water meters with the Java class below. The main method of another class runs the four statements shown after it. Put the lines the program prints in the order they appear on the console.
Live rate versus locked-in rate
A credit union's savings software uses the Java class below. Each account computes one year of interest two ways: with the rate shared by the whole class, and with the rate copied when the account was opened.
Another class then runs these statements:
After the last statement, find the value returned by each of x.currentInterest(), x.lockedInterest(), y.currentInterest() and y.lockedInterest(). Give each in dollars to two decimal places.