Reputation: 9
I am Trying to make a simulated dice rolling game for my friends. I want to utilize two classes Dice and a child class VarDice which will let user choose how many sides. However, I am struggling to get my derived class to work properly.
This is my parent class :
import java.util.Random;
public class Dice {
protected int numRolled;
final Random rand = new Random();
public void roll() {
numRolled = rand.nextInt(5) + 1;
}
public int getNumRolled() {
return numRolled;
}
}
This is the derived class:
import java.util.Random;
public class VarDice extends Dice {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int numSides;
public void setNumSides(int sides) {
numSides = sides;
}
public int getNumSides() {
return numSides;
}
@Override
public void roll() {
numRolled = rand.nextInt(numSides) + 1;
}
}
}
This is finally my main code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int numSides;
VarDice dice = new VarDice();
numSides = scar.nextInt();
dice.setNumSides(numSides);
dice.roll();
System.out.println("The dice come up " + dice.getNumRolled());
}
}
I want the user to be able to input how many sides are on their dice and how many dice, but my VarDice class is not overriding the roll method very well and I am a little bit lost on the errors I am getting... I appreciate the help and hope the format is appropriate.
Upvotes: 0
Views: 321
Reputation: 709
A few mistakes I noticed:
the line numSides = scar.nextInt();
should be numSides = scnr.nextInt();
the class VarDice has wrapped everything in public static void main(String[] args) {...}
. Remove this wrapping to ensure that the code is a part of the class and not the method.
With those changes, the code worked for me.
Upvotes: 0
Reputation: 2587
There are some problems, here:
VarDice
is in a main
method that doesn't belong here.Your Die
class could look something like this:
import java.util.Random;
public class Die {
private final Random rand;
private int numSides;
private int numRolled;
public Die(int numSides) {
this.rand = new Random();
this.numRolled = -1;
// custom no. of sides, 6 if invalid
this.numSides = numSides > 0 ? numSides : 6;
}
public Die() {
this(6); // 6 is default
}
public void setNumSides(int sides) {
numSides = sides;
numRolled = -1;
}
public int getNumSides() {
return numSides;
}
public int getNumRolled() {
if (numRolled < 0) {
roll();
}
return numRolled;
}
public int roll() {
numRolled = rand.nextInt(numSides) + 1;
return numRolled;
}
}
And you'd use this class something like this in your application:
import java.util.Scanner;
public class App {
public static void main(String[] args) {
// use default die
Die die = new Die(); // create instance with default no of sides
System.out.println(die.roll()); // roll die and print result
System.out.println(die.getNumRolled()); // print rolled number again
// use custom die
Scanner scanner = new Scanner(System.in);
System.out.print("Set number of sides (greater than 0): ");
Die customDie = new Die(scanner.nextInt());
System.out.println(customDie.roll()); // roll custom die
scanner.close();
}
}
Upvotes: 1