0% found this document useful (0 votes)
7 views2 pages

Abstarct Class

The document describes a Java program that defines an abstract class Shape with methods for calculating area and perimeter. It includes subclasses Circle and Triangle that implement these methods using specific formulas. The program also demonstrates the creation of Circle and Triangle objects and displays their area and perimeter calculations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Abstarct Class

The document describes a Java program that defines an abstract class Shape with methods for calculating area and perimeter. It includes subclasses Circle and Triangle that implement these methods using specific formulas. The program also demonstrates the creation of Circle and Triangle objects and displays their area and perimeter calculations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Program 06: Abstract Class

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.

Java Code:
abstract class Shape {
abstract double calculateArea();
abstract double calculatePerimeter();
}
class Circle extends Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
double calculateArea() {
return Math.PI * radius * radius;
}
@Override
double calculatePerimeter() {
return 2 * Math.PI * radius;
}
}
class Triangle extends Shape {
private double side1;
private double side2;
private double side3;
public Triangle(double side1, double side2, double side3) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
@Override
double calculateArea() {
// Using Heron's formula to calculate the area of a triangle
double s = (side1 + side2 + side3) / 2;
return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
}
@Override
double calculatePerimeter() {
return side1 + side2 + side3;
}
}
public class ShapeDemo {
public static void main(String[] args) {
// Creating Circle and Triangle objects
Circle circle = new Circle(5.0);
Triangle triangle = new Triangle(3.0, 4.0, 5.0);
// Calculating and displaying area and perimeter
System.out.println("Circle Area: " + circle.calculateArea());
System.out.println("Circle Perimeter: " + circle.calculatePerimeter());
System.out.println("\nTriangle Area: " + triangle.calculateArea());
System.out.println("Triangle Perimeter: " + triangle.calculatePerimeter());
}
}
Circle Area: 201.06192982974676
Circle Perimeter: 50.26548245743669

Triangle Area: 4.565615374733186


Triangle Perimeter: 18.3

You might also like