Java Reference
In-Depth Information
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
this
(name,
20
, weight, height);
18 }
19
20
public double
getBMI() {
21
double
bmi = weight * KILOGRAMS_PER_POUND /
22 ((height * METERS_PER_INCH) * (height * METERS_PER_INCH));
23
getBMI
return
Math.round(bmi *
100
) /
100
.
0
;
24 }
25
26
public
String getStatus() {
getStatus
27
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.8. 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.8.
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
combines the power of the procedural paradigm with an added dimension that integrates data
with operations into objects.
In procedural programming, data and operations on the data are separate, and this meth-
odology requires passing data to methods. Object-oriented programming places data and
procedural vs. object-oriented
paradigms
Search WWH ::
Custom Search