• 02-28-2012, 08:44 PM
ankiit
Hi Mentors,

I want to calculate the area of circle, triangle, rectangle, square & cube within a single program with the help of function overloading. the issue i am facing is that that in some of the area function definitions I have only one parameter.

please have a look at my code:-
Code:

``` /*circle, triangle, rectangle, square, pentagon, cube, cylinder, cone, cuboid*/ class AreaShape {                         int area;                 void Area(int b, int h)                        //triangle                 {                 area = (b*h)/2;                 System.out.println("area of triangle is:-" + area);                 }                                 void Area(int r)                        // circle                 {                 area = (int) 3.14*r*r;                 System.out.println("area of circle is:-" + area);                 }                 void Area(int w , int h)                //rectangle                 {                 area = w*h;                 System.out.println("area of rectangle is:-" + area);                 }                                 void Area(int s)                        //square                 {                 area = s*s;                 System.out.println("area of square is:-" + area);                 }                         void Area(int a)                //cube                 {                 area = 6*a*a;                 System.out.println("area of square is:-" + area);                 }                         } class CalArea {         public static void main(String [] args)         {         AreaShape obj1 = new AreaShape();         obj1.Area(4,5);         obj1.Area(3);         obj1.Area(1,4);                obj1.Area(6);         obj1.Area(7);         } }```

error i am getting is as below:-
Attachment 3103

I want to use only one data type either an int or a float as arguments to my methods.
• 02-28-2012, 08:54 PM
Diargg
Quote:

int area;
void area(int r)
Java is case sensitive.

EDIT: This is incorrect. Variables and methods can have the same name. Perhaps not good style, but still compile-able.
• 02-28-2012, 09:08 PM
ankiit
Quote:

Originally Posted by Diargg
Java is case sensitive.

I am using function overloading, in which every function has same name.

My point is that i want to use on integers as an argument to the functions, since area of square is a*a and for cube is 6*a*a. I have an issue with this function overloading.
• 02-28-2012, 09:16 PM
Diargg
Forgive me, I saw an error where there wasn't one. Turns out you can have variables with the same name as a method. Go figure.

If I say "area(6)", do you know which one I'm talking about? If you don't, neither does your program. You need different parameters, switching inside the method, or different method names.

Also:
Quote:

void Area(int b, int h)
void area(int r)
• 02-28-2012, 09:45 PM
DarrylBurke
Quote:

Originally Posted by ankiit

Java has methods. Not functions.

db
• 03-01-2012, 02:22 PM
ankiit
Quote:

Originally Posted by DarrylBurke
Java has methods. Not functions.

db

Hi,

• 08-20-2012, 06:41 PM
cobwer
In order to make overloading works, you have to have unique signatures.
Code:

```void Area(int b, int h) {...} void Area(int r) {...}```
they are ok.
Since they use different signature you can call:
Code:

```Area(10, 20); // to execute the first function or.. Area(5); // to execute the second function```

But the signature of the fallowing functions are identical, even though the variable name are different because it doesn't matter, the types are the same
Code:

```void Area(int b, int h){...} void Area(int w, int h) {...}```
and also the same for
Code:

```void Area(int r){...} void Area(int s){...} void Area(int a){...}```
Here the definition is ambiguous.
• 08-20-2012, 08:43 PM
Zyril
```public class Geometry {         public static void main(String[] args) {               circleArea(3);       }             public static void circleArea(int radius){               double area = Math.PI*Math.pow(radius, 2);               System.out.println(area);       } }```