🌙 DARK

Abstract Class - List of Shapes Cricle, Rectangle, Square

 

Abstract Class 


Abstract classes are declared with a keyword abstract. These classes cannot be instantiated. They can have attributes/methods. They can have normal methods as well as abstract methods. These abstract methods must be implemented in subclasses or else they should also be declared as abstract. 

Write a program to calculate the perimeter of Circle, Rectangle, and square which implements the abstract class attributes/methods.

Strictly adhere to the Object-Oriented specifications given in the problem statement. All class names, attribute names and method names should be the same as specified in the problem statement.


Create an abstract class Shape with abstract method public abstract Double calculatePerimeter()

Consider a class Circle that extends Shape with the following private attributes,

AttributeDatatype
radiusFloat

Include appropriate getters and setters, constructors for the class.

Create a parameterized constructor with an argument in order public Circle(Float radius).

Consider a class Rectangle that extends Shape with the following private attributes,

AttributeDatatype
lengthFloat
breadthFloat

Include appropriate getters and setters, constructors for the class.
Prototype for the Parameterized Constructor, public Rectangle(Float length, Float breadth).

Consider  a class Square that extends Shape with the following private attributes,

AttributeDatatype
sideFloat

Include appropriate getters and setters, constructors for the class.

Prototype for the Parameterized Constructor, public Square (Float side).


Implement the method calculatePerimeter() in all the child classes to calculate appropriate perimeters.

Note: Use 3.14 for pi value

The link to download the template code is given below
Code Template


Input and Output format:


All the double values should be displayed up to 2 decimal places.
Refer to sample Input and Output for formatting specifications.

[All Texts in bold corresponds to the input and rest are output]

Sample Input and Output 1:

List of Shapes:
1.Circle
2.Rectangle
3.Square
Enter your choice:
1
Enter the radius of the Circle:
2.34
The perimeter is 14.70


Sample Input and Output 2:

List of Shapes:
1.Circle
2.Rectangle
3.Square
Enter your choice:
2
Enter the length of the Rectangle:
12
Enter the breadth of the Rectangle:
3
The perimeter is 30.00


Sample Input and Output 3:

List of Shapes:
1.Circle
2.Rectangle
3.Square
Enter your choice:
3
Enter the side of the Square:
13
The perimeter is 52.00


@MRProgrammer89 


CREATE CLASS < Shape :


public abstract class Shape {


    public abstract double calculatePerimeter();

}


@MRProgrammer89 


CREATE CLASS < Square:

public class Square extends Shape{


    private float side;

    public Square(float side)

    {

        this.side = side;

    }

    public  double calculatePerimeter(){

        return 4.00*side;

    }


}


@MRProgrammer89 


CREATE CLASS < Rectangle:


public class Rectangle extends Shape{

    private float length,breadth;

    public Rectangle(float length,float breadth){

        this.length = length;

        this.breadth = breadth;

    }

    public double calculatePerimeter(){   

     return 2.00*(length+breadth);

    }

}

@MRProgrammer89 


CREATE CLASS < Circle :


public class Circle extends Shape{


    private float radius;

    

    public Circle(float radius){

        this.radius = radius;

    }

    public  double calculatePerimeter(){

        return 2.00*3.14*radius;

    }


}


@MRProgrammer89 


CREATE CLASS < Main :


import java.util.Scanner; 


public class Main {

    public static void main(String args[]) throws Exception {

             Scanner scanner = new Scanner(System.in);


        System.out.println("List of Shapes:");

        System.out.println("1.Circle");

        System.out.println("2.Rectangle");

        System.out.println("3.Square");

        System.out.println("Enter your choice:");

        int shapeType = Integer.parseInt(scanner.nextLine());


        switch (shapeType) {

            case 1:

                System.out.println("Enter the radius of the Circle:");


                float radius = Float.parseFloat(scanner.nextLine());


                Shape circle = new Circle(radius);


                System.out.printf("The perimeter is %.2f%n",circle.calculatePerimeter());

                break;

            case 2:


                System.out.println("Enter the length of the Rectangle:");

                float length = Float.parseFloat(scanner.nextLine());

                System.out.println("Enter the breadth of the Rectangle:");

                float breadth = Float.parseFloat(scanner.nextLine());

                Shape rectangle = new Rectangle(length,breadth);

                System.out.printf("The perimeter is %.2f%n",rectangle.calculatePerimeter());

                break;

            case 3:

                System.out.println("Enter the side of the Square:");

                float side = Float.parseFloat(scanner.nextLine());

                Shape square = new Square(side);

                System.out.printf("The perimeter is %.2f%n",square.calculatePerimeter());

                break;

            default:

                break;

        }

        

}

}



@MRProgrammer89 






MOTIVATION :


    “The way we spend our time defines who we are 


Although we may not see each other as often as we’d like, distance is no match for the bond that we share. Thank you for coming to visit. It was fantastic to catch up.


VISITE MORE BLOGS




THANK YOU....😊

Post a Comment

Thanks for reading the blog. We hope it was useful to you and that you learned something new. Will always be writing on new and interesting topics, so you can visit our website to know the latest updates of our blogs. Thank You!

Previous Post Next Post

Contact Form