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



The given task is to categorize Taller, Dwarf, and Average by the Height of a Person. First of all, we need to define tall, dwarf, and average height of a person. Let us assume -
  • A person with a height between 170cm to 195cm is considered taller.
  • If his height is between 150cm and 195cm, he is considered taller.
  • A person whose height is below 150cm is considered a dwarf.
  • If his height is greater than 195cm, he is considered abnormal.

Categorizing the Height of a Person in Java

To categorize the Height of a Person, we need to compare his height with each range specified above. In Java, we can do this in various ways. Let's go through some of these one by one -

  • Using if-else-if condition
  • Using a user-defined function
  • Using the Ternary operator
  • Using Switch Case

Using if-else-if condition

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

For the given problem, we need to check in which category the specified height belongs, and there are multiple conditions, so the if-else-if statements will be suitable here.

Example

In the Java code given below, we have taken a person's height. Then, using an if-else-if condition, we categorized the height.

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");
      }
   }
}

Following is the output of the above program - 

Taller Height Person

User-Defined Method

We can also perform the same task with a user-defined method. In Java, methods are blocks of code that can be reused multiple times to perform a single operation.

If we want to categorize the height of multiple persons at the same time, then this approach is best suited, as we can call the custom method several times with different arguments as per our requirement.

Example

In the following Java program, we have created a method that categorizes the height. In the main method, we called this method multiple times with different arguments.

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);
   }
}

Following is the output of the above program -

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

Using Ternary Operator

You can replace the if-else-if conditions with a ternary operator to categorize a person on the basis of height.

Example

Following is the program -

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

Following is the output of the above program -

Person considered as Dwarf
Updated on: 2025-05-30T16:22:30+05:30

486 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements