Java Program to Categorize Taller, Dwarf and Average by Height of a Person


First, we need to define taller, dwarf and average height of person. A person with height between 170cm to 195cm is considered as taller, height in the range of 150cm to 170cm is considered as average, person whose height is below 150cm is considered as dwarf and person whose height is greater than 195cm is abnormal.

Now let’s jump into the java program to categorize taller, dwarf and average by height of a person using if else if condition.

Approach 1: Using if else if condition

Generally, we use if else if statements when we have to check multiple conditions. It follows top down approach.

Syntax

if(condition 1){
   //code will be executed only when condition 1 is true
}else if(condition 2){
   //code will be executed only when condition 2 is true
}else{
   //code will be executed when all of the above condition is false
}

The compiler will start checking from the first if condition, if it is true then it will execute the code inside that if block and program execution will stop here, if it is false only then execution of other else if block will start. Similarly, compiler will check conditions of other blocks also, if condition will match then code inside that block will get executed otherwise it will execute the last else block code.

Example

public class Main{
   public static void main(String []args){
      double ht=176;
      if(ht>150 && ht<170 ){
         System.out.println("Average Height Person");
      }else if(ht>170 && ht<195){
         System.out.println("Taller Height Person");
      }else if(ht<150){
         System.out.println("Person considered as Dwarf");
      }else{
         System.out.println("Abnormal Height Person");
      }
   }
}

Output

Taller Height Person

In the above code, we have initialized double variable as ‘ht’ with a value 176. We have taken double because a person’s height could be in decimal. Then, using if-else-if condition we categorized the height.

Approach 2: User Defined Method

We can also do the same task with user defined method. Methods are block of code that can be reused multiple times to perform a single operation. It saves our time and also reduces the size of code. If we want to categorize height of multiple persons at same time then this approach is best suited for this condition because we can call this method several times with different arguments as per our needs.

Syntax

accessSpecifier nonAccessModifier return_Type method_Name(Parameters){
   // Body of the method
}

accessSpecifier − It is used to set the accessibility of the method. It may be public, protected, default and private.

nonAccessModifier − It shows additional functionalities or behaviour of the method such as static and final.

return_Type − The datatype a method is going to return. We use void keyword when method does not return anything.

method_Name − Name of the method.

parameters − It contains name of the variable followed by datatype.

We can call a method in two ways: by using method name with arguments inclosed in brackets or we can directly passed the method to ‘System.out.println’.

Method_Name(arguments);
Or
System.out.println(method_Name(arguments));

arguments − They are the values that are passed during method call. Arguments must match the type of parameter passed in the method signature.

Example

public class Main{
   public static void height(double ht){
      if(ht>150 && ht<170 ){
         System.out.println(ht + ": Average Height Person");
      }else if(ht>170 && ht<195){
         System.out.println(ht + ": Taller Height Person");
      }else if(ht<150){
         System.out.println(ht + ": Person considered as Dwarf");
      }else{
         System.out.println(ht + ": Abnormal Height Person");
      }
   }
   public static void main(String []args){
      height(156);
      height(177);
      height(196);
      height(146);
   }
}

Output

156.0: Average Height Person
177.0: Taller Height Person
196.0: Abnormal Height Person
146.0: Person considered as Dwarf

We have created a method named ‘height’ along with a parameter ‘ht’ of type doiuble. In main method, we called the height method multiple times with different arguments.

Conclusion

We have seen two approaches to categorize height of a person as taller, dwarf and average. The first one is very basic and easily understandable. The second one is better approach if we have multiple values available to us. We have also understood the basics of user defined function.

Updated on: 25-Apr-2023

190 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements