Thinking in Objects
Tags: java, programming, learning, beginners
Published: 2024-06-05 20:55:27

The procedural paradigm focuses on designing methods. The object-oriented
paradigm couples data and methods together into objects. Software design using the object-oriented paradigm focuses on objects and operations on objects. Classes provide more flexibility and modularity for building reusable software. This section improves the solution for a problem introduced in previous post using the object-oriented approach. From these improvements, you will gain insight into the differences between procedural and object-oriented programming and see the benefits of developing reusable code using objects and classes.

ComputeAndInterpretBMI.java, [here](https://dev.to/paulike/case-study-computing-body-mass-index-and-computing-taxes-4jck) presented a program for computing body mass index. The code cannot be reused in other programs, because the code is in the **main** method. To make it reusable, define a static method to compute body mass index as follows:

`public static double getBMI(double weight, double height)`

This method is useful for computing body mass index for a specified weight and height. However, it has limitations. Suppose you need to associate the weight and height with a person’s name and birth date. You could declare separate variables to store these values, but these values would not be tightly coupled. The ideal way to couple them is to create an object that contains them all. Since these values are tied to individual objects, they should be stored in instance data fields. You can define a class named **BMI** as shown in Figure below.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b3r1a060eyir6728dsoz.png)

Assume that the **BMI** class is available. The program below gives a test program that uses this class.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ghhnw6m81oc1c27zjes.png)

Line 6 creates the object **bmi1** for **Kim Yang** and line 9 creates the object **bmi2** for **Susan King**. You 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 below.

```
package demo;

public class BMI {
	private String name;
	private int age;
	private double weight; // in pounds
	private double height; // in inches
	public static final double KILOGRAMS_PER_POUND = 0.45359237;
	public static final double METERS_PER_INCH = 0.0254;
	
	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, 20, weight, height);
	}
	
	public double getBMI() {
		double bmi = weight * KILOGRAMS_PER_POUND / ((height * METERS_PER_INCH) * (height * METERS_PER_INCH));
		return Math.round(bmi * 100) / 100;
	}
	
	public String getStatus() {
		double bmi = getBMI();
		if(bmi < 18.5)
			return "Underweight";
		else if(bmi < 25)
			return "Normal";
		else if(bmi < 30)
			return "Overweight";
		else
			return "Obese";
	}
	
	public String getName() {
		return name;
	}
	
	public int getAge() {
		return age;
	}
	
	public double getWeight() {
		return weight;
	}
	
	public double getHeight() {
		return height;
	}
}

```

The mathematical formula for computing the BMI using weight and height is given in linked post above. The instance method **getBMI()** returns the BMI. Since the weight and height are instance data fields in the object, the **getBMI()** method can use these properties to compute the BMI for the object.

The instance method **getStatus()** returns a string that interprets the BMI. The interpretation is also given in linked post.

This example demonstrates the advantages of the object-oriented paradigm over the procedural paradigm. The procedural paradigm focuses on designing methods. The object-oriented paradigm couples data and methods together into objects. Software design using the object-oriented paradigm focuses on objects and operations on objects. The object-oriented approach combines the power of the procedural paradigm with an added dimension that integrates data with operations into objects.

In procedural programming, data and operations on the data are separate, and this methodology requires passing data to methods. Object-oriented programming places data and the operations that pertain to them in an object. This approach solves many of the problems inherent in procedural programming. The object-oriented programming approach organizes programs in a way that mirrors the real world, in which all objects are associated with both attributes and activities. Using objects improves software reusability and makes programs easier to develop and easier to maintain. Programming in Java involves thinking in terms of objects; a Java program can be viewed as a collection of cooperating objects.