Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using a value from a void method in another separate class.

 
WeiJie Lim
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to get the area of the circle with its original radius value tripled ( using the enlargeCircle method, but is void, so I cannot invoke it in TestCircle class ) =/ .

Guidance is appreciated



 
Rajdeep Biswas
Ranch Hand
Posts: 216
1
Eclipse IDE Java Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, please take care of naming conventions seriously,
Circle circle1 = new Circle(2.0);

If you want to enlarge the circle, you can call enlargeCircle() method with Circle class's object reference. Then the radius increased, and you can again call getRadius() and getArea() methods to get the enlarged radius and area of the circle.
What more do you expect, please explain.
 
WeiJie Lim
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajdeep Biswas wrote:First of all, please take care of naming conventions seriously,
Circle circle1 = new Circle(2.0);

If you want to enlarge the circle, you can call enlargeCircle() method with Circle class's object reference. Then the radius increased, and you can again call getRadius() and getArea() methods to get the enlarged radius and area of the circle.
What more do you expect, please explain.


Thanks alot. Sorry I just couldn't figure out the flow of the program just now.. I thought I have to store the return value of the void method to do it.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12097
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WeiJie Lim wrote:Thanks a lot. Sorry I just couldn't figure out the flow of the program just now.. I thought I have to store the return value of the void method to do it.

umm...void methods don't HAVE return values. That's what 'void' means.
 
Tony Docherty
Bartender
Posts: 2952
59
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Are you sure this is correct?
 
WeiJie Lim
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:
Are you sure this is correct?


Oops. It should be . My bad =X.

fred rosenberger wrote:
umm...void methods don't HAVE return values. That's what 'void' means.


Yup, that's why I encountered the problem..
 
Steve Myers
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The new value of radius is stored in the instance variable radius, this is a side effect of calling the enlargeCircle() method.
so



as others have mentioned the variable should be called circle1 not Circle1
 
Winston Gutkowski
Bartender
Pie
Posts: 10243
58
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WeiJie Lim wrote:Guidance is appreciated

The following have nothing to do with your problem, but are just a couple of guidelines for design:

1. Try not to be redundant when naming methods.
The name enlargeCircle() is redundant, since you already know that the class is a Circle, so you really only need to call it enlarge().
Furthermore, if at some point in the future you decide to make your Circle class part of some sort of Shape hierarchy that also includes Square and Triangle, you'll be very glad that you gave your method a more generic name - enlargeCircle() doesn't make much sense for a Triangle, but enlarge() most certainly does.
Method names should generally describe an action, not a type - at least, not the type of the class that defines it.

2. When you create methods, think about flexibility.
An enlarge() method can only do one thing, but amethod allows your users to change the size any way they like.

HIH

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic