0% found this document useful (0 votes)
2 views5 pages

Problem Statement (1)

The document outlines the creation of an abstract class 'Shape' with instance variables for side, area, and perimeter, along with abstract methods for calculating area and perimeter. It includes subclasses such as 'Square', 'Rectangle', 'Circle', and 'Triangle' that implement the abstract methods and a concrete display method. The main class 'Test' demonstrates the functionality by creating instances of these shapes, calculating their area and perimeter, and displaying the results.

Uploaded by

Sam Gameer
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Problem Statement (1)

The document outlines the creation of an abstract class 'Shape' with instance variables for side, area, and perimeter, along with abstract methods for calculating area and perimeter. It includes subclasses such as 'Square', 'Rectangle', 'Circle', and 'Triangle' that implement the abstract methods and a concrete display method. The main class 'Test' demonstrates the functionality by creating instances of these shapes, calculating their area and perimeter, and displaying the results.

Uploaded by

Sam Gameer
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Problem Statement:

Create abstract class Shape which has instance variables side, area and perimeter And methods
calculateArea(), calculatePerimeter() as abstract methods and display() as concrete method.
Write subclasses which extend Shape class like Triangle, Rectangle, Circle, Cube and Squere and
override abstract methods and display methods in subclass take instance variable if needed as
per the formula. And use parameterized constructor to initialize instance variables using “this”
reference variable Code:

abstract class Shape{

//Instance variables double

side,area,perimeter; String shape;

//Constructor to initialize value

Shape(double value){

[Link] = value;

//Abstract methods abstract void

calculateArea(); abstract void

calculatePerimeter();

//Concrete methods

void display(){

[Link]("Shape:"+[Link]);

[Link]("Side:"+[Link]);

[Link]("Area:"+[Link]);

[Link]("Perimeter:"+[Link]);

[Link]();

}
}
class Square extends Shape{

Square(double side){

super(side);

[Link] = "Square";

//Overriding abstract methods

void calculateArea(){

[Link] = side*side;

void calculatePerimeter(){

[Link] = 4*side;

class Rectangle extends Shape{ private

double width; Rectangle(double

length,double width){

super(length);

[Link] = width;

[Link] = "Rectangle";

//Overriding abstract methods

void calculateArea(){

[Link] = side*width;
}

void calculatePerimeter(){

[Link] = 2*(side+width);

class Circle extends Shape{

private double width;

Circle(double radius){

super(radius);

[Link] = "Circle";

//Overriding abstract methods

void calculateArea(){ [Link] =

3.14*(side*side);

void calculatePerimeter(){

[Link] = 2*3.14*side;

class Triangle extends Shape{ private double

height; Triangle(double base,double

height){
super(base);

[Link] = height;

[Link] = "Triangle";

//Overriding abstract methods

void calculateArea(){ [Link] =

0.5*side*height;

void calculatePerimeter(){

[Link] = side+height;

public class Test{ public static void

main(String[] args){ Square square = new

Square(4.0);

[Link]();

[Link]();

[Link]();

Rectangle rectangle = new Rectangle(4.0,3.0);

[Link]();

[Link](); [Link]();

Triangle triangle = new Triangle(4.0,3.0);

triangle .calculateArea();
triangle .calculatePerimeter();

triangle .display();

Circle circle = new Circle(4.0);

circle .calculateArea();
circle .calculatePerimeter();

circle .display();

Output:

You might also like