You are on page 1of 2

EXPERIMENT NO 5

METHOD OVERLOADING

AIM
Write a Java program to find the area of square, rectangle and triangle using method overloading.

COURSE OUTCOME
CO1 Develop programs using Java class & object

MODULE OUTCOME
M1.05 Implement programs using method overloading

PROGRAM
import java.io.*;
class Shape
{
double area;

void area(float x)
{
area = x * x;
}

void area(float x, float y)


{
area = x * y;
}

void area(float x, float y, float z)


{
double s; s=(x+y+z)/2.0;
area = Math.sqrt(s*(s-x)*(s-y)*(s-z));
}

void display()
{
System.out.println("Area is "+area);
}
}
public class Exp4
{
public static void main(String[] args) throws IOException
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
float a,b,c;
Shape S=new Shape();

System.out.print("Enter side of square");


a = Float.parseFloat(br.readLine());
S.area(a);
S.display();

System.out.print("Enter length and breadth of reactanble");


a = Float.parseFloat(br.readLine());
b = Float.parseFloat(br.readLine());
S.area(a,b);
S.display();

System.out.print("Enter three sides of the triangle");


a = Float.parseFloat(br.readLine());
b = Float.parseFloat(br.readLine());
c = Float.parseFloat(br.readLine());
S.area(a,b,c);
S.display();
}
}

OBSERVATIONS
Enter side of square5
Area is 25.0
Enter length and breadth of reactanble 10 5
Area is 50.0
Enter three sides of the triangle 2 3 4
Area is 2.9047375096555625

RESULT
Studies method overloading in Java.

You might also like