In: Computer Science
JAVA
Here is my code:
public class BMI { private String name; private Double height; private Double weight; Double getBMI(){ return weight/(height*height); } public BMI(String name, Double height, Double weight){ this.name = name; this.height = height; this.weight = weight; } @Override public String toString(){ return name + " is " + height +"m tall and " + weight + "kg and has a BMI of" + getBMI() ; } }
Have a look at the below code. I have put comments wherever required for better understanding.
// create class
class BMI{
// create instance variables
public String name;
public double height;
public double weight;
// create constructor
BMI(String name,double height,double weight){
this.name = name;
this.height = height;
this.weight = weight;
}
// create get BMI method
public double getBMI(){
double numerator = this.weight;
double denominator = this.height*this.height;
return numerator/denominator;
}
// create toString method
public String toString(){
return this.name + " is " + this.height +"m tall and " + this.weight + "kg and has a BMI of " + getBMI();
}
}
// create main class
class Main {
public static void main(String[] args) {
// create an object BMI class
BMI bmi = new BMI("Fred",1.9,87.0);
// call the toString method...
System.out.println(bmi.toString());
}
}
Happy Learning!