Ex No.4
Ex No.4
Aim
To write a Java Program to create an abstract class named shape that
contains two integers and
an
empty method named printArea. Provide three classes named
Rectangle,Triangle and Circle
subclass that each one of the classes extends the Class Shape.Each one of
the classes contains
only the method printArea() that prints the area of Shape.
Algorithm
Step 1: Start the program.
Step 2: To create an abstract class named shape that contains two integers
and an empty method
named printArea.
Step 3: Provide three classes named Rectangle,Triangle and Circle as a
subclass that each one of
the classes extends the Class Shape.
Step 4: Each one of the classes contains only the method printArea() that
prints the area of Shape.
Step 4.1: classes named Rectangle
area= x * y;
System.out.println("Area of Rectangle is " +area);
Step 4.2: classes named Triangle
area= (x * y) / 2;
System.out.println("Area of Triangle is " + area);
Step 4.3: classes named Circle
area=(22 * x * x) / 7;
System.out.println("Area of Circle is " + area);
Step 5: Stop the program.
Program
import java.util.*;
abstract class shape {
public int x,y;
public abstract void printArea();25
}
class Rectangle1 extends shape
{
public void printArea() {
float area;
area= x * y;
System.out.println("Area of Rectangle is " +area);
}}
class Triangle extends shape
{
public void printArea()
{ float area;
area= (x * y) / 2;
System.out.println("Area of Triangle is " + area);
}} class Circle extends shape
{
public void printArea()
{ float area;
area=(22 * x * x) / 7;
System.out.println("Area of Circle is " + area);
}}
public class Shapes
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter values : ");
int x1=sc.nextInt();
int y1=sc.nextInt();
Rectangle1 r = new Rectangle1();
r.x = x1; r.y = y1;
r.printArea();
Triangle t = new Triangle();
t.x = x1; t.y = y1;
t.printArea();
Circle c = new Circle();
c.x = x1;
c.printArea();
}}
Output
D:\JavaPrograms>java Shapes
Enter values :
78
Area of Rectangle is 56.0
Area of Triangle is 28.0
Area of Circle is 154.0
Result
Thus the Java program to create an abstract class was executed successfully.