brenda flire wrote:I have written the class, but the area and perimeter methods don't work, and i don't know why. Do I have to initialize the area and the perimeter in the constructor?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
brenda flire wrote:Ok, I think I figured it out. I was getting all caught up in set and get methods which is not what was needed for the area & perimeter. Of course, after literally spending hours staring at this and trying different things, I figure it out within 10 minutes of posting for help.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
brenda flire wrote:Thank you for your explanation. It really helped me to see the issue in terms of concepts (and not just java variables & methods). Very useful!
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Spot on. And you're welcome!brenda flire wrote:Matthew...(bear with the numbers, it's how my brain sorts things out )
...(snip)...
Is this correct?
Correct. Now tell me what this little bit of crappy code will print out:-brenda flire wrote: . . .
1. I set an instance variable called area (let me call it area-i for my own clarity right now)
2. I didn't initialize this variable (it's absent from my constructor) so as a double, its default value is zero... area-i = 0
3. I made my setArea() method use a parameter also called area (now area-p)
4. Whatever I did in my setArea() method was on area-p, because it took precedence over area-i
5. area-p was never actually used for anything
6. Outside of setArea(), any reference to variable "area" used the instance variable area-i -- which was still 0.
and
7. area-p couldn't have been used outside my setArea() method anyway b/c it was a method variable. so even if i called it something besides 'area' and avoided the shadowing problem, it would still be useless for my purposes
. . .
Campbell Ritchie wrote:Correct. Now tell me what this little bit of crappy code will print out:-
Campbell Ritchie wrote:
But never say a double is equal to 0. Say it is 0.0.
Also, take note of what Winston said; a rectangle doesn’t actually have an area that you can measure. You can only measure length and breadth, and you calculate the area from them.
Don't get me started about those stupid light bulbs. |