Can't figure out how to roll individual dice.
Alright so I am a beginner when it comes to java. I was working on a project in class and I eventually got stumped.
So here is what I want to do. I need to figure out how to roll the three dice individually in the dice object.
Here is my code for the dice class.
Code :
public class Dice //class header
{
//instance variables
private int die1;
private int die2;
private int die3;
/**
* Default Constructor
* Initialize the dice to 0's to indicate that they haven't been rolled yet
*
*/
public Dice ()
{
die1 = 0;
die2= 0;
die3 = 0;
}
/**
* getDie1 method
* Return the value stored in the instance variable die1
* @return the face value of die1
*/
public int getDie1()
{
return die1;
}
/**
* getDie2 method
* Return the value stored in the instance variable die2
* @return the face value of die2
*/
public int getDie2()
{
return die2;
}
/**
* getDie3 method
* Return the value stored in the instance variable die3
* @return the face value of die3
*/
public int getDie3()
{
return die3;
}
/**
* roll method
* Each instance variable gets a random number in the range 1 to 6
*/
public void roll()
{
die1 = (int)(Math.random()*6+1);
die2 = (int)(Math.random()*6+1);
die3 = (int)(Math.random()*6+1);
}
/**
*calcTotalRoll method - this method will calculate the total of the three die
*@return the sum of the dice
*/
public int calcTotalRoll()
{
return die1 + die2 + die3;
}
/**
*threeOfAKind method - this method will determine if all three of the dice have the same value
*@return true if they have the same, false otherwise
*/
public String threeOfAKind()
{
String aKind = new String("Three of a kind");
String notOfAKind = new String("Not three of a kind");
if ((die1==die2)&&(die2==die3)==true)
return aKind;
else
return notOfAKind;
}
/**
* findHighestDie method - this method will determine which of the three die have the highest die
* @return the value of the highest die
*/
public int findHighestDie()
{
if ((die1 > die2)&&(die1 > die3))
return die1;
else
if (die2 > die3)
return die2;
else
return die3;
}
public void rollOneDie()
{
}
/**
* toString method
* Create a String with the values of the three dice.
* @return a reference to a String with the state of the object
*/
public String toString()
{
String str = new String(die1 + " " + die2 + " " + die3);
return str;
}
}
and here is my lab class
Code :
public class Lab5App //class header
{
public static void main(String[] args) //main method header
{
//Instantiated a Dice object
Dice theDice = new Dice();
// Testing the constructor and the toString
System.out.println("After instantiation: " + theDice.toString() + "\n");
//Tested the accessor method
System.out.println("Value of die1: " + theDice.getDie1());
System.out.println("Value of die1: " + theDice.getDie2());
System.out.println("Value of die1: " + theDice.getDie3() + "\n");
//Roll the dice
theDice.roll();
//Display the dice after being rolled
System.out.println("After rolling the dice: " + theDice.toString() + "\n");
//Display sum total of die face values
System.out.println("The roll total is: " + theDice.calcTotalRoll() + "\n");
//Displays whether the dies have alike face values
System.out.println(theDice.threeOfAKind() + "\n");
//Displays the highest value between the three die face values
System.out.println("The highest value of the three die is " + theDice.findHighestDie() + "\n");
//Rolls individual die's
}
}
As you can see. Whenever you roll a dice object, three different dice are rolled.
The output she is expecting for the rollOneDice method is something like this: Rolling die 1 only: 1 2 5
Rolling die 2 only: 4 6 3
Rolling die 3 only: 5 5 2
So each die is rolled individually three times I guess.
I believe we are suppose to use if statements and toString but I am having trouble seeing where to start.
She gave an example on the board which was like this:
If ()
die1 = ()()
else
if ()
die2 = ....
and she also wrote something like this on the board theDice.rollOneDice(2);
Please help give me some sort of extra help on what to do.
Thanks, I appreciate it.
Re: Can't figure out how to roll individual dice.
Looks like your instructor wants your rollOneDie() method to take an int parameter. Right now it doesn't.
Then it looks like you're supposed to use that parameter to figure out which die should be rolled.
Re: Can't figure out how to roll individual dice.
Thanks! I figured it out.