Project Code Area Calculator

 Project Code

Area Calculator

/**
 *  Area Calculator
 */
import java.util.*;
class Area
{
    Scanner sc = new Scanner(System.in);
    double areaOfShape;

    /**
     * Defaule Constructor to initialize data members of class Area
     */
    public Area()
    {
        areaOfShape=0.0d;
    }

    /**
     * Menu method to display menu options to user and accept user
     * choice
     */
    private void menu(){
        System.out.println("\n\t\t\t _____________________________________");
        System.out.println("\n\t\t\t Press 1 for Area of Rectangle");
        System.out.println("\n\t\t\t Press 2 for Area of Circle");
        System.out.println("\n\t\t\t Press 3 for Area of Square");
        System.out.println("\n\t\t\t Press 4 for Area of Triangle");
        System.out.println("\n\t\t\t Press 5 for Area of Parallelogram");
        System.out.println("\n\t\t\t Press 6 for Area of Trapezoid");
        System.out.println("\n\t\t\t Press 7 for Area of Elipse");
        System.out.println("\n\t\t\t Press 8 for Surface Area of Cube");
        System.out.println("\n\t\t\t Press 9 for Surface Area of Cone");
        System.out.println("\n\t\t\t Press 10 for of Exit");
        System.out.println("\n\t\t\t _____________________________________");
        System.out.print("\n\t\t\t Please Enter Your Choice: ");
        try{
            sc=new Scanner(System.in);
            int choice = sc.nextInt();

            if(choice<1 || choice>10){
                System.out.println("\t\t\t  Please Enter Numbers between 1 to 4 Only..");
                menu();
            }
            operateMenu(choice);
        }
        catch(InputMismatchException e){
            System.out.println("\t\t\t  Please Enter Numbers Only..");
            sc.close();
            menu();

        }
    }

    /**
     * OperatMenu method to implement menu options using switch case.
     */
    private void operateMenu(int choice){
        try{
            switch(choice){
                case 1:
                System.out.print("\n\t\t\t Enter Length of Rectangle : ");
                double length = sc.nextDouble();
                System.out.print("\n\t\t\t Enter Breadth of Reactangle : ");
                double breadth = sc.nextDouble();
                area(length, breadth);
                break;
                case 2:
                System.out.print("\n\t\t\t Enter radius of circle : ");
                double r = sc.nextDouble();
                area(r);
                break;
                case 3:
                System.out.print("\n\t\t\t Enter side of Square  : ");
                double s = sc.nextDouble();
                areaOfSquare(s);
                break;
                case 4:
                System.out.print("\n\t\t\t Enter base of Triangle  : ");
                double base = sc.nextDouble();
                System.out.print("\n\t\t\t Enter height of Triangle  : ");
                double height = sc.nextDouble(); 
                areaOfTriangle(base, height);
                break;
                case 5:
                System.out.print("\n\t\t\t Enter base of Parallelogram  : ");
                double pbase = sc.nextDouble();
                System.out.print("\n\t\t\t Enter vertical height of Parallelogram  : ");
                double pheight = sc.nextDouble(); 
                areaOfParallelogram(pbase, pheight);
                break;
                case 6:
                System.out.print("\n\t\t\t Enter length of 1st parallel sides of Trapezoid  : ");
                double ta = sc.nextDouble();
                System.out.print("\n\t\t\t Enter length of 2nd parallel sides of Trapezoid  : ");
                double tb = sc.nextDouble();
                System.out.print("\n\t\t\t Enter height of the Trapezoid  : ");
                double th = sc.nextDouble();
                areaOfTrapezoid(ta, tb, th);
                break;
                case 7:
                System.out.print("\n\t\t\t Enter value of a axis of Ellipse  : ");
                double ea = sc.nextDouble();
                System.out.print("\n\t\t\t Enter value of b axis of Ellipse  : ");
                double eb = sc.nextDouble();
                areaOfElipse(ea,eb);
                break;
                case 8:
                System.out.print("\n\t\t\t Enter value of edge of Cube : ");
                double ce = sc.nextDouble();
                surfaceAreaOfcube(ce);
                break;
                case 9:
                System.out.print("\n\t\t\t Enter value of radius of Cone  : ");
                double cr = sc.nextDouble();
                System.out.print("\n\t\t\t Enter value of height of Cone  : ");
                double ch = sc.nextDouble();
                surfaceareaOfCone(cr,ch);
                break;
                case 10: System.out.println("\n\n\t\t\t\t .......Thank You......");
                System.exit(0);
            }
        }
        catch(InputMismatchException e){
            System.out.println("\t\t\t  Please Enter Numbers Only..");
            operateMenu(choice);
        }

    }

