• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to pass value to methods  RSS feed

 
Andy Anderson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've really been stuck with this for 2 days now; I'm a complete nooby to coding ..... does anyone understand how come this won't display the values I'm trying to pass into the methods:

 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch



Inside your calculateArea you try to compute using the values of variables lenght and width (which default to zero incidently)


Solution: either assign the values of x & y to them and then compute
OR
compute with x & y
 
Lalit Sahu
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andy Anderson wrote:I've really been stuck with this for 2 days now; I'm a complete nooby to coding ..... does anyone understand how come this won't display the values I'm trying to pass into the methods:



Andy Anderson wrote: does anyone understand how come this won't display the values I'm trying to pass into the methods:
You declared two variable length and breadth whose values declared at  TextBox i.e 5 and 10.Calculate "Area Method" using length and breadth to calculate area(look at calculateArea method) Therefor value calculated of area is 50 .

Argument passed through calculateArea(3,8)  received by   x and y i.e x=3 and y=8 but x and y never used to calculate area because length *breadth used to calculate area.

If you want to calculate area using x and y parameter instead of length and breadth then your code look like this
    now it calculate area via x and y argument.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say you are both wrong about how to calculate the area in object‑oriented programming. You have a Box object, and you set up its dimensions in its constructor. You now have length and width set, so you don't need to pass anything to the method at all. Your method takes nothing and returns the area. If the method is called getArea, it shou‍ld only return the area, not print it on screen.
 
Fred Kleinschmidt
Bartender
Posts: 572
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie has given you the proper way to use OOP with your Box class.
But you could declare your calculateArea method as static, to use to compute the area of a Box without using an instance of Box.Then you would say

to caluclate the area of boxes that are 3x8 and 22x56, respectively.
Note that this would be similar in style to the pow() method of Math where you use

to compute the 2.5th power of 12, without having to create an instance of Math.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I would say you are both wrong about how to calculate the area in object‑oriented programming. You have a Box object, and you set up its dimensions in its constructor. You now have length and width set, so you don't need to pass anything to the method at all. Your method takes nothing and returns the area. If the method is called getArea, it shou‍ld only return the area, not print it on screen.


True.
But the OP asked about value passed as argument not being used.
does anyone understand how come this won't display the values I'm trying to pass into the methods:
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Maneesh, for not getting annoyed when I disagreed with you.
I think there are two conflicting demands here: using proper object‑oriented code, and using the arguments passed. I would say you shou‍ld have a class like this:-\u2715 looks like this:- ✕
Note:-
  • 1: I have written the class so as to be immutable without inheritance. No setXXX methods or similar.
  • 2: You can add equals and hashCode methods if you wish.
  • Now, if you want to pass arguments to that method, you end up with this sort of thing:-Now let's try the same code again:-That won't work, so we pass two numbers to that method call.[/code]Now let's try the same code again:-So what is the area of that Rectangle? Is it 50 or 36? You can either revert to the no‑arguments version of the getArea method, and get the “correct” area of 50, or you can recognise that 4, 9 has no relationship to the Rectangle object and it is a method quite independent of rectangles. You can implement it like this:-Now, you can have rectangles of any size and that method will tell you their areas.
    I would implement that method slightly differently myself; I would also add some tweaks to the above Rectangle constructor, but those changes don't affect the principle of my arguments. You shoul‍ld also add documentation comments to all that code.
     
    Andy Anderson
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks all for being kind in your replies .... after reading each reply I now understand why it's not working the way it thought it should of. 
     
    Campbell Ritchie
    Marshal
    Posts: 56599
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Andy Anderson wrote:. . . I now understand . . .
    That makes it all worth while and well done.
    To the resolved button!
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!