A scorekeeping program uses the following Java class.
public class Tally { private int total; public void add(int amount) { int total = this.total + amount; } public void addTwice(int amount) { total += amount; total += amount; } public int getTotal() { return total; } }
The program runs the statements below. What value does the final statement print? Give your answer as an integer.
Tally t = new Tally(); t.add(40); t.addTwice(15); t.add(25); t.addTwice(10); System.out.println(t.getTotal());
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.
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.
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.