Chapter 10
Chapter 10
电子与信息工程学院课后作业
班级 1 学号 2362720018 姓名 孙安家
作业日期: 2024 年 6 月 19 日
Chapter10 Thinking in Objects
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)
+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;
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:
/**
* <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();