File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes problem understanding methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "problem understanding methods" Watch "problem understanding methods" New topic
Author

problem understanding methods

robert205
Greenhorn

Joined: Jul 06, 2002
Posts: 1
I am currently taking an introductory course in java programming and am having difficulty understanding methods. I am writing a program that simulates a Craps game and have to make a rollDice method. I know how to get the numbers for the dice but do not understand how to create the method, more specifically whether it should return a value or be void. Having a very hard time comprehending the return value issue. I do not undresatnd what that means. Does it mean returning a value to main which is used to choose a particular switch case to execute or does returning a value only pertain to what will be printed on the screen? I am extremely frustrated and although I have read over and over about methods, it is just not clicking with me. I would be exrtemely greatful for anyone's advisement on this
Thank you,
Robert
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
If you will be needing the value that the rolldice method created, then you should have it return a value. You could call it from main or from another method. It depends on your needs.
public int rollDice(your parameters if any)
{
compute value
return value
}
You can call ist like this...
int myValue = rollDice(your parameters if any)
There is a very good tutorial at Sun's site too.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
Having a very hard time comprehending the return value issue. I do not undresatnd what that means. Does it mean returning a value to main which is used to choose a particular switch case to execute or does returning a value only pertain to what will be printed on the screen?

Returning a value means that when a method is used it returns a value to the method that called it, whether that method is main or some other method. For example:



The getSum() method returns an int (the sum of one and two) to the method that called it (aMethod()). The aMethod method doesn't return anything (void) because all it does is print the sum. So when anotherMethod calls aMethod(), it will get nothing in return.

The method that receives the returned value (if it receives a returned value) can do whatever it wants to with the value it got -- use it in a switch, print it, or something else.

In your case, since you probably want to do something with the sum of the dice, I recommend you return a value to the method that calls rollDice() so that method can do something with the result.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
By the way, robert205, welcome to JavaRanch.

We do have one rule around here. Please adjust your display name to meet the JavaRanch Naming Policy. You can change it here.

Thanks!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: problem understanding methods
 
Similar Threads
Return Question
Field set in action method coming null in JSP
Having problems calling a method?
Anyone willing to look at this code
boolean return