• Post Reply Bookmark Topic Watch Topic
  • New Topic

Practicing overloading in static methods  RSS feed

 
Michael Giara
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My professor is trying to explain (if I semi-understand correctly) that we will use the enumerated variable RECTANGLE and TRIANGLE to help java differentiate between the two area methods that both are defined by Shape, double, double.

I'm still getting an error though, after adding the enumerated variables and not sure why:




Obviously if I add the "half" variable into the triangle area method,

it solves the problem, but he states he wants it done this way:

The Overloaded area Methods
There are supposed to be three methods to calculate the area of three shapes, but overloading only works if the parameter lists can be unique. When we calculate the area of a circle, we only need a radius (one parameter), but when we calculate the area of a rectangle or a triangle, we need two parameters. For the rectangle it is the length and width. For the triangle it is the base and the height. It is impossible for Java to tell the difference between the rectangle and triangle based on the parameters if we use all double parameters and I do not want to force the parameters of one of the methods to be of another data type. That is why I decided to have you put in the enumerated type as a parameter.

An alternative would have been to just rename the methods to something like circleArea, rectangleArea, triangleArea, but I wanted you to have some fun with overloading methods and enumerated types.

So, to calculate the area of a circle, use this in your main program...

area = Geometry.area( Geometry.Shape.CIRCLE, radius );

Where area is your variable, Geometry.area is the public static method that you are calling, Geometry.Shape.CIRCLE is the enumerated type to indicate that you are calculating for a circle, and radius is your variable.

To calculate the area of a rectangle, use this...

area = Geometry.area( Geometry.Shape.RECTANGLE,
length, width );

To calculate the area of a triangle, use this...

area = Geometry.area( Geometry.Shape.TRIANGLE,
base, height );

You may note that the first area method for calculating the area of a circle does not really need the enumerated type, but we will put it in there for consistency so that it looks like the other methods.


I'm just a little fuzzy on how to make what he's saying work.
 
Michael Giara
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just wanted to state that my professor Strongly encourages us to seek help from any and all resources available to us.
He has no problem with us reaching out to classmates, seasoned programmers, himself, tutoring centers, or online communities.
I only feel the need to make that clear because I see frequently on other forums where when advice is requested, the only replies are "we're not going to do your homework for you..."

The way I'll draw that fine line is by writing the code that hopefully you all can help me correct, as opposed to posting the assignment all at once and saying "wahhhhhh, write me this program plzzzzz"

That said, I'll be throwing up my few questions here up until 11:30 pm pacific when I have to turn in my program, with hopes that other novices can use this information in the future!
 
Paul Clapham
Sheriff
Posts: 22843
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found that pretty confusing as well. Eventually I figured out that it was saying "There are supposed to be three methods, but you can't make that work with overloading, so we'll do this workaround."

So what you can conclude from it is, first of all, that writing three methods is not the right thing to do. And that's what you did.

If you read the second half of the instructions carefully and think about the rules for overloading ("parameter lists must be unique"), you should be able to figure out how many methods you should write. Clearly it's less than three. And since overloading is still one of the learning goals here, it must be more than one.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Asking for help on your code is fine -- as you point out, asking us to write it for you is not. So you're doing it right!

What you're not doing right is defining your methods. For example, the declaration at line 6 defines a method with a parameter named CIRCLE. The name CIRCLE does not match the enum instance with the same name. That's not how enums are used.

The methods at line 10 and 14 have the same signature (param names don't count) so that's why you are getting the compiler error.

I believe the point is for you to define one method that handles all the shape types based upon passing in a shape enum value.

[Edit: Paul's post snuck in before mine!]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And please, please, please, stop with single-character names! length is a perfectly good and honorable word, why dishonor it by abbreviating it to l?
 
Michael Giara
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So this is as far as I've gotten:


My problems are the following:
1. The line where I define the parameters of the second method is too long, and I don't know how to correct that. I tried going to the next line and prefixing it with a +, but that's not correct.
Where would I read up on how to correct this?
2. Where Bear pointed out that I'm not defining my methods, and how the name CIRCLE does not match the enum instance with the same name. I tried formally declaring it ie:
but obviously that's incorrect.
Where would I read up on how to correct/do this properly?
3.
gives the program no way to differentiate what to return, and when, resulting in


I don't want to keep dragging this out and getting closer and closer to asking for code, so here's my table of contents up to what we've covered in class.
Please let me know what section you suggest reading to possibly uncover the mysteries I've come upon.



 
Paul Clapham
Sheriff
Posts: 22843
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your problem isn't anything to do with Java, so that table of contents image is irrelevant. So far you've got some overloaded methods, that's good. One of them is correct but the other one isn't. They are perfectly good overloaded methods, at least their signatures are perfectly good, but your problem is that the second one doesn't follow the instructions you were given.

And remember that the second one is supposed to be used for rectangles and triangles -- but not at the same time! So its code is going to have to figure out which it is, and behave accordingly.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:And please, please, please, stop with single-character names! length is a perfectly good and honorable word, why dishonor it by abbreviating it to l?


Especially since the letter l looks so much like the number 1.


 
Michael Giara
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Giara wrote:
That is what you were supposed to think.

I actually don’t think that is a good way to learn about overloading because the real object‑oriented way to do it is for each of the elements in the enum to have its own getArea() method.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Giara wrote:So this is as far as I've gotten:...

Michael,

Please DontWriteLongLines. It makes your thread very hard to read. I've broken yours up this time.

Winston
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!