Below is my java programming assignment we are working on classes with private and public members and I am suppose to make a dice class to go along with another couple classes that make a pokemon game. My coding is below the assignment details along with where I am getting an error. My professor said that the actual dice is to be created in a class that isn't this one this class is just to get the rolling of the dice. Dice class -private members: -int numSides The number of sides of the die public methods Dice(int sides) -Creates a die with numSides set to the number of sides passed in -e.g. Dice d6 = new Dice(6); //creates a six-sided die int roll() -Returns a random integer from 1 up to and including numSides ---------------------------------------... import java.util.Random; public class Dice { private int numSides; int Sides; public Dice(int sides) { numSides=Sides; Random myRand= new Random(); } public int roll() { return(myRand.nextInt(numSides+1));<... error right here at the myRand } }
You don't have a range set for the die. If the die has six sides the program needs to know that it is selecting a random number within the range of six. Also, an enhanced for loop would be better than recursion for something like this. Example: An int range set to 6 and a new Random object named randy. For int index = 0; index < range; indexx++ int roll = rest is up to you. Not being rude by not typing in the full answer, I just don't think it helps to throw the whole thing out there when this is an assignment for schoolwork.