Tracing two step counters

A fitness app uses the following Java class to track daily steps against a goal.


Java
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.


Java
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.

Your answers

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Instance Variables and Class Variables Questions: