How to find out how many times a method has been called from the main class?

My problem is to find out how many times the weight() method is called from the main class. I have to calculate it in the totalWeightsMeasured() method.

The output code should be 0.2.6. (EDIT // I used to be 0.2.4 here, but the output should really be 0.2.6)

But I just don’t know how you can calculate it, and I tried to do google and that’s it, but I just don’t know how to do it. (and you should not add more instance variables )

CLASS:

 public class Reformatory { private int weight; public int weight(Person person) { int weight = person.getWeight(); // return the weight of the person return weight; } public void feed(Person person) { //that increases the weight of its parameter by one. person.setWeight(person.getWeight() + 1); } public int totalWeightsMeasured() { return 0; } } 

MAIN:

 public class Main { public static void main(String[] args) { Reformatory eastHelsinkiReformatory = new Reformatory(); Person brian = new Person("Brian", 1, 110, 7); Person pekka = new Person("Pekka", 33, 176, 85); System.out.println("total weights measured "+eastHelsinkiReformatory.totalWeightsMeasured()); eastHelsinkiReformatory.weight(brian); eastHelsinkiReformatory.weight(pekka); System.out.println("total weights measured "+eastHelsinkiReformatory.totalWeightsMeasured()); eastHelsinkiReformatory.weight(brian); eastHelsinkiReformatory.weight(brian); eastHelsinkiReformatory.weight(brian); eastHelsinkiReformatory.weight(brian); System.out.println("total weights measured "+eastHelsinkiReformatory.totalWeightsMeasured()); } } 
+5
source share
4 answers

The trick is to use the existing variable weight of the instance, which is not yet used as a counter.

 public class Reformatory { private int weight; public int weight(Person person) { int weight = person.getWeight(); this.weight++; // return the weight of the person return weight; } public void feed(Person person) { //that increases the weight of its parameter by one. person.setWeight(person.getWeight() + 1); } public int totalWeightsMeasured() { return weight; } } 
+2
source

As stated by Satya, enter the static counter variable as follows:

 public class Reformatory { private static int weightMessurements = 0; public int weight(Person person) { // increment counter weightMessurements++; // messure weight return person.getWeight(); } public void feed(Person person) { // increase weight person.setWeight(person.getWeight() + 1); } public int totalWeightsMeasured() { int result = weightMessurements; // reset counter so that the output matches 0,2,4 instead of 0,2,6 weightMessurements = 0; return result; } } 
+1
source

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; //Renamed instance variable public int weight(Person person) { weightsMeasured++; //instance variable incremented int weight = person.getWeight(); //This is the local variable //return the weight of the person return weight; } public void feed(Person person) { //that increases the weight of its parameter by one. person.setWeight(person.getWeight() + 1); } public int totalWeightsMeasured() { //return the number of times the method was called return weightsMeasured; } } 

Therefore, instead of adding any new variables, we simply renamed an existing instance variable that was not used.

0
source

A slightly modified and shorter version of the answers above:

 public class Reformatory { private int weight; public int weight(Person person) { this.weight++; //count the number of times when the weight gets measured. return person.getWeight(); } public int totalWeightMeasured() { return weight; //return the number of times when the weight gets measured. } 

}

0
source

Source: https://habr.com/ru/post/1237236/


All Articles