Beruflich Dokumente
Kultur Dokumente
TODO 1
Create a class BMICalculator with
Instance variables: height, weight
Method : calculateBMI() this method accepts height and weight as parameters and returns
calculated bmi and comment.
[use different parameter names not same as instance variables]
Create class that creates objects for BMICalculator and tests it.
< 18.5kg/m2 UnderWeight
18.5 - 25kg/m2 Healthy
25 30 kg/m2 - slightly overweight
> 30kg/m2 heavily overweight
Weight in Kilograms
99.79 Kg
= 27.5
(1.905 m) x (1.905 m)
Sample output:
Your BMI is 24.5 Healthy
Method Overloading
Methods of the same name can be declared in the same class, as long as they have different sets
of parameters (determined by the number, types and order of the parameters)
Purpose:
Perform the same or similar tasks, but on different types or different numbers of arguments.
Example 5:
public class compute
{
int square(int x)
{
System.out.println("Called square with argument: "+x);
return x*x;
}
double square(double y)
{
System.out.println("Called square with argument: "+y);
return y*y;
}
public static void main(String[] args)
{
compute c1=new compute();
System.out.println("Square of integer 8 equals to "+c1.square(8));
System.out.println("Square of double 8.5 equals to "+c1.square(8.5));
}
}
}
TODO 2
Modify the BMICalculator class created above as follows:
Add another instance variables : unitHeight, unitWeight
Add another overloaded method : calculateBMI() with parameters height, weight, unitForHeight and
unitForWeight. [use different parameter names not same as instance variables]
This method should identify the units given for height and weight and then use the appropriate formula
to calculate BMI.
The formula to calculate BMI when unitForHeight is in inches and unitForWeight is in pounds
BMI =
Weight in Pounds
x 703
For example, a person who weighs 220 pounds and is 75 inches tall has a BMI of 27.5.
220 lbs.
(75 inches) x (75 inches)
) x 703 = 27.5
Constructor
Example 6:
class Circle
{
int radius;
Circle (int r)
{
radius = r;
}
float area() {
return (3.14*radius*radius);
}
float circumference() {
return (2*3.14*radius);
}
}
TODO 3
Modify the BMICalculator class created above as follows:
Create a constructor to initialize height and weight