Find the area of triangle and rectangle in java


Find the area of triangle and rectangle in java

Week 6

a)Aim: To Write java program to create a super class called Figure that receives the dimensions of two dimensional objects. It also defines a method called area that computes the area of an object. The program derives two subclasses from Figure. The first is Rectangle and second is Triangle. Each of the sub class overridden area() so that it returns the area of a rectangle and a triangle respectively.

Description :

create a super class called Figure that receives the dimensions of two dimensional objects. It also defines a method called area that computes the area of an object. The program derives two subclasses from Figure. The first is Rectangle and second is Triangle. Each of the sub class overridden area() so that it returns the area of a rectangle and a triangle respectively.

Source code :

import java.io.*;

class Figure

{

            int l,w;

            Figure(int a,int b)

            {

                        l=a;

                        w=b;

            }

            void area()

            {

                        System.out.println(“Area of the Figure”);

            }

}

class Triangle extends Figure

{

            Triangle(int g,int h)

            {

                        super(g,h);

            }

            void area()

            {

                        System.out.println(“Area is “+(l*w)/2);

            }

}

class Rectangle extends Figure

{

            Rectangle(int k,int l)

            {

                        super(k,l);

            }

            void area()

            {

                        System.out.println(“Area is “+l*w);

            }

}

class A

{

            public static void main(String ar[])

            {

                        Triangle obj1=new Triangle(10,20);

                        Rectangle obj2=new Rectangle(20,20);

                        obj1.area();

                        obj2.area();

            }

}

Sample output :

Area is 100

Area is 400

Find the area of triangle and rectangle in java

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories