Methods to Build a BMI Calculator Using Java OOP
Building a BMI (Body Mass Index) calculator is not only a practical programming exercise but also an excellent way to explore Java's Object-Oriented Programming (OOP) principles. This guide will walk you through creating a robust BMI calculator u...

- Input: Weight (kg or lbs) and height (cm or inches).
- Processing: Calculation based on the formula.
- Output: BMI value and corresponding category (e.g., Normal, Overweight).
- Person: Represents the individual whose BMI is being calculated.
- BMICalculator: Handles the computation of BMI.
- BMICategory: Maps BMI ranges to categories like Normal or Overweight.
- The Person class will hold weight and height attributes.
- The BMICalculator class will use the Person object to compute BMI.
- The BMICategory class will interpret the BMI value and determine the category.
public class Person {
private double weight; // in kilograms
private double height; // in meters
public Person(double weight, double height) {
this.weight = weight;
this.height = height;
}
public double getWeight() {
return weight;
}
public double getHeight() {
return height;
}
public void setWeight(double weight) {
this.weight = weight;
}
public void setHeight(double height) {
this.height = height;
}
}
- Encapsulation: The Person class encapsulates weight and height, exposing them through getter and setter methods.
- Reusability: This class can be reused in other health-related applications.
public class BMICalculator {
public double calculateBMI(Person person) {
double weight = person.getWeight();
double height = person.getHeight();
if (height <= 0) {
throw new IllegalArgumentException("Height must be greater than 0.");
}
return weight / (height * height);
}
}
- Responsibility: This class focuses solely on BMI calculation.
- Validation: It checks for invalid height to prevent runtime errors.
public class BMICategory {
public String determineCategory(double bmi) {
if (bmi < 18.5) {
return "Underweight";
} else if (bmi >= 18.5 && bmi < 24.9) {
return "Normal weight";
} else if (bmi >= 25 && bmi < 29.9) {
return "Overweight";
} else {
return "Obesity";
}
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your weight (kg): ");
double weight = scanner.nextDouble();
System.out.println("Enter your height (m): ");
double height = scanner.nextDouble();
Person person = new Person(weight, height);
BMICalculator calculator = new BMICalculator();
BMICategory category = new BMICategory();
double bmi = calculator.calculateBMI(person);
String bmiCategory = category.determineCategory(bmi);
System.out.printf("Your BMI is: %.2f ", bmi);
System.out.println("You are classified as: " + bmiCategory);
}
}
- User Interaction: The Main class integrates the system, providing a user-friendly interface.
- Scalability: Additional features, such as unit conversion, can be added without modifying existing classes.
public class UnitConverter {
public static double poundsToKilograms(double pounds) {
return pounds 0.453592;
}
public static double inchesToMeters(double inches) {
return inches 0.0254;
}
}
Read more at : Methods to Build a BMI Calculator Using Java OOP
About the author
I am Tuanh.net. As of 2024, I have accumulated 8 years of experience in backend programming. I am delighted to connect and share my knowledge with everyone.
Comments
Join the discussionNo comments yet. Be the first to comment.





