Java program to calculate the area of different geometric shapes

Written by Super User. Posted in Basic

This is java program to illustrate the switch case and functions and the buffer reader. It will calculate the areas of different geometric shapes by taking the required input from the user

  • Area of a Triangle is 1/2 * height * widht
  • Square is side * side
  • Circle is Pi * radius * radius
  • Rectangle is length * width
  • Cylinder is 2 * (( Pi * radius*radius) + (Pi * radius * height))
  • Cube is 6 * side * side
  • Cuboid is 1/2 * ( (length * width) + (width * height) + (height * length))
  • Sphere is 4 * pi * radius * radius
import java.io.*;
public class area{
    public static void main(String[] args){
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        try {
            printmenu();
            int choice = Integer.parseInt(br.readLine());
            do{
                switch(choice){
                    case 1:
                    area_triangle();
                    break;
                    case 2:
                    area_square();
                    break;
                    case 3:
                    area_circle();
                    break;
                    case 4:
                    area_rectangle();
                    break;
                    case 5:
                    area_cylinder();
                    break;
                    case 6:
                    area_cube();
                    break;
                    case 7:
                    area_cuboid();
                    break;
                    case 8:
                    area_sphere();
                    break;
                    default:
                    System.out.println("You must enter a valid choice ");
                    break;
                }
                printmenu();
                choice = Integer.parseInt(br.readLine());
            }while(choice != -1);
        }
        catch (IOException ioe) {
            System.out.println("IO error trying to read that what you have entered !");
            System.exit(1);
        }
        
    }
    public static void printmenu(){
        System.out.println("\n 1. Triangle \n 2. Square \n 3. Circle \n 4. Rectangle \n 5. Cylinder \n 6. Cube \n 7. Cuboid \n 8. Sphere ");
        System.out.print("\n Enter you choice (Enter -1 to exit) :");
    }
    public static void area_triangle(){
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        try{
            System.out.print(" Enter the height : ");
            float height = Float.parseFloat(br.readLine());
            System.out.print(" Enter the width : ");
            float width = Float.parseFloat(br.readLine());
            System.out.println("The area of the Triangle is : " + 0.5 * height * width);
        }
        catch(IOException ioe){
            System.out.println("IO error trying to read that what you have entered !");
            System.exit(1);
        }
    }
    public static void area_square(){
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        try{
            System.out.print("Enter the side of the Square : ");
            float side = Float.parseFloat(br.readLine());
            System.out.println("The area of the Square is :" + side * side);
        }
        catch(IOException ex){
            System.out.println("IO error trying to read that what you have entered !");
            System.exit(1);
        }
    }
    
    public static void area_circle(){
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        try{
            System.out.print("Enter the Radius of the Circle : ");
            float radius = Float.parseFloat(br.readLine());
            System.out.println("The area of the Circle is :" + Math.PI * radius * radius);
        }
        catch(IOException ex){
            System.out.println("IO error trying to read that what you have entered !");
            System.exit(1);
        }
    }
    
    public static void area_rectangle(){
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        try{
            System.out.print("Enter the Height of the Rectangle : ");
            float height = Float.parseFloat(br.readLine());
            System.out.print("Enter the Width of the Rectangle : ");
            float width = Float.parseFloat(br.readLine());
            System.out.println("The area of the Rectangle is :" + height*width);
        }
        catch(IOException ex){
            System.out.println("IO error trying to read that what you have entered !");
            System.exit(1);
        }
    }
    
    public static void area_cylinder(){
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        try{
            System.out.print("Enter the Height of the cylinder : ");
            float height = Float.parseFloat(br.readLine());
            System.out.print("Enter the radius of the cylinder : ");
            float radius = Float.parseFloat(br.readLine());
            float tsa = 2 * (((float) Math.PI * radius*radius) + ((float)Math.PI * radius * height));
            System.out.println("The Total Surface area of the cylinder is :" +  tsa);
        }
        catch(IOException ex){
            System.out.println("IO error trying to read that what you have entered !");
            System.exit(1);
        }
    }
    
    public static void area_cube(){
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        try{
            System.out.print("Enter the side of the Cube : ");
            float side = Float.parseFloat(br.readLine());
            System.out.println("The Total Surface area of the Cube is :" +  6 * side * side);
        }
        catch(IOException ex){
            System.out.println("IO error trying to read that what you have entered !");
            System.exit(1);
        }
    }
    
    public static void area_cuboid(){
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        try{
            System.out.print("Enter the Length of the Cuboid : ");
            float length = Float.parseFloat(br.readLine());
            System.out.print("Enter the Width of the Cuboid : ");
            float width = Float.parseFloat(br.readLine());
            System.out.print("Enter the Height of the Cuboid : ");
            float height = Float.parseFloat(br.readLine());
            System.out.println("The Total Surface area of the Cuboid is :" +  0.5 * ( (length * width) + (width * height) + (height * length)) );
        }
        catch(IOException ex){
            System.out.println("IO error trying to read that what you have entered !");
            System.exit(1);
        }
    }
    
    public static void area_sphere(){
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        try{
            System.out.print("Enter the radius of the Sphere : ");
            float radius = Float.parseFloat(br.readLine());
            System.out.println("The Total Surface area of the Cube is :" + 4* Math.PI * radius * radius);
        }
        catch(IOException ex){
            System.out.println("IO error trying to read that what you have entered !");
            System.exit(1);
        }
    }
} 

Out put

Output of the area program

меня зовут эрл