A theatre uses the following Java class to track a pool of 24 available seats and the seats reserved by each booking object. The statements run once in a fresh program, in the order shown.
class Booking { static int available = 24; int reserved = 0; }
Booking first = new Booking(); first.reserved += 6; Booking.available -= 6; Booking second = new Booking(); // Checkpoint: Second booking created second.reserved += 4; Booking.available -= 4; // Checkpoint: Second reservation completed first.reserved -= 2; Booking.available += 2; // Checkpoint: Two seats returned
Complete the table with the value of each field at each checkpoint. Enter exact whole numbers of seats. Assume no other code changes these fields.
Unlock full course notes by signing up
Output of a constructor with a shadowed field
Consider the following Java class, written for a bookstore inventory program.
What is printed when the following code runs?
Tracing two step counters
A fitness app uses the following Java class to track daily steps against a goal.
The app then runs these statements in order.
After the last line runs, find the value stored in x, the value stored in y, and the value of the steps field in the object that raj refers to. Give each as an integer.
A local variable that shadows a field
A scorekeeping program uses the following Java class.
The program runs the statements below. What value does the final statement print? Give your answer as an integer.
Initialize individual targets from a changing default
A greenhouse program uses a shared default temperature when creating individual heater settings. All temperatures are in °C. The following Java statements run once in a fresh program, in the order shown, with no other assignments.
Determine the final values of first.target, second.target, third.target, and HeaterSetting.defaultTarget. Give every temperature in °C, to zero decimal places.