In: Computer Science
Writing a Java Code
Requirements of the JAVA program:
Your task is to calculate geometric area for 3 shapes(square, rectangle and circle).
Expected output:
***Area Calculator***
S -- Square
R -- Rectangle
C -- Circle
S
Please enter length of the square:
2
Area of your square is: 4
***Area Calculator***
S -- Square
R -- Rectangle
C -- Circle
R
Please enter length of the rectangle:
2
Please enter length of the rectangle:
3
Area of your rectangle is: 6
***Area Calculator***
S -- Square
R -- Rectangle
C -- Circle
C
Please enter the redius of the circle:
3
Area of your circle is: 28.26
package add;
import static java.lang.System.exit;
import java.util.*;
public class Geometry {
    static float pi=(float) 3.14;
     float squareArea(float x)
    {
        return x*x;
    }
    float rectangleArea(float x, float y)
    {
        return x*y;
    }
    float circleArea(float r)
    {
        return (float) (pi*r*r);
    }
    public static void main(String args[]){
        Geometry obj = new Geometry();
       
        Scanner s=new Scanner(System.in);
        char sc;
        float n,n1,r,a;
        while(true)
        {
            System.out.println("\n***Area Calculator***");
            System.out.println("S  --  Square");
            System.out.println("R  --  Rectangle");
            System.out.println("C  --  Circle");
            sc=s.next().charAt(0);
            switch(sc)
            {
                case 'S':
                    System.out.println("Please enter length of the square:");
                     n=s.nextFloat();
                    a=obj.squareArea(n);
                    System.out.print("Area of your square is:"+a);
                    break;
                case 'R':
                    System.out.println("Please enter length of the rectangle:");
                    n=s.nextFloat();
                    System.out.println("Please enter breadth of the rectangle:");
                    n1=s.nextFloat();
                    a=obj.rectangleArea(n,n1);
                    System.out.print("Area of your rectangle is:"+a);
                    break;
                case 'C':
                    System.out.println("Please enter the redius of the circle:");
                    r=s.nextFloat();
                    a=obj.circleArea(r);
                    System.out.print("Area of your circle is:"+a);
                    break;
                default:System.out.println("Wrong choice");
                    exit(1);
            }
        }
    }
    
}
OUTPUT:-
***Area Calculator***
S -- Square
R -- Rectangle
C -- Circle
S
Please enter length of the square:
2
Area of your square is:4.0
***Area Calculator***
S -- Square
R -- Rectangle
C -- Circle
R
Please enter length of the rectangle:
2
Please enter breadth of the rectangle:
3
Area of your rectangle is:6.0
***Area Calculator***
S -- Square
R -- Rectangle
C -- Circle
C
Please enter the redius of the circle:
3
Area of your circle is:28.26
***Area Calculator***
S -- Square
R -- Rectangle
C -- Circle
k
Wrong choice
Java Result: 1



