Question 8.1: A. Abstract Class
• Create an abstract class 'Shape' with one attributed named ‘area’ of double type.
• Write proper setter and getter for the attributes
• Three abstract methods namely 'RectangleArea' taking two parameters, 'SquareArea' and 'CircleArea'
taking one parameter each.
• The parameters of 'RectangleArea' are its length and breadth, that of 'SquareArea' is its side and that
of 'CircleArea' is its radius.
• Now create another class 'Area' containing all the three methods 'RectangleArea', 'SquareArea' and
'CircleArea' for calculating the area of rectangle, square and circle respectively.
• Create an ArrayList of Shape type. Your main () method must display the following first:
Press (1) for calculating Rectangle Area
Press (2) for calculating Square Area
Press (3) for calculating Circle Area
• You must create at least 3 shape type reference variable and assign area type object to them in this
manner
• Call the respective method for all three objects and display the area.
Soluton:
//Area Class
//CircleArea Class
package com.company;
public class CircleArea extends Shape{
double redias;
double pi = 3.1416;
public CircleArea(){
}
public CircleArea(double redias){
this.redias = redias;
}
@Override
public double area() {
return 2*pi*redias;
}
}
//Main Class
package com.company;
import java.util.Scanner;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
Scanner obj = new Scanner(System.in);
ArrayList <Shape> shapes = new ArrayList<>();
while(true){
System.out.println();
System.out.print("width: ");
double width = obj.nextDouble();
System.out.print("Length: ");
double Length = obj.nextDouble();
System.out.print("Radias: ");
double radias = obj.nextDouble();
System.out.println();
System.out.println();
System.out.println("press (1) for calculating Rectangle Area");
System.out.println("press (2) for calculating Square Area");
System.out.println("press (3) for calculating Circle Area");
System.out.print("Enter your number: ");
int n = obj.nextInt();
System.out.println();
if(n==1){
RectangleArea rec = new RectangleArea(Length,width);
System.out.println("Rectangle Area is: " + rec.area());
}
else if(n == 2){
SquareArea squar = new SquareArea(radias);
System.out.println("Square Area is: "+ squar.area());
}
else if(n == 3){
CircleArea circl = new CircleArea(radias);
System.out.println("Ciarcal Aea is: " + circl.area());
}
}
}
}
//RectangleArea Class
package com.company;
public class RectangleArea extends Shape{
double width,length;
public RectangleArea(){
}
public RectangleArea(double width , double length){
this.width = width;
this.length = length;
}
public double area(){
return width * length;
}
}
//Shape Class
package com.company;
public abstract class Shape {
public abstract double area();
}
//SquareArea Class
package com.company;
public class SquareArea extends Shape {
double redias;
public SquareArea(){
}
public SquareArea(double redias){
this.redias = redias;
}
public double area(){
return redias * redias;
}
}