1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
package Q7;
import java.util.*;
public class Person {
private double bmi;
private double height;
private double weight;
private String category;
private String name;
public Person(String name, double weight, double height) {
this.name = name;
this.weight = weight;
this.height = height;
this.updateBMI();
}
public String getCategory() {
return this.getCategory(this.bmi);
}
public String getCategory(double bmi) {
if (bmi < 18.5) {
return "Underweight";
} else if (bmi < 25.0) {
return "Normal";
} else if (bmi < 30.0) {
return "Overweight";
}
return "Obese";
}
public String getName() {
return this.name;
}
public double getBMI() {
return this.bmi;
}
public double getHeight() {
return this.height;
}
public double getWeight() {
return this.weight;
}
public void setBMI(double bmi) {
this.bmi = bmi;
}
public void setHeight(double height) {
this.height = height;
this.updateBMI();
}
public void setName(String name) {
this.name = name;
}
public void setWeight(double weight) {
this.weight = weight;
this.updateBMI();
}
// BMI = (weight (lb) * 703) / ((height (in))^2)
private void updateBMI() {
this.setBMI((this.weight * 703) / Math.pow(height, 2));
}
public static void main(String[] args) {
ArrayList<Person> people = new ArrayList<Person>();
people.add(new Person("Andrew", 125.5, 55.1));
people.add(new Person("Boyd", 150.0, 67.0));
people.add(new Person("Cathy", 135.0, 72.3));
people.add(new Person("Donna", 190.0, 64.0));
System.out.println(String.format("%-20s Weight Height BMI Category", "Name"));
System.out.println("-----------------------------------------------");
for (Person person : people) {
System.out.println(
String.format(
"%-20s %+5.1f %+6.1f %+3.0f %s",
person.getName(),
person.getWeight(),
person.getHeight(),
person.getBMI(),
person.getCategory()));
}
}
}
|