In: Computer Science
Hello There,
Please find the answer mentioned Below
/*
* Construction of Abstract class Shape2D
* */
public abstract class Shape2D {
abstract double get2DArea();
}
/*
* Construction of class Rectangle2D with length and breadth as field
* */
public class Rectangle2D extends Shape2D{
private double length;
private double breadth;
public Rectangle2D(double length, double breadth) {
super();
this.length = length;
this.breadth = breadth;
}
@Override
double get2DArea() {
return length*breadth;
}
}
/*
* Construction of class Circle with radius as field
* */
public class Circle2D extends Shape2D {
double radius;
public Circle2D(double radius) {
super();
this.radius = radius;
}
@Override
double get2DArea() {
// TODO Auto-generated method stub
return 3.14*(radius*radius);
}
}
import java.text.DecimalFormat;
import java.util.Scanner;
/*
* Construction of final class Shape2D which will take the input from user ,
* format the area to one pointer than print the area
* */
public class Shape2DDriver {
public static void main(String[] args) {
Scanner scan_obj=new Scanner(System.in);
System.out.println("Enter the length in double");
double length=scan_obj.nextDouble();
System.out.println("Enter the breadth in double");
double breadth=scan_obj.nextDouble();
System.out.println("Enter the radius in double");
double radius=scan_obj.nextDouble();
Shape2D rectangle_obj= new Rectangle2D(length,breadth);
Shape2D circle_obj=new Circle2D(radius);
double rectangle_area=print(rectangle_obj.get2DArea());
System.out.println("area of rectangle\n"+rectangle_area);
double circle_area=print(circle_obj.get2DArea());
System.out.println("area of circle\n"+circle_area);
}
private static double print(double get2dArea) {
String formatted_value = new DecimalFormat("#.#").format(get2dArea);
double area=Double.parseDouble(formatted_value);
return area;
}
}
Output:
Thank You!!