Week-8:
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.
import java.util.Scanner;
abstract class Shape
{
int length;
int breadth;
abstract void printArea();
}
class Rectangle extends Shape
{
void printArea()
{
int area = length *
breadth;
System.out.println("Area
of Rectangle: " + area);
}
}
class Triangle extends Shape
{
void printArea()
{
double area = 0.5 * length
* breadth;
System.out.println("Area
of Triangle: " + area);
}
}
class Circle extends Shape
{
void printArea()
{
double area = Math.PI *
length * length;
System.out.println("Area
of Circle: " + area);
}
}
class Main
{
public static void
main(String[] args)
{
Scanner sc = new
Scanner(System.in);
Rectangle rect = new
Rectangle();
System.out.print("Enter
length of rectangle: ");
rect.length = sc.nextInt();
System.out.print("Enter
breadth of rectangle: ");
rect.breadth =
sc.nextInt();
rect.printArea();
Triangle tri = new
Triangle();
System.out.print("\nEnter
base of triangle: ");
tri.length = sc.nextInt();
System.out.print("Enter
height of triangle: ");
tri.breadth = sc.nextInt();
tri.printArea();
Circle c = new Circle();
System.out.print("\nEnter
radius of circle: ");
c.length = sc.nextInt(); //
Only length is used as radius
c.printArea();
}
}