Results 1 to 12 of 12
  1. #1
    ToolJob is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default 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?

    Java 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);
    
      }
    
    }
    And the other class

    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;
    
      }
    
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,305
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ToolJob View Post
    Java Code:
      double computeBox()
      {
    
        return height * length;
    
      }
    Print both the height and length values in that method and you'll see what's happening.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    lovelesh is offline Senior Member
    Join Date
    Nov 2010
    Location
    Delhi
    Posts
    135
    Blog Entries
    1
    Rep Power
    0

    Smile

    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.

  5. #5
    überfuzz is offline Member
    Join Date
    Feb 2011
    Posts
    43
    Rep Power
    0

    Default

    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.

  6. #6
    ToolJob is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    Jos

    I did a

    Java Code:
      double computeBox()
      {
    
    
        System.out.println(height + "   " + length);
        return height * length;
    
      }
    The height and length still print out:

    0.0 0.0
    The width x height is: 0.0


    ToolJob

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  8. #8
    ToolJob is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    Fubarable

    Can you give me a simple example? Ill try it if I can see how it would work.

    Thx TJ

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by ToolJob View Post
    Fubarable

    Can you give me a simple example? Ill try it if I can see how it would work.

    Thx TJ
    You really don't need an example,... you just need to create one Joe object only, not three. Please try it, and you'll see. If it doesn't work, then please post the code.

  10. #10
    ToolJob is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    Lovelesh

    If I do:

    Java Code:
      double computeBox()
      {
    
        height = 5; length = 10;
        System.out.println(height + "   " + length);
        return height * length;
    
      }
    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?

    I been trying to make this work and my brain is fried LOL

    The values I set

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,305
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ToolJob View Post
    Lovelesh

    If I do:

    Java Code:
      double computeBox()
      {
    
        height = 5; length = 10;
        System.out.println(height + "   " + length);
        return height * length;
    
      }
    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?

    I been trying to make this work and my brain is fried LOL

    The values I set
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    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

  1. Method, returning reference to an object
    By Saletra in forum New To Java
    Replies: 3
    Last Post: 08-23-2010, 08:22 PM
  2. Returning Value from a method
    By Mirix in forum New To Java
    Replies: 12
    Last Post: 06-01-2010, 09:48 PM
  3. Inherited method returning bad value
    By viking90 in forum New To Java
    Replies: 11
    Last Post: 04-07-2010, 03:53 PM
  4. returning an object from a method
    By bigj in forum New To Java
    Replies: 7
    Last Post: 01-08-2010, 12:39 PM
  5. Need help. Method won't returning proper value..
    By zlwilly in forum New To Java
    Replies: 2
    Last Post: 12-02-2008, 09:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •