0% found this document useful (0 votes)
6 views

Program 6

program

Uploaded by

kayo.sharma09
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Program 6

program

Uploaded by

kayo.sharma09
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

6) Develop a JAVA program to create an abstract class Shape with abstract methods

calculateArea() and calculatePerimeter(). Create subclasses Circle and Triangle that


extend the Shape class and implement the respective methods to calculate the area and
perimeter of each shape.

abstract class Shape {

// Abstract method to calculate the area

public abstract double calculateArea();

// Abstract method to calculate the perimeter

public abstract double calculatePerimeter();

class Circle extends Shape {

private double radius;

// Constructor

public Circle(double radius) {

this.radius = radius;

@Override

public double calculateArea() {

return Math.PI * radius * radius;

@Override

public double calculatePerimeter() {

return 2 * Math.PI * radius;

class Triangle extends Shape {

private double side1;

private double side2;

private double side3;


// Constructor

public Triangle(double side1, double side2, double side3) {

this.side1 = side1;

this.side2 = side2;

this.side3 = side3;

@Override

public double calculateArea() {

// Using Heron's formula to calculate the area of a triangle

double s = (side1 + side2 + side3) / 2.0;

return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));

@Override

public double calculatePerimeter() {

return side1 + side2 + side3;

public class Main {

public static void main(String[] args) {

Circle circle = new Circle(5.0);

System.out.println("Circle - Area: " + circle.calculateArea());

System.out.println("Circle - Perimeter: " + circle.calculatePerimeter());

Triangle triangle = new Triangle(3.0, 4.0, 5.0);

System.out.println("Triangle - Area: " + triangle.calculateArea());

System.out.println("Triangle - Perimeter: " + triangle.calculatePerimeter());

You might also like