• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem understanding methods

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 would give a normal human mental abilities to rival mine. To think it is just a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic