Reputation: 7074
I come from a procedural programming background, and need some help grasping how methods pass variables back and forth. The methods work with explicitly declared values, but when I try to pass values from one method to another, I get all kinds of "error: cannot find symbol" messages.
I suspect that I'm either a.) declaring the variables in the wrong place and they clear out at the end of the method or b.) how I'm coding the send/return of variables is wrong, or c.) both. More importantly, while I've read my textbook and a number of online tutorial resources, I'm still having trouble grasping how the syntax is supposed to work. Can someone clue me in?
The program: In the code provided, I'm trying to roll five six-sided dice. One method rolls a single die, the other calls that method multuiple times and writes the values to the array...I think.
Thanks in advance, d
public class FiveDice
{
// MAIN METHOD
public static void main(String[] args)
{
// SET VARIABLES FOR DIE HIGH AND LOW VALUES, NUMBER OF DICE TO ROLL
final int LOWEST_DIE_VALUE = 1;
final int HIGHEST_DIE_VALUE = 6;
final int DICE_TO_ROLL = 5;
// ROLL A SINGLE DIE VIA METHOD rollADie()
int roll = rollADie(HIGHEST_DIE_VALUE,LOWEST_DIE_VALUE);
System.out.println("roll " + roll);
}
//
// RETURNS THE RESULT OF A SINGLE DIE ROLL
public static int rollADie(int HIGHEST_DIE_VALUE,int LOWEST_DIE_VALUE)
{
int roll;
roll = ((int)(Math.random()*100)%HIGHEST_DIE_VALUE+LOWEST_DIE_VALUE);
return roll;
}
//
// CALL rollADie TO ROLL DICE_TO_ROLL (above) DICE; RETURN ARRAY OF ROLLED DICE
public static int[] rollTheDice(int DICE_TO_ROLL, int HIGHEST_DIE_VALUE,int LOWEST_DIE_VALUE)
{
int rollNum;
int rolledDie;
for(rollNum=1;rollNum<=DICE_TO_ROLL;rollNum++)
{
int[] rolledDie = new rollADie(HIGHEST_DIE_VALUE,LOWEST_DIE_VALUE)
{
rolledDie
};
return rolledDie[];
}
}
}
Upvotes: 2
Views: 714
Reputation: 33954
I'll be honest, this is a total mess (but we were all new at one time, right)? What you really need to do is just go through the tutorials on writing Java apps, and especially object-oriented concepts, and learn as you go. The fundamental object-oriented concept, that methods represent actions/abilities of an object they're attached to, as opposed to being a list of mostly standalone functions/subroutines, will really help you see what's different about OO-programming vs. functional programming.
I'll go section-by-section and try to help you out here.
Section 1:
// MAIN METHOD
public static void main(String[] args)
{
// SET VARIABLES FOR DIE HIGH AND LOW VALUES, NUMBER OF DICE TO ROLL
final int LOWEST_DIE_VALUE = 1;
final int HIGHEST_DIE_VALUE = 6;
final int DICE_TO_ROLL = 5;
// ROLL A SINGLE DIE VIA METHOD rollADie()
int roll = rollADie(HIGHEST_DIE_VALUE,LOWEST_DIE_VALUE);
System.out.println("roll " + roll);
}
Ok, so you've got a main method. Good start. All Java apps need this. However:
final
key words here, re-capitalize the variable names to the camelCase
convention, and just pass them in as normal variables.lowestDieValue
followed by highestDieValue
, which makes total sense, but then you're passing them in highest first, then lowest. This feels a little weird and inconsistent, so I've flipped them around.So section 1, after these changes, should look something like:
public static void main(String[] args) {
int lowestDieValue = 1;
int highestDieValue = 6;
int diceToRoll = 5;
int roll = rollADie(lowestDieValue, highestDieValue);
System.out.println("roll " + roll);
}
Section 2:
final
variables). When you pass it into a method, they're not finals/constants.When you make these changes, section 2 should look like:
public static int rollADie(int lowestDieValue, int highestDieValue) {
return ((int)(Math.random()*100)%highestDieValue+lowestDieValue);
}
Section 3:
This method can be greatly simplified. We're basically trying to accomplish three things:
int[]
(integer array)You've got several problems in this method, including calling the return
statement in the middle of a for
loop (which is compilable, but is technically incorrect in your case as it results in your method returning early with only one value in the array).
I'm just going to completely re-write this method, including renaming/re-capitalizing your variable names according to the aforementioned conventions. The rewrite cleanly accomplishes the 3 things this method needs to do (as specified above):
public static int[] rollTheDice(int numDiceToRoll, int lowestDieValue, int highestDieValue) {
int[] allDiceRolls = new int[numDiceToRoll];
for(int i = 0; i < allDiceRolls.length; i++) {
allDiceRolls[i] = rollADie(lowestDieValue, highestDieValue);
}
return allDiceRolls;
}
Finally:
{
at the end of a line, as opposed to on their own line, but that's just a style thing. Either style is valid and means the same thing when compiled.rollTheDice
method.Upvotes: 1
Reputation: 106518
I can't look at your code since it's not exactly a specific snippet of code...it takes time to read and weave through that.
I'll attempt to address your primary concern - passing variables along. I've always thought of programming functions as math functions, in the sense that, whichever value I pass to the method is used.
As a for-instance: I have a method f(x) = 2x2-3x+7. x is clearly bound to the scope of this function.
If I have a variable y = 17, and I call my method with x = y, then the value 17 is used in place of x inside of f(x).
For Java, it's not that much different. You pass a value (with type as described in the API or specified by yourself), then expect a result (or void, if the return type is void). To take an example from your code...
public static int rollADie(int HIGHEST_DIE_VALUE,int LOWEST_DIE_VALUE)
{
int roll;
roll = ((int)(Math.random()*100)%HIGHEST_DIE_VALUE+LOWEST_DIE_VALUE);
return roll;
}
...if I were to call this method with two int variables var1=15
and var2=17
it would look like this.
int myRoll = rollADie(var1, var2);
What we do when we pass variables to functions is accept that the variables that we are using in the signature (in this example, HIGHEST_DIE_VALUE
and LOWEST_DIE_VALUE
) are bound specific to the scope of rollADie. This means that those two variables have no significance outside of the method, save for their type.
Upvotes: 1
Reputation: 519
This is an excellent place to get started learning about how java classes and methods work when coming from another language. Specifically it appears that some object oriented concepts are eluding you in which case I'd recommend reading these.
Looking at your code you call rollADie correctly here:
int roll = rollADie(HIGHEST_DIE_VALUE,LOWEST_DIE_VALUE);
However down here you try instantiating it like a class which won't work:
for(rollNum=1;rollNum<=DICE_TO_ROLL;rollNum++)
{
int[] rolledDie = new rollADie(HIGHEST_DIE_VALUE,LOWEST_DIE_VALUE)
{
rolledDie
};
return rolledDie[];
}
Try replacing this second block with this:
int[] rolledDiceValues;
for(rollNum=1;rollNum<=DICE_TO_ROLL;rollNum++)
{
rolledDiceValues[rollNum] = rollADie(HIGHEST_DIE_VALUE,LOWEST_DIE_VALUE);
}
return rolledDiceValues;
You have to wait for the for loop to complete iterating before you can return the array filled with the completed rolls of dice.
Upvotes: 1
Reputation: 122536
int newVariable = return methodFromWhichDataIsReturned(fieldReturnedFromMethod);
The keyword return
cannot be placed there. It indicates that you wish to exit the method that is currently executing so it can only be placed at the beginning of a line, such as return 3;
When you're calling a method that returns a value, there's no need to place return
in front. In other words, it should be:
int newVariable = return methodFromWhichDataIsReturned(fieldReturnedFromMethod);
The method rollTheDice should more likely be written as:
int rollNum;
int[] rolledDie = new int[DICE_TO_ROLL];
for (rollNum = 1; rollNum <= DICE_TO_ROLL; rollNum++) {
rolledDie[rollNum - 1] = rollADie(HIGHEST_DIE_VALUE, LOWEST_DIE_VALUE);
}
return rolledDie;
In the second line above, we're creating a new array with DICE_TO_ROll
number of elements. This array has integers and each integer will be 0 by default. We're now going to roll a die and each time rollADie has computed a value, we'll store it in rollADie
at position rollNum - 1
(as arrays start at 0, so the first element is at rolledDie[0]
). In the last line, we're returning the array from this method.
Lastly, in various languages it is a convention to write the names of constants in capitals and the names of regular variables otherwise. For example, the following line:
public static int rollADie(int HIGHEST_DIE_VALUE,int LOWEST_DIE_VALUE)
would commonly be written as:
public static int rollADie(int highestDieValue, int lowestDieValue)
or:
public static int rollADie(int highest_die_value, int lowest_die_value)
Upvotes: 1
Reputation: 21111
Have you tried reading up on java for instance looking at some of the tutorials available:
http://docs.oracle.com/javase/tutorial/java/index.html
Upvotes: 0
Reputation: 688
Functions have a return type, that is what they return. They can only have one return. Once the function returns its execution ends.
public int foo()
{
return 1;
}
public int bar()
{
return 1;
// No code down here will execute
}
To set a value, say of type int myInt:
int myInt = foo(); // There is no reason to return here. If a function has a return it will return it regardless.
Upvotes: 0