MAIN.JAVA:
import java.util.*; import java.text.*; public class Main { public static void main(String[] args){ Scanner sc=new Scanner(System.in); DecimalFormat df=new DecimalFormat("0.00"); System.out.println("Enter the shape\n1.Rectangle\n2.Triangle"); int input=sc.nextInt(); double value1,value2; switch (input) { case 1: System.out.println("Enter the length and breadth:"); Shape.Rectangle rectangle=new Shape.Rectangle(); rectangle.setValue1(sc.nextDouble()); rectangle.setValue2(sc.nextDouble()); System.out.println("Area of rectangle is "+df.format(rectangle.computeRectangleArea())); break; case 2: System.out.println("Enter the base and height:"); Shape.Triangle triangle=new Shape.Triangle(); triangle.setValue1(sc.nextDouble()); triangle.setValue2(sc.nextDouble()); System.out.println("Area of triangle is "+df.format(triangle.computeTriangleArea())); break; default: System.out.println("Invalid choice"); } } }
SHAPE.JAVA:
public class Shape { public static double value1; public static double value2; //fill your code here public static class Rectangle{ public void setValue1(double nextDouble){ value1=nextDouble; } public static double getValue1(){ return value1; } public void setValue2(double nextDouble){ value2=nextDouble; } public static double getValue2(){ return value2; } public double computeRectangleArea(){ double ar = getValue1()*getValue2(); return ar; } } public static class Triangle{ public void setValue1(double nextDouble){ value1=nextDouble; } public static double getValue1(){ return value1; } public void setValue2(double nextDouble){ value2=nextDouble; } public static double getValue2(){ return value2; } public double computeTriangleArea(){ double ar = 0.5*(getValue1()*getValue2()); return ar; } } }