Write the same program above using classes, methods and constructors. Create a c
ID: 3816885 • Letter: W
Question
Explanation / Answer
HealthDemo.java
public class HealthDemo {
public static void main(String[] args) {
WH w1 = new WH(145, 72);
w1.message(w1.ratio());
WH w2 = new WH(163, 72);
w2.message(w2.ratio());
WH w3 = new WH(165, 72);
w3.message(w3.ratio());
WH w4 = new WH(185, 72);
w4.message(w4.ratio());
}
}
WH.java
public class WH {
private int height, weight;
public WH(int weight, int height){
this.height = height;
this.weight= weight;
}
public double ratio() {
return weight/(double)height;
}
public void message(double ratio){
System.out.format("Ration is %.3f ", ratio);
if(ratio < 2.1){
System.out.println("Weight and Height ratio is normal");
}
else if(ratio >= 2.1 && ratio <2.3){
System.out.println("Weight and Height ratio is slightly above average");
}
else if(ratio >= 2.3 && ratio <2.5){
System.out.println("Need to lost 5% percent of weight");
}
else{
System.out.println("Need to lost more than 5% percent of weight");
}
}
}
Output:
Ration is 2.014
Weight and Height ratio is normal
Ration is 2.264
Weight and Height ratio is slightly above average
Ration is 2.292
Weight and Height ratio is slightly above average
Ration is 2.569
Need to lost more than 5% percent of weight
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.