A fitness app uses the following Java class to track daily steps against a goal.
public class StepCounter { private int steps; private int goal; public StepCounter(int goal) { this.goal = goal; } public void walk(int n) { steps += n; } public int remaining() { return Math.max(goal - steps, 0); } }
The app then runs these statements in order.
StepCounter ana = new StepCounter(8000); StepCounter raj = new StepCounter(10000); ana.walk(3500); raj.walk(4200); ana.walk(5100); raj.walk(2600); int x = ana.remaining(); int y = raj.remaining();
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.
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?
Track a shared seat pool
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.
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.
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.