BMI Calculator Class in Java: Explanation & Practice
Create a class to calculate BMI
Problem summary
Create a Person class that calculates BMI from height and weight.
Starter code
// Create Person class with:
// - double weight (kg), double height (m)
// - double getBMI() returns weight / (height * height)
// - String getCategory() based on BMI
public class Main {
public static void main(String[] args) {
// Test case 1: weight=70, height=1.75
// Print: BMI: <value>, Category: <category>
// Categories: <18.5 Underweight, 18.5-24.9 Normal, 25-29.9 Overweight, 30+ Obese
}
}Expected output and test cases
- 70kg, 1.75m
BMI: 22.86, Category: Normal
- 55kg, 1.77m
BMI: 17.58, Category: Underweight
- 100kg, 1.77m
BMI: 32.00, Category: Obese
Hints
- BMI = weight / (height * height)
- Use if-else for categories
- Format BMI to 2 decimal places
Related OOP Basics exercises
- Practice Method Overloading in Java
- Practice Using 'this' Keyword in Java
- Practice Student Grade Calculator in Java
Practice all OOP Basics exercises · Run this idea in the Java compiler