SLIDE 1
Encapsulation (1.1)
Consider the following problem:
- A person has a name, a weight, and a height.
- A person’s weight may be in kilograms or pounds.
- A person’s height may be in meters or inches.
- A person’s BMI is calculated using their height in meters and
weight in kilograms. Consider a first solution:
class Person { public String name; public double weight; /* in kilograms */ public double height; /* in meters */ public double getBMI() { return weight / (height * height); } }
- Since both attributes height and weight are declared as
public, we do not need the setter methods for them.
2 of 9