# Methods to Build a BMI Calculator Using Java OOP

<div><strong>Source</strong>: <a href='https://www.tuanh.net/blog/oop/methods-to-build-a-bmi-calculator-using-java-oop'>Methods to Build a BMI Calculator Using Java OOP</a></div><div class="line-post"> 
 <h2 class="title-post">1. What Is a BMI Calculator?</h2> 
</div>
<div class="line-post">
  BMI is a measure derived from a person’s weight and height to determine if they are underweight, normal, overweight, or obese. While the formula is straightforward, implementing it in Java offers numerous opportunities to showcase OOP concepts such as encapsulation, inheritance, and polymorphism. 
</div>
<div class="line-post"> 
 <h3 class="sub1">1.1 Understanding the Core Requirements</h3> 
</div>
<div class="line-post">
  A BMI calculator typically requires: 
</div>
<div class="line-post"> 
 <ul> 
  <li>Input: Weight (kg or lbs) and height (cm or inches).</li> 
  <li>Processing: Calculation based on the formula.</li> 
  <li>Output: BMI value and corresponding category (e.g., Normal, Overweight).</li> 
 </ul> 
</div>
<div class="line-post"> 
 <h3 class="sub1">1.2 Why Use OOP for This?</h3> 
</div>
<div class="line-post">
  OOP allows us to encapsulate related data and behavior into classes. This not only makes the code more organized but also facilitates scalability. For example, adding support for additional units or categories becomes significantly easier with an OOP approach. 
</div>
<div class="line-post"> 
 <h2 class="title-post">2. Designing the BMI Calculator</h2> 
</div>
<div class="line-post">
  Before jumping into code, we’ll design the application by identifying the classes and their responsibilities. 
</div>
<div class="line-post"> 
 <h3 class="sub1">2.1 Identifying Classes</h3> 
</div>
<div class="line-post"> 
 <ul> 
  <li><strong>Person</strong>: Represents the individual whose BMI is being calculated.</li> 
  <li><strong>BMICalculator</strong>: Handles the computation of BMI.</li> 
  <li><strong>BMICategory</strong>: Maps BMI ranges to categories like Normal or Overweight.</li> 
 </ul> 
</div>
<div class="line-post"> 
 <h3 class="sub1">2.2 Relationships Between Classes</h3> 
</div>
<div class="line-post"> 
 <ul> 
  <li>The Person class will hold weight and height attributes.</li> 
  <li>The BMICalculator class will use the Person object to compute BMI.</li> 
  <li>The BMICategory class will interpret the BMI value and determine the category.</li> 
 </ul> 
</div>
<div class="line-post"> 
 <h2 class="title-post">3. Implementing the BMI Calculator</h2> 
</div>
<div class="line-post">
  Let’s bring the design to life with Java code. 
</div>
<div class="line-post"> 
 <h3 class="sub1">3.1 Defining the Person Class</h3> 
