0% found this document useful (0 votes)
14 views3 pages

Ex No.4

Uploaded by

catvoice22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views3 pages

Ex No.4

Uploaded by

catvoice22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

EX.

NO:4 CREATE AN ABSTRACT CLASS – SHAPE CLASS

Create a Java application to find the area of different shapes using


abstract class.
Use the abstract class Shape that include two integers and an
empty method named
printArea(). Construct the classes Rectangle, Triangle and Circle
inherited from the class
Shape. The Derived classes should include only the method
printArea() that print the area
of the given shape.
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.

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.

You might also like