Get your CodeRanch badge!*
The moose likes Beginning Java and the fly likes Using a value from a void method in another separate class. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using a value from a void method in another separate class." Watch "Using a value from a void method in another separate class." New topic
Author

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

WeiJie Lim
Ranch Hand

Joined: Sep 05, 2012
Posts: 85
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

Joined: Mar 26, 2012
Posts: 185

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.


The biggest gamble will be to ask a question whose answer you know in that it will challenge your theory | www.TechAspire.blogspot.in
WeiJie Lim
Ranch Hand

Joined: Sep 05, 2012
Posts: 85
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

Joined: Oct 02, 2003
Posts: 10906
    
  12

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 1935
    
  28

Are you sure this is correct?
WeiJie Lim
Ranch Hand

Joined: Sep 05, 2012
Posts: 85
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

Joined: Dec 08, 2012
Posts: 47
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

Joined: Mar 17, 2011
Posts: 7024
    
  16

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


Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using a value from a void method in another separate class.
 
Similar Threads
Programing challenge
Help with Circle Class :)
Circle program errors
constructor
Class get and set explanation