</div>
<pre class="code"><span class="keyword-css">public</span> <span class="keyword-css">class</span> Person {<br>    <span class="keyword-css">private</span> <span class="keyword-css">double</span> weight; // in kilograms<br>    <span class="keyword-css">private</span> <span class="keyword-css">double</span> height; // in meters<br><br>    <span class="keyword-css">public</span> Person(<span class="keyword-css">double</span> weight, <span class="keyword-css">double</span> height) {<br>        <span class="keyword-css">this</span>.weight = weight;<br>        <span class="keyword-css">this</span>.height = height;<br>    }<br><br>    <span class="keyword-css">public</span> <span class="keyword-css">double</span> getWeight() {<br>        <span class="keyword-css">return</span> weight;<br>    }<br><br>    <span class="keyword-css">public</span> <span class="keyword-css">double</span> getHeight() {<br>        <span class="keyword-css">return</span> height;<br>    }<br><br>    <span class="keyword-css">public</span> <span class="keyword-css">void</span> setWeight(<span class="keyword-css">double</span> weight) {<br>        <span class="keyword-css">this</span>.weight = weight;<br>    }<br><br>    <span class="keyword-css">public</span> <span class="keyword-css">void</span> setHeight(<span class="keyword-css">double</span> height) {<br>        <span class="keyword-css">this</span>.height = height;<br>    }<br>}</pre>
<div class="line-post"> 
 <ul> 
  <li><strong>Encapsulation</strong>: The Person class encapsulates weight and height, exposing them through getter and setter methods.</li> 
  <li><strong>Reusability</strong>: This class can be reused in other health-related applications.</li> 
 </ul> 
</div>
<div class="line-post"> 
 <h3 class="sub1">3.2 Implementing the BMICalculator Class</h3> 
</div>
<pre class="code"><span class="keyword-css">public</span> <span class="keyword-css">class</span> BMICalculator {<br><br>    <span class="keyword-css">public</span> <span class="keyword-css">double</span> calculateBMI(Person person) {<br>        <span class="keyword-css">double</span> weight = person.getWeight();<br>        <span class="keyword-css">double</span> height = person.getHeight();<br><br>        <span class="keyword-css">if</span> (height &lt;= 0) {<br>            <span class="keyword-css">throw</span> <span class="keyword-css">new</span> IllegalArgumentException("Height must be greater than 0.");<br>        }<br>        <span class="keyword-css">return</span> weight / (height * height);<br>    }<br>}</pre>
<div class="line-post"> 
 <ul> 
  <li><strong>Responsibility</strong>: This class focuses solely on BMI calculation.</li> 
  <li><strong>Validation</strong>: It checks for invalid height to prevent runtime errors.</li> 
 </ul> 
</div>
<div class="line-post"> 
 <h3 class="sub1">3.3 Creating the BMICategory Class</h3> 
</div>
<pre class="code"><span class="keyword-css">public</span> <span class="keyword-css">class</span> BMICategory {<br><br>    <span class="keyword-css">public</span> String determineCategory(<span class="keyword-css">double</span> bmi) {<br>        <span class="keyword-css">if</span> (bmi &lt; 18.5) {<br>            <span class="keyword-css">return</span> "Underweight";<br>        } <span class="keyword-css">else</span> <span class="keyword-css">if</span> (bmi &gt;= 18.5 &amp;&amp; bmi &lt; 24.9) {<br>            <span class="keyword-css">return</span> "Normal weight";<br>        } <span class="keyword-css">else</span> <span class="keyword-css">if</span> (bmi &gt;= 25 &amp;&amp; bmi &lt; 29.9) {<br>            <span class="keyword-css">return</span> "Overweight";<br>        } <span class="keyword-css">else</span> {<br>            <span class="keyword-css">return</span> "Obesity";<br>        }<br>    }<br>}</pre>
<div class="line-post"> <strong>Clarity</strong>: The BMICategory class makes the code modular by separating BMI range logic. 
</div>
<div class="line-post"> 
 <h3 class="sub1">3.4 Integrating All Components</h3> 
</div>
<pre class="code"><span class="keyword-css">import</span> java.util.Scanner;<br><br><span class="keyword-css">public</span> <span class="keyword-css">class</span> Main {<br>    <span class="keyword-css">public</span> <span class="keyword-css">static</span> <span class="keyword-css">void</span> main(String[] args) {<br>        Scanner scanner = <span class="keyword-css">new</span> Scanner(System.in);<br><br>        System.out.println("Enter your weight (kg): ");<br>        <span class="keyword-css">double</span> weight = scanner.nextDouble();<br><br>        System.out.println("Enter your height (m): ");<br>        <span class="keyword-css">double</span> height = scanner.nextDouble();<br><br>        Person person = <span class="keyword-css">new</span> Person(weight, height);<br>        BMICalculator calculator = <span class="keyword-css">new</span> BMICalculator();<br>        BMICategory category = <span class="keyword-css">new</span> BMICategory();<br><br>        <span class="keyword-css">double</span> bmi = calculator.calculateBMI(person);<br>        String bmiCategory = category.determineCategory(bmi);<br><br>        System.out.printf("Your BMI is: %.2f
", bmi);<br>        System.out.println("You are classified as: " + bmiCategory);<br>    }<br>}</pre>
<div class="line-post"> 
 <ul> 
  <li><strong>User Interaction</strong>: The Main class integrates the system, providing a user-friendly interface.</li> 
  <li><strong>Scalability</strong>: Additional features, such as unit conversion, can be added without modifying existing classes.</li> 
 </ul> 
</div>
<div class="line-post"> 
 <h2 class="title-post">4. Extending the Application</h2> 
</div>
<div class="line-post">
  What if the user wants to input weight in pounds or height in inches? We can extend the functionality using OOP principles. 
</div>
<div class="line-post"> 
 <h3 class="sub1">4.1 Adding Unit Conversion</h3> 
</div>
<pre class="code"><span class="keyword-css">public</span> <span class="keyword-css">class</span> UnitConverter {<br>    <span class="keyword-css">public</span> <span class="keyword-css">static</span> <span class="keyword-css">double</span> poundsToKilograms(<span class="keyword-css">double</span> pounds) {<br>        <span class="keyword-css">return</span> pounds * 0.453592;<br>    }<br><br>    <span class="keyword-css">public</span> <span class="keyword-css">static</span> <span class="keyword-css">double</span> inchesToMeters(<span class="keyword-css">double</span> inches) {<br>        <span class="keyword-css">return</span> inches * 0.0254;<br>    }<br>}</pre>
<div class="line-post">
  This utility class introduces methods for unit conversion, enhancing flexibility. 
</div>
<div class="line-post"> 
 <h2 class="title-post">5. Best Practices for Building Scalable Applications</h2> 
</div>
<div class="line-post"> <strong>Use Dependency Injection</strong> 
</div>
<div class="line-post">
  Instead of creating instances directly in the Main class, consider using a dependency injection framework like Spring to manage objects. 
</div>
<div class="line-post"> <strong>Add Validation</strong> 
</div>
<div class="line-post">
  Enhance user input validation to handle edge cases (e.g., negative weights or heights). 
</div>
<div class="line-post"> <strong>Testing</strong> 
</div>
<div class="line-post">
  Write unit tests for each class to ensure reliability. 
</div>
<div class="line-post"> 
 <h2 class="title-post">6. Conclusion</h2> 
</div>
<div class="line-post">
  Building a BMI calculator in Java using OOP is an excellent way to practice designing modular and maintainable applications. By dividing responsibilities among well-defined classes, we ensure clarity, scalability, and reusability. If you have any questions about this implementation or would like to see enhancements, feel free to comment below! 
</div><p><strong>Read more at </strong>: <a href='https://www.tuanh.net/blog/oop/methods-to-build-a-bmi-calculator-using-java-oop'>Methods to Build a BMI Calculator Using Java OOP</a></p>
