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[];
}
}
}
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:
Ok, so you’ve got a main method. Good start. All Java apps need this. However:
finalkey words here, re-capitalize the variable names to thecamelCaseconvention, and just pass them in as normal variables.lowestDieValuefollowed byhighestDieValue, 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:
Section 2:
finalvariables). When you pass it into a method, they’re not finals/constants.When you make these changes, section 2 should look like:
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
returnstatement in the middle of aforloop (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):
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.rollTheDicemethod.