' Area of Triangle, Rectangle and Triangle in Java using Method Overloading | Infolet

Area of Triangle, Rectangle and Triangle in Java using Method Overloading



Area of a Rectangle, Triangle and Circle in Java using Method Overloading

This is a Java program to find area of a rectangle, circle and triangle using method overloading. Method overloading is one of the ways that Java implements polymorphism. When an overloaded method is invoked, Java uses the type and number of arguments to determine which version of the overloaded method to actually call. Thus, overloaded methods must differ in the type and number of parameters.



Ø Manually we can find the area of a rectangle by using equation:

Area = Breadth * Length.

Ø Also we can find the area of a circle by using equation:

Area = 3.14 * (radius)2

Ø Also we can find the area of a triangle by using equation:

a = length of first side
b = length of second side
c = length of third side
s = (a + b+ c)/2
Area = sqrt(s*(s-a)*(s-b)*(s-c))
-where sqrt stands for square root

Program Code:

import java.io.*;
class area
{
void findarea(int a, int b)
    {
       System.out.println( "\n Area of rectangle with breadth "+a+" and lenght " +b+ " is :" + a*b);
    }

void findarea(int a)
       {
         System.out.println( "\n Area of circle with  radius " +a+ " is :" + 3.14 * a);
}

void findarea(int a, int b, int c)
    {
       double temp = (a + b + c);
       double s= temp/2;
       double triarea = Math.sqrt(s*(s-a)*(s-b)*(s-c));
        System.out.println( "\n Area of triangle with lenght of sides  "+a+"," +b+ " and " +c+" is : "+ triarea);
}
public static void main(String p[]) throws IOException
 {
     area d = new area();
     BufferedReader Br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("\n Find area of \n 1 . Rectangle \n 2 . Triangle \n 3 . Circle \n\nSelect a choice : ");
int choice =Integer.parseInt(Br.readLine());
switch(choice)
{
    case 1:
         System.out.print("\n Enter the breadth : ");
          int a =Integer.parseInt(Br.readLine());
         System.out.print("\n Enter the lenght : ");
          int b=Integer.parseInt(Br.readLine());
          d.findarea(a,b);
          break;
    case 2:
         System.out.print("\n Enter the lenght of first side : ");
         int x =Integer.parseInt(Br.readLine());
        System.out.print("\n Enter the lenght of second side : ");
         int y=Integer.parseInt(Br.readLine());
System.out.print("\n Enter the lenght of third side : ");
         int z =Integer.parseInt(Br.readLine());
         d.findarea(x,y,z);
         break;
    case 3:
        System.out.print("\n Enter the radius : ");
         int r =Integer.parseInt(Br.readLine());
d.findarea(r);
         break;
    default:
         System.out.println("Invalid choice");
      }
  }
}

Here class name is 'area', so you must save your java file in name of 'area.java'.

Sample Output:

Find Area of
1. Rectangle
2 . Triangle
3 . Circle

Select a choice: 1
Enter the breadth: 3
Enter the length: 5

Area of rectangle with breadth 3 and length 5 is : 15

Find calculatevArea of a Rectangle, Triangle and Circle in Jav



Responses

0 Respones to "Area of Triangle, Rectangle and Triangle in Java using Method Overloading"

Post a Comment

 

Free Updates

Enter your email address:

Delivered by FeedBurner

Learn C

About Author

Author of infolet.org
Muhammed Aslam PK, He is a MCA(Master of Computer Applications) student. See More here
Contact Us
Return to top of page Copyright © 2010 | Infolet.org Google Converted into Blogger Template by HackTutors