Aim:
To write a java program to calculate the area of rectangle, circle and triangle using the concept of an abstract class.
Procedure:
- Create an abstract class named shape that contains two integers and an empty method named printarea().
- Provide three classes named rectangle, triangle and circle such that each one of the classes extends the class Shape.
- Each of the inherited class from shape class should provide the implementation for the method printarea().
- Get the input and calculate the area of rectangle,circle and triangle .
- In the shapeclass , create the objects for the three inherited classes and invoke the methods and display the area values of the different shapes.
Program:
import java.util.*; abstract class shape { int a,b; abstract public void printarea(); } class rectangle extends shape { public int area_rect; public void printarea() { Scanner s=new Scanner(System.in); System.out.println("enter the length and breadth of rectangle"); a=s.nextInt(); b=s.nextInt(); area_rect=a*b; System.out.println("Length of rectangle "+a +"breadth of rectangle "+b); System.out.println("The area ofrectangle is:"+area_rect); } } class triangle extends shape { double area_tri; public void printarea() { Scanner s=new Scanner(System.in); System.out.println("enter the base and height of triangle"); a=s.nextInt(); b=s.nextInt(); System.out.println("Base of triangle "+a +"height of triangle "+b); area_tri=(0.5*a*b); System.out.println("The area of triangle is:"+area_tri); } } class circle extends shape { double area_circle; public void printarea() { Scanner s=new Scanner(System.in); System.out.println("enter the radius of circle"); a=s.nextInt(); area_circle=(3.14*a*a); System.out.println("Radius of circle"+a); System.out.println("The area of circle is:"+area_circle); } } public class shapeclass { public static void main(String[] args) { rectangle r=new rectangle(); r.printarea(); triangle t=new triangle(); t.printarea(); circle r1=new circle(); r1.printarea(); } }
Output:

Result:
Thus a java program for calculating the area of a rectangle, circle and triangle were implemented and executed successfully.