Results 1 to 12 of 12
Thread: My method keeps returning 0
- 03-27-2011, 03:27 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
My method keeps returning 0
Hi I have the following two class files. How come my method keeps spitting out 0??? I can't for the life of me get it to calculate and display the length x height it always outputs zero?
And the other classJava Code:public class UseJoe { public static void main(String[] args) { Joe boxH = new Joe(); Joe boxL = new Joe(); boxH.setHeight(55.5); boxL.setLength(10.0); //System.out.println("The width x height is: "); Joe calcTotal = new Joe(); //calcTotal.computeBox(); double cTotal = calcTotal.computeBox(); System.out.println("The width x height is: " + cTotal); } }
Java Code:public class Joe { private double length; private double height; public double getLength() { return length; } public void setLength(double l) { length = l; } public double getHeight() { return height; } public void setHeight(double h) { height = h; } double computeBox() { return height * length; } }
-
You really don't want to create three separate Joe objects since each is completely distinct from the other. For instance you set the height of boxH, you set the length of boxL, and then do the calculations with calcTotal, and this makes no sense, since setting the fields of the two previous Joe objects has absolutely no effect on the third.
Why not just create one Joe object, set it's height and length and do the calculations with that single Joe object?Last edited by Fubarable; 03-27-2011 at 03:31 PM.
- 03-27-2011, 03:30 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 03-27-2011, 03:32 PM #4
Senior Member
- Join Date
- Nov 2010
- Location
- Delhi
- Posts
- 135
- Blog Entries
- 1
- Rep Power
- 0
You are not setting any values in your third Joe object.
And default value for double is zero (0.0). Hence you are getting zero always.
- 03-27-2011, 03:38 PM #5
Member
- Join Date
- Feb 2011
- Posts
- 43
- Rep Power
- 0
Try to set some values before you start to calculate stuff. It seems you're tangled up in the object orientated code here. ;-)
Joe test = new Joe;
Now you have to set the values in the object you wanna tinker with. In this case it would be something like text.setHeight(10); You're making tree objects but you only give the first two height and width. Try giving the Joe object calcTotal some height and width and see what happens.
- 03-27-2011, 03:53 PM #6
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
Jos
I did a
The height and length still print out:Java Code:double computeBox() { System.out.println(height + " " + length); return height * length; }
0.0 0.0
The width x height is: 0.0
ToolJob
-
Please read all the replies to your question.
- 03-27-2011, 03:55 PM #8
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
Fubarable
Can you give me a simple example? Ill try it if I can see how it would work.
Thx TJ
-
- 03-27-2011, 04:23 PM #10
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
Lovelesh
If I do:
The values are printed when I set them to a value in the method. I guess I am way off course here - I was under the impression private variables can be used in methods of the same class?Java Code:double computeBox() { height = 5; length = 10; System.out.println(height + " " + length); return height * length; }
I been trying to make this work and my brain is fried LOL
The values I set
- 03-27-2011, 04:45 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
In your code you have three different Joes: named BoxH, BoxL and calcTotal; you set the length of BoxL, you set the height of BoxH and you ask calcTotal to calculate the area. It will be zero of course because you never set the height nor length of the calcTotal object. Use one Joe object instead; set its length and height and then ask it to calculate its area.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-27-2011, 05:22 PM #12
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
As the others have said, every time you do setHeight etc. You're setting height for only that instance of Joe (whatever object you did it with). Which means that, since you never set the height for the calcTotal object, it will return 0 since the length and width values are 0 for that instance of Joe.
Similar Threads
-
Method, returning reference to an object
By Saletra in forum New To JavaReplies: 3Last Post: 08-23-2010, 08:22 PM -
Returning Value from a method
By Mirix in forum New To JavaReplies: 12Last Post: 06-01-2010, 09:48 PM -
Inherited method returning bad value
By viking90 in forum New To JavaReplies: 11Last Post: 04-07-2010, 03:53 PM -
returning an object from a method
By bigj in forum New To JavaReplies: 7Last Post: 01-08-2010, 12:39 PM -
Need help. Method won't returning proper value..
By zlwilly in forum New To JavaReplies: 2Last Post: 12-02-2008, 09:44 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks