• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

trying to do a calculation in a class

 
Greenhorn
Posts: 10
Mac Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hola...

I don't know if my title is correct, but here's my issue. I'm trying to create a class (Rectangle) that has attributes of length and width. Also within this class i want to calculate the area and perimeter of the rectangle.

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? The methods never get accessed (if I put a print statement in them, it doesn't show) and I'm sure I'm misunderstanding some basic concept because I don't understand why those methods aren't doing anything. Please help me see what I'm missing.

I use a separate class (RectangleTest) to instantiate some Rectangle objects and call the toString method for each one. I don't have any problem with that... it's just that 'area' and 'perimeter' are always zero.

Here's my code for the Rectangle class -- help would be much appreciated:
 
brenda flire
Greenhorn
Posts: 10
Mac Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

However, even though I got it to work, I still don't really understand why the way I had it didn't work, so can someone explain to me why the set & get methods I had for area & perimeter weren't doing anything? I guess the corollary to this question is, why is the way that I've now changed it to (see below) the correct (meaning it does what I want it to) way to do it?

Here's the relevant working code I ended up with:


 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Brenda. Welcome to the Ranch!

Here's what was going on in your previous version:

In the setArea method you've got two separate area variables. One is the parameter that is passed in, and the other is the instance variable declared on line 5. The first of these takes priority - the term commonly used is that this shadows the instance variable. So when you try and update area on line 13 you're just updating the method parameter. The instance variable is unaffected.

In the presence of a shadowing parameter like this, you could have updated the instance variable like this:
But you'll notice now that the parameter isn't used anywhere. The solution you came up with yourself is the correct approach.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?


Hola Brenda, and welcome to JavaRanch.

To answer your question: Based on the way you've written your class, it's probably best; however, there is another way of looking at it:

Length and width are clearly primary attributes of a Rectangle: If you don't know them, you'll never be able to do anything. However, 'area' and 'perimeter' are functions of length and width - in database design, we call these sorts of values "derived data", because there really is no reason to store them.

Think about this: If you allow someone to change the width or length of your Rectangle, you then also have to update area and perimeter to keep them in sync. However, if you wrote something like:you don't have to bother with any of that, so you save yourself a pile of code (and thought).

Also: you have a setArea() method. If you run that, your area will now be out of sync with width and length. How do you propose to correct that?
(Hint: it can be done; but it's tricky, and involves making some assumptions - like keeping the same proportions)

See if what I've said makes any sense, and come back if you have any more questions.

Winston
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.


It's very often the case. You basically worked out what my post was about, but I leave it for you as an explanation of why it's so.

It's also one of the reasons why, when you run into problems, we usually advise people to StopCoding (←click).

Winston
 
brenda flire
Greenhorn
Posts: 10
Mac Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks so much, Matthew and Winston! I think I understand what you both are saying... but let me double-check.

Matthew...(bear with the numbers, it's how my brain sorts things out )
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

Is this correct?


Winston...
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!
And thanks for the StopCoding page; it's great advice and a good reminder. A couple months ago I was always writing pseudocode first and I let that fall by the wayside. Forming bad habits . I'll work on being more diligent about understanding what before how.

Really appreciate the info, guys!
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!


Oh good. At least I got one thing right this week.

And you're most welcome.

Winston
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

brenda flire wrote:Matthew...(bear with the numbers, it's how my brain sorts things out )

...(snip)...

Is this correct?

Spot on. And you're welcome!
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome, again

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

. . .

Correct. Now tell me what this little bit of crappy code will print out:-

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.
 
brenda flire
Greenhorn
Posts: 10
Mac Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Correct. Now tell me what this little bit of crappy code will print out:-



Uh-oh, and I thought I was understanding my mess. Ok... I think this will print out 123.45. But I'm not sure.
I think line 4 uses 5.0 & 4.0 to set that local area (within the method), which would then equal 20.0. Then, nothing else happens with that because it doesn't get returned or anything else. So your line 5 prints out the value for area that it has, which is still 123.45. Is this right?

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.



Noted .
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It prints 123.45, yes.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic