Java Reference
In-Depth Information
7
public static final double
METERS_PER_INCH =
0
.
0254
;
8
9
public
BMI(String name,
int
age,
double
weight,
double
height) {
constructor
10
this
.name = name;
11
this
.age = age;
12
this
.weight = weight;
13
this
.height = height;
14 }
15
16
public
BMI(String name,
double
weight,
double
height) {
constructor
17
18 }
19
20
21
double
bmi = weight * KILOGRAMS_PER_POUND /
22 ((height * METERS_PER_INCH) * (height * METERS_PER_INCH));
23
this
(name,
20
, weight, height);
public double
getBMI() {
getBMI
return
Math.round(bmi *
100
) /
100
.
0
;
24 }
25
26
27
public
String getStatus() {
getStatus
double
bmi = getBMI();
28
if
(bmi <
18.5
)
29
return
"Underweight"
;
30
else if
(bmi <
25
)
31
return
"Normal"
;
32
else if
(bmi <
30
)
33
return
"Overweight"
;
34
else
35
return
"Obese"
;
36 }
37
38
public
String getName() {
39
return
name;
40 }
41
42
public int
getAge() {
43
return
age;
44 }
45
46
public double
getWeight() {
47
return
weight;
48 }
49
50
public double
getHeight() {
51
return
height;
52 }
53 }
The mathematical formula for computing the BMI using weight and height is given in Section 3.9.
The instance method
getBMI()
returns the BMI. Since the weight and height are instance
data fields in the object, the
getBMI()
method can use these properties to compute the BMI
for the object.
The instance method
getStatus()
returns a string that interprets the BMI. The interpre-
tation is also given in Section 3.9.
This example demonstrates the advantages of the object-oriented paradigm over the proce-
dural paradigm. The procedural paradigm focuses on designing methods. The object-oriented
paradigm couples data and methods together into objects. Software design using the object-
oriented paradigm focuses on objects and operations on objects. The object-oriented approach
procedural vs. object-oriented
paradigms