0% found this document useful (0 votes)
4 views

Chapter 10

programming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Chapter 10

programming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

台州学院

电子与信息工程学院课后作业

班级 1 学号 2362720018 姓名 孙安家

作业日期: 2024 年 6 月 19 日
Chapter10 Thinking in Objects

Project: Calculate BMI

Problem Description:
1. Create a java class to calculate the BMI
2. The class should have data field: name age weight height
3. The class should have methods: getBMI getStatus
4. Write a Test class to test the BMI class

Analysis:
(Describe the problem including input and output in your own words.)

This program is for calculating the body mass index (BMI). Here we have to create a
class to calculate the BMI and it should contain data field: name, age, weight and
height. getBMI and get Status methods should be used to write the program. Also we
have to write a Test class to test the BMI class.

Design:
(Describe the major steps for solving the problem. Draw the UML class diagram for
BMI)

The major steps for solving the problems are:


1. We need to make two classes one named BMI and the other one named
TestBMI.
2. Firstly we need a static method to compute the body mass index as
follows:
3. public static double getBMI (double weight, double height).
4. We use methods : getBMI, getStatus to write the code.
Here is the UML class diagram for BMI:
BMI
- name: String The name of the person.
- age: int The age of the person.
- weight: double The weight of the person in pounds.
- height: double The height of the person in inches.

Creates a BMI object with the


+BMI(name: String, age: int, weight: specified name, age, weight and
double, height: double) height.

+BMI (name: String, weight: double, Create a BMI object with the
height: double) specified name, weight, height, and
default age.
+getBMI(): double
Returns the BMI.
+getstatus(): String Returns the BMI status (e.g.,
normal, overweight, etc.

Coding: (Copy and Paste Source Code here. Format your code using Courier
10pts)

Assuming BMI class is available. The code below gives a test program that uses this
class.

import java.text.DecimalFormat;

public class TestBMI {


public static void main(String[] args) {
DecimalFormat df = new DecimalFormat("#.##");

BMI bmi1 = new BMI("Noor", 25, 90, 1.77);


System.out.println(bmi1.getName() + "'s BMI is " +
df.format(bmi1.getBMI()) + ", he is "+ bmi1.getStatus()+ ".");

BMI bmi2 = new BMI("Lina", 58, 1.72);


System.out.println(bmi2.getName() + "'s BMi is " +
df.format(bmi2.getBMI()) + ", she is " + bmi2.getStatus()+ ".");
}
}

Here line 7 creates the object bmi1 for Noor and line 10 creates the object bmi2 for
Lina. Now we can use the instance methods getName(), getBMI() and getStatus() to
return the BMI information in a BMI object.
The BMI class can be implemented as in the below code:

public class BMI {


private String name;
private int age;
private double weight;
private double height;

public BMI(String name, int age, double weight, double height) {


this.name = name;
this.age = age;
this.weight = weight;
this.height = height;
}

public BMI(String name, double weight, double height){


this.name =name;
this.weight = weight;
this.height = height;
}

public double getBMI() {


return weight / (height * height);
}

/**
* <18.5 underweight
* 15.8 <=BMI < 25.0 normal
* 25.0 <= BMI < 30.0 Overweight
* 30.0 <= BMI Obese
*/
public String getStatus() {
double bmi = getBMI();

if (bmi <= 18.5) {


return "Underweight";
} else if (bmi >= 18.5 && bmi < 25.0) {
return "Normal";
} else if (bmi >= 25.0 && bmi < 30.0) {
return "Overweight";
} else {
return "Obese";
}
}

// get methods for data fields.


public String getName() {
return this.name;
}

public int getAge() {


return age;
}

public double getWeight() {


return weight;
}

public double getHeight() {


return height;
}
}

Testing: (Describe how you test this program)


To test this program we can try these few steps:
1. Create instance of the BMI class with different sets of input values, including
name, age, weight, and height.
2. Use the getBMI() method to calculate the BMI for each person and verify that
the values and computed correctly.
3. Use the getStatus() method to determine the BMI status for each person and
ensure that the correct status is returned.
4. Check the get methods to retrieve the name, age, weight and height for each
person and verify that the values match the unput given during object creation.
5. Repeat the steps above with different inputs and test cases to cover a wide
range of scenarios, such as underweight, normal weight, overweight and
obese.

You might also like