A program to calculate the area of different shapes (circle, triangle,
rectangle, square) by using method overloading.
OUTPUT :-
<====Menu====>
1:area of rectangle
2:area of square
3:area of circle
4:area of Triangle
5:Exit
1
enter the value of length
6
enter the value of breadth
8
area of rectangle=48
<====Menu====>
1:area of rectangle
2:area of square
3:area of circle
4:area of Triangle
5:Exit
2
enter the value of side
5
area of square=25
<====Menu====>
1:area of rectangle
2:area of square
3:area of circle
4:area of Triangle
5:Exit
3
enter the value of radius
4
area of circle=50.24
<====Menu====>
1:area of rectangle
2:area of square
3:area of circle
4:area of Triangle
5:Exit
4
enter the value of base
3
enter the value of height
6
area of Triangle=9.0
<====Menu====>
1:area of rectangle
2:area of square
3:area of circle
4:area of Triangle
5:Exit
This is how you'll get the output, all you need to just enter the numbers from Menu list and implement your results. And here some observations, learning & conclusion over this program.
rectangle, square) by using method overloading.
Method
Overloading:
If a class has multiple methods by same name but different
parameters, it is known as Method
Overloading. If we have to perform only one operation, having same name of
the methods increases the readability of the program. Suppose you have to
perform addition of the given numbers but there can be any number of arguments,
if you write the method such as a(int,int) for two parameters, and b
(int,int,int) for three parameters then it may be difficult for you as well as
other programmers to understand the behavior of the method because its name
differs. So, we perform method overloading to figure out the program quickly.
Advantage
of method overloading?
Method overloading increases
the readability of the program.
Different
ways to overload the method
There
are two ways to overload the method in java
|
- By
changing number of arguments
- By
changing the data type
1) Method
Overloading: changing no. of arguments
In this
example, two methods are created, first add() method performs addition of two
numbers and second add method performs addition of three numbers.
In this
example, static methods are created so that don't need to create instance for
calling methods.
1.
class Adder{
2.
static int add(int a,int b){return a+b;}
3.
static int add(int a,int b,int c){return a+b+c;}
4.
}
5.
class TestOverloading1{
6.
public static void main(String[] args){
7.
System.out.println(Adder.add(11,11));
8.
System.out.println(Adder.add(11,11,11));
9.
}}
Output :
22
33
2) Method
Overloading: changing data type of arguments
In this
example, two methods are created that differs in data type. The first add
method receives two integer arguments and second add method receives two double
arguments.
1.
class Adder{
2.
static int add(int a, int b){return a+b;}
3.
static double add(double a, double b){return a+b;}
4.
}
5.
class TestOverloading2{
6.
public static void main(String[] args){
7.
System.out.println(Adder.add(11,11));
8.
System.out.println(Adder.add(12.3,12.6));
9.
}}
Output :
22
24.9
Method Overloading is not possible by changing the return
type of method only?
In java,
method overloading is not possible by changing the return type of the method
only because of ambiguity. Let's see how ambiguity may occur:
1.
class Adder{
2.
static int add(int a,int b){return a+b;}
3.
static double add(int a,int b){return a+b;}
4.
}
5.
class TestOverloading3{
6.
public static void main(String[] args){
7.
System.out.println(Adder.add(11,11));//ambiguity
8.
}}
Output :
Compile Time Error: method add(int,int) is
already defined in class Adder
Algorithm:-
1.
Start
2.
Define
class Shape with its attributes and methods area() with different type and
number of parameters.
- Define class
Demo
- Create object
of Shape class inside class Demo.
- Call
different overloaded area() methods using object of Shape.
- Stop
import
java.util.Scanner;
class
area
{
public void area(int x)
{
System.out.println("area of
square="+(x*x));
}
public void area(int x,int y)
{
System.out.println("area of
rectangle="+(x*y));
}
public void area(double x)
{
System.out.println("area of
circle="+(x*x*3.14));
}
public void area(double x,double y)
{
System.out.println("area of
Triangle="+(x*y*0.5));
}
}
class
A1
{
public
static void main(String args[])
{int
i;
Scanner s=new Scanner(System.in);
area
w=new area();
do
{
System.out.println("<====Menu====>\n
1:area of rectangle\n 2:area of square\n 3:area of circle\n 4:area of
Triangle\n 5:Exit");
i=s.nextInt();
switch(i)
{
case
1:System.out.println("enter the value of length");
int l=s.nextInt();
System.out.println("enter the
value of breadth");
int b=s.nextInt();
w.area(l ,b);
break;
case
2: System.out.println("enter the value of side");
int x= s.nextInt();
w.area (x);
break;
case
3: System.out.println("enter the
value of radius");
double e=s.nextDouble();
w.area(e);
break;
case
4:System.out.println("enter the value of base");
double u=s.nextDouble();
System.out.println("enter the
value of height");
double v=s.nextDouble();
w.area(u ,v);
break;
case
5:System.out.println("Thank you");
break;
default:System.out.println("invlid
choice");
break;
}
}while(i!=5);
}
}
OUTPUT :-
<====Menu====>
1:area of rectangle
2:area of square
3:area of circle
4:area of Triangle
5:Exit
1
enter the value of length
6
enter the value of breadth
8
area of rectangle=48
<====Menu====>
1:area of rectangle
2:area of square
3:area of circle
4:area of Triangle
5:Exit
2
enter the value of side
5
area of square=25
<====Menu====>
1:area of rectangle
2:area of square
3:area of circle
4:area of Triangle
5:Exit
3
enter the value of radius
4
area of circle=50.24
<====Menu====>
1:area of rectangle
2:area of square
3:area of circle
4:area of Triangle
5:Exit
4
enter the value of base
3
enter the value of height
6
area of Triangle=9.0
<====Menu====>
1:area of rectangle
2:area of square
3:area of circle
4:area of Triangle
5:Exit
This is how you'll get the output, all you need to just enter the numbers from Menu list and implement your results. And here some observations, learning & conclusion over this program.
Observations and learning:
Learned how
to use method in java, and passing parameters to methods and and calling
methods in java.Also learned the different return type of methods and learned
the method overloading and use of method overloading.
Conclusion:
By using method overloading we achieve the given problem. And learned the method
overloading in java.
No comments:
Post a Comment