    /**
     *  overloaded area method to calculate area of rectangle
     */
    private void area(double l, double b){
        areaOfShape = l*b;
        System.out.println("\n\t\t\t Area of Reactangle :" +areaOfShape );
        checkCotinue();
    }

    /**
     * overloaded area method to calculate area of circle
     */
    private void area(double r){
        areaOfShape = 3.14*r*r;
        System.out.println("\n\t\t\t Area of Circle :" +areaOfShape );
        checkCotinue();

    }

    /**
     * areaOfSquare(double s) method to calculate area of Square
     */
    public void areaOfSquare(double s){
        areaOfShape = s * s;
        System.out.println("\n\t\t\t Area of Square :" +areaOfShape );
        checkCotinue();
    }

    /**
     * areaOfTriangle(double base, double height) method to calculate area of Triangle
     */
    public void areaOfTriangle(double base, double height){
        areaOfShape = 0.5 * base * height;
        System.out.println("\n\t\t\t Area of Triangle :" +areaOfShape );
        checkCotinue();
    }

    /**
     *  areaOfParallelogram(double pbase, double pheight) method to calculate area of Parallelogram
     */
    public void  areaOfParallelogram(double pbase, double pheight){
        areaOfShape = pbase * pheight;
        System.out.println("\n\t\t\t Area of Parallelogram :" +areaOfShape );
        checkCotinue(); 
    }

    /**
     * areaOfTrapezoid(double ta, double tb, double th) method to calculate area of Trapezoid
     */
    public void areaOfTrapezoid(double ta, double tb, double th){
        areaOfShape = ((ta+tb)/2)*th;
        System.out.println("\n\t\t\t Area of  Trapezoid :" +areaOfShape );
        checkCotinue(); 
    }

    /**
     * areaOfElipse(double ea, double eb) method to calculate are of Elipse 
     */
    public void areaOfElipse(double ea, double eb){
        areaOfShape = Math.PI * ea * eb;
        System.out.println("\n\t\t\t Area of  Elipse :" +areaOfShape );
        checkCotinue(); 
    }

    /**
     * surfaceAreaOfcube(double ce) method to calculate surface area of Cube
     */
    public void surfaceAreaOfcube(double ce){
        areaOfShape = (Math.pow(ce,2)) * 6;
        System.out.println("\n\t\t\t Area of  Elipse :" +areaOfShape );
        checkCotinue();
    }

    /**
     * surfaceareaOfCone(double cr,double ch) method to calculate surface area fo Cone
     */
    public void surfaceareaOfCone(double cr,double ch){
        areaOfShape = (Math.PI*cr)*((cr + Math.sqrt(ch*ch+ cr*cr)));
        System.out.println("\n\t\t\t Area of  Elipse :" +areaOfShape );
        checkCotinue();
    }

    public void checkCotinue(){
        System.out.println("\n\t\t\t Press 1 To Continue \n\t\t\t Press 0 to exit ");
        System.out.print("\n\t\t\t Please Enter Your Choice: ");
        try{
            int subOption = sc.nextInt();
            if(subOption == 1)
                menu();
            else
                System.out.println("\n\t\t\t.....Thank You....");
            System.exit(0); 
        }
        catch(InputMismatchException e){
            System.out.println("\t\t\t  Please Enter Numbers Only..");
            checkCotinue();
        }
    }

    public static void main(String args[]){
        Area ob = new Area();
        System.out.println("\t\t\t ====================================");
        System.out.println("\t\t\t\t     Area Calculator ");
        System.out.println("\t\t\t ====================================");
        ob.menu();
    }
}




No comments: