Skip to main content

Write a java program to create an abstract class named Shape that contains two integers and an empty method named print Area (). Provide three classes named Rectangle, Triangle and Circle such that each one of the classes extends the class Shape. Each one of the classes contains only the method print Area () that prints the area of the given shape

 CODE:


abstract class Shape {

    protected int dimension1;

    protected int dimension2;


    public Shape(int dimension1, int dimension2) {

        this.dimension1 = dimension1;

        this.dimension2 = dimension2;

    }


    public abstract void printArea();

}


class Rectangle extends Shape {

    public Rectangle(int length, int width) {

        super(length, width);

    }


    @Override

    public void printArea() {

        System.out.println("Area of Rectangle: " + (dimension1 * dimension2));

    }

}


class Triangle extends Shape {

    public Triangle(int base, int height) {

        super(base, height);

    }


    @Override

    public void printArea() {

        System.out.println("Area of Triangle: " + (0.5 * dimension1 * dimension2));

    }

}


class Circle extends Shape {

    public Circle(int radius) {

        super(radius, radius);

    }


    @Override

    public void printArea() {

        System.out.println("Area of Circle: " + (Math.PI * dimension1 * dimension2));

    }

}


public class ShapeTest {

    public static void main(String[] args) {

        Rectangle rectangle = new Rectangle(5, 4);

        Triangle triangle = new Triangle(6, 8);

        Circle circle = new Circle(3);


        rectangle.printArea();

        triangle.printArea();

        circle.printArea();

    }

}


Algorithm:

  1. Define an abstract class Shape with two protected instance variables dimension1 and dimension2.
  2. Define a constructor in the Shape class to initialize these variables.
  3. Define an abstract method printArea() in the Shape class.
  4. Define three subclasses Rectangle, Triangle, and Circle, each extending the Shape class.
  5. Each subclass has its own constructor to initialize the dimensions inherited from the Shape class.
  6. Each subclass overrides the printArea() method to calculate and print the area of the respective shape.
  7. Define a class ShapeTest.
  8. Inside the ShapeTest class: a. Create objects of Rectangle, Triangle, and Circle. b. Call the printArea() method for each object to print the area of the respective shape.
  9. End of the program.








Popular posts from this blog

Write a program to create a class Student2 along with two method getData (), printData () to get the value through argument and display the data in printData. Create the two objects s1, s2 to declare and access the values from class STtest.

 CODE: import java.util.Scanner; class Student2 {     private String name;     private int age;          // Method to set data     public void getData(String name, int age) {         this.name = name;         this.age = age;     }          // Method to print data     public void printData() {         System.out.println("Name: " + name);         System.out.println("Age: " + age);     } } public class STtest {     public static void main(String[] args) {         Scanner scanner = new Scanner(System.in);                  // Creating objects of Student2 class         Student2 s1 = new Student2();         Student2 s2 = new Student2();              ...

Create an in-order threaded binary search tree and perform the traversals.(Double Thread)

 CODE: #include <iostream> using namespace std; struct Node {     int data;     Node* left;     Node* right;     bool leftThread;     bool rightThread; }; Node* createThreadedBST(int arr[], int n) {     Node* root = nullptr;     for (int i = 0; i < n; ++i) {         Node* newNode = new Node;         newNode->data = arr[i];         newNode->left = newNode->right = nullptr;         newNode->leftThread = newNode->rightThread = true;         if (root == nullptr) {             root = newNode;         } else {             Node* current = root;             Node* parent = nullptr;             while (true) {                ...

Create a BST and find inorder successor and inorder predecessor of specific node.

CODE: #include <iostream> using namespace std; // Node definition struct Node {     int data;     Node* left;     Node* right;     Node(int val) {         data = val;         left = nullptr;         right = nullptr;     } }; // Insertion in BST Node* insert(Node* root, int val) {     if (root == nullptr) {         return new Node(val);     }     if (val < root->data) {         root->left = insert(root->left, val);     } else {         root->right = insert(root->right, val);     }     return root; } // Finding inorder successor Node* findInorderSuccessor(Node* root, Node* target) {     if (target->right != nullptr) {         Node* current = target->right;         while (current->l...