I want to calculate a person's BMI by passing an instance of that person as parameter in class BMI static method calculate() as follows:
Main.java
public class Main {
public static void main(String[] args) {
John john = new John(182, 68);
BMI.calculate(john); // NaN???
}
}
Physique.java
public interface Physique {
int height = 0;
int weight = 0;
}
John.java (implements Physique interface)
public class John implements Physique {
int height;
int weight;
int age;
public John(int height, int weight, int age) {
this.weight = weight;
this.height = height;
}
}
BMI.java
public class BMI {
public static void calculate(Physique physique) { // problem?
System.out.println(physique.height / Math.pow(physique.weight, 2));
}
}
I figure the problem should be my misuse of interface as type in paramater in BMI class.
It works if I use calculate(John john)
, but if I have another person, that won't work. How could I correctly implement this?