Find the area of triangle and rectangle in java

Find the area of triangle and area of rectangle in java

To write a java program to create a superclass 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 the 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 superclass called Figure that receives the dimensions of two-dimensional objects. It also defines a method called area that computes the area of a triangle of an object. The program derives two subclasses from Figure. The first is Rectangle and the second is Triangle. Each of the sub-class overridden area() so that it returns the area of a triangle and a rectangle respectively.

Java Program to Calculate Area of Triangle 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 of triangle " + (l * w) / 2);
    }
    }

    class A
        {
            public static void main(String ar[])
        {
            Rectangle obj1 = new Rectangle(20, 20);

        obj1.area();
        }
        }

Output: Area of Triangle

Find the area of triangle in java

Java Program to Calculate Area of Rectangle 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 Rectangle extends Figure
{
Rectangle(int k,int l)
{
super(k,l);
}
void area()
{
System.out.println("Area of rectangle is "+l*w);
}
}

class A
{
public static void main(String ar[])
{
Triangle obj2 = new Triangle(20, 20);

obj2.area();
}
}

Output: Area of Rectangle

Find the area of rectangle in java

FAQ:

How do you find the area of a rectangle in Java?

To find the area of a rectangle, we have to know the formula to find the area of a rectangle and that is Area = ( l * w ), where L is the length and w is the width.

How do you find the area of a triangle in Java?

To find the area of a triangle, we have to know the formula to find the area of a triangle and that is Area = ( b * h ) / 2, Where b is the base and h is the height.

What is the formula for area of a Triangle?

The formula for the area of a triangle is: Area = ( b * h ) / 2, Where b is the base and h is the height.

What is the formula for area of a Rectangle?

The formula for the area of a rectangle is: Area = ( l * w ), where L is the length and w is the width.

Recommended Post:

Find the solution to the salesforce Question and many more

Pramod Kumar Yadav is from Janakpur Dham, Nepal. He was born on December 23, 1994, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.



Leave a Comment