I think I have a solution that will not require you to add any instances or static variables at all.
Since inside this block of code you already have a local variable called weight .
public int weight(Person person) { int weight = person.getWeight(); /return the weight of the person return weight; }
Therefore, as part of this method, you have two variables called weight , but one of them is an instance variable and the other is a local variable. You can distinguish between them by writing this.weight for the instance variable or just weight for the local variable.
All you have to do is rename the current instance variable weight to weightsMeasured so that it weightsMeasured which variable you are accessing.
And then just add weightsMeasured++ to the weight(Person person) method and return weightsMeasured from totalWeightsMeasured() method.
So, the final code will look like this:
public class Reformatory { private int weightsMeasured;
Therefore, instead of adding any new variables, we simply renamed an existing instance variable that was not used.
source share