aspose file tools*
The moose likes Beginning Java and the fly likes trying to do a calculation in a class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "trying to do a calculation in a class" Watch "trying to do a calculation in a class" New topic
Author

trying to do a calculation in a class

brenda flire
Greenhorn

Joined: Apr 07, 2013
Posts: 10

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

Joined: Apr 07, 2013
Posts: 10

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:


Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4392
    
    8

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.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7811
    
  21

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7811
    
  21

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

Joined: Apr 07, 2013
Posts: 10

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

Joined: Mar 17, 2011
Posts: 7811
    
  21

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

Joined: Apr 06, 2010
Posts: 4392
    
    8

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!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
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

Joined: Apr 07, 2013
Posts: 10

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
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
It prints 123.45, yes.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: trying to do a calculation in a class