Quiz-2 / Feb. 28, 2008

Write a class named Person that has following properties:

a)    It stores two global variables for height (inch)  and weight (pounds)

b)    Write a Default constructor with no parameters ( initialize parameters as 65 inches, 110 pounds)

c)    Write a Default constructor with two parameters

d)    Write a method that returns the body mass index of the person using following formula

e)    Write the necessary statement to create a person object named Zeynep with height=60 inches and 120 pounds.

f)       Write a statement to calculate bmi index of Zeynep  and assign it to local variable then print it.

Solution

class Person

{

            private int height;

            private int weight;

            public Person()

            {

                        height =  65;

                        weight =  110;

            }

            public Person(int height,int weight)

            {

                        this.height = height;

                        this.weight = weight;

            }

            public double getBmiIndex()

            {

                        return (weight/Math.pow(height,2))*703.0;

            }

            public static void main(String [] args)

            {

                        Person zeynep = new Person(60,120);

                        double bmi = zeynep.getBmiIndex();

                        System.out.println("BMI Index of Zeynep is " + bmi);

            }

}