0% found this document useful (0 votes)
2 views

04_Function-Overloading

Uploaded by

ABHYUDAY HARSH
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

04_Function-Overloading

Uploaded by

ABHYUDAY HARSH
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

FUNCTION OVERLOADING

“Function Overloading” is a form of polymorphism supported by Java that


allows two or more functions performing logically similar tasks to have the
same name.
Overloaded functions must vary on the basis of the number, type or
sequence of their arguments.

Ex-1: double cal(int x)


double cal(double x)

Ex-2: void compute(double a, int b)


void compute(int a, double b)

Ex-3: double score(int N)


void score(int x)
Q> Design a polymorphic function “area” to compute the area of a square,
rectangle or triangle.

class Schools
{

double area(double s)
{
return s*s;
}

double area(double l, double b)


{
return l*b;
}

double area(double a, double b, double c)


{
double s, ar;
s = (a+b+c)/2;
ar = Math.sqrt(s*(s-a)*(s-b)*(s-c));
return ar;
}

public static void main(String args[])


{
double rs1, rs2, rs3 ;

Schools ob = new Schools( ) ;

rs2 = ob.area(12.0, 8.0);


rs1 = ob.area(7.5) ;
rs3 = ob.area(10.0, 8.0, 12.0) ;

System.out.println("Area of Square = " + rs1) ;


System.out.println("Area of Rectangle = " + rs2) ;
System.out.println("Area of Triangle = " + rs3) ;

}}
import java.util.* ;

class ICSE
{

public static void main(String args[])


{
Scanner sc = new Scanner(System.in);

double s, l, b, x, y, z, rs1, rs2, rs3 ;

System.out.println("Enter the side of the square : ");


s = sc.nextDouble();

System.out.println("Enter the length & breadth of the rectangle : ");


l = sc.nextDouble();
b = sc.nextDouble();

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


x = sc.nextDouble();
y = sc.nextDouble();
z = sc.nextDouble();

Schools ob = new Schools() ;

rs2 = ob.area(l, b);

rs1 = ob.area(s) ;

rs3 = ob.area(x, y, z) ;

System.out.println("Area of Square = " + rs1) ;


System.out.println("Area of Rectangle = " + rs2) ;
System.out.println("Area of Triangle = " + rs3) ;

double area(double s1)


{
return s1*s1;
}
double area(double l1, double b1)
{
return l1*b1;
}

double area(double x1, double y1, double z1)


{
double s, ar;
s = (x1+y1+z1)/2;
ar = Math.sqrt(s*(s-x1)*(s-y1)*(s-z1));
return ar;
}

}
Q> WaP using method-overloading to find the volume of a cube / cuboid /
cylinder depending on the user’s choice.

import java.util.*;

class Volumes
{
double vol(double s) // Cube
{
return s*s*s;
}

double vol(double l, double b, double h) // Cuboid


{
return l*b*h;
}

double vol(double r, double h) // Cylinder


{
return Math.PI*r*r*h;
}

public static void main(String args[])


{
Scanner sc = new Scanner(System.in);
double x, y, z, v ;
int ch ;

Volumes ob = new Volumes( ) ;

System.out.println("Enter 1 for Cube, 2 for Cuboid and 3 for Cylinder : ") ;


ch = sc.nextInt() ;

switch(ch)
{
case 1 : System.out.println("Enter the side of the Cube : ") ;
x = sc.nextDouble() ;
v = ob.vol(x) ;
System.out.println("Volume of the Cube = " + v) ;
break ;

case 2 : System.out.println("Enter the length, breadth and height of the


Cuboid : ") ;
x = sc.nextDouble() ;
y = sc.nextDouble() ;
z = sc.nextDouble() ;
v = ob.vol(x, y, z) ;
System.out.println("Volume of the Cuboid = " + v) ;
break ;

case 3 : System.out.println("Enter the radius and height of the Cylinder : ")


;
x = sc.nextDouble() ;
y = sc.nextDouble() ;
v = ob.vol(x, y) ;
System.out.println("Volume of the Cylinder = " + v) ;
break ;

default : System.out.println("Invalid Choice.") ;


}

}}

You might also like