MAIN METHOD TRIANGLE PERIMETER JAVA

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.

PROGRAM:

NOTE:

·  Make sure you're running the correct class (ShapeDemo): Ensure that you're compiling and running the ShapeDemo class, which contains the main method.

·  Check your classpath or IDE settings: If you're using an IDE (like IntelliJ IDEA, Eclipse, etc.), ensure that the class containing the main method (ShapeDemo) is set as the entry point for your program.

 

PROGRAM:

package jo;

 abstract class Shape {

              abstract double calculateArea();

              abstract double calculatePerimeter();

              }

              class Circle extends Shape {

              double radius;

              public Circle(double radius)

              {

              this.radius = radius;

              }

              public double calculateArea() {

              return Math.PI * radius * radius;

              }

              public double calculatePerimeter()

              {

              return 2 * Math.PI * radius;

              }

              }

              class Triangle extends Shape

              {

              double base, height, side1, side2, side3;

              public Triangle(double base, double height, double side1, double side2,

              double side3) {

              this.base = base;

              this.height = height;

              this.side1 = side1;

              this.side2 = side2;

              this.side3 = side3;

              }

              @Override

              public double calculateArea() {

              return 0.5 * base * height;

              }

              @Override

              public double calculatePerimeter() {

              return side1 + side2 + side3;

              }

              }

              class ShapeDemo {

              public static void main(String[] args) {

              Circle circle = new Circle(5.0);

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

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

              System.out.println("\nTriangle - Area: " + triangle.calculateArea() +

              ", Perimeter: " + triangle.calculatePerimeter());

              }

              }

 OUTPUT:

NOTE:





No comments:

Post a Comment