I am working on coding a program to roll dice. I am very new to java still as I am taking classes for schooling. I am using multiple classes in different packages for this program, what I am trying to figure out is, in one class, for my package pairOfDice, I have created objects in a class pairOfDice, die1 and die2. Now I have another package rollDice, adn my goal is to use the pairOfDice class to roll two die and display the rolls. what I am struggling with is how to exactly do that. When I am rolling the die my results display as if I am only rolling one die. I have made adjustments to display two die every roll, although feel as if I am not doing it in a more proficient sort of way.
package die;
import java.util.Scanner;
/**
*
* @author <a href= "mailto:[email protected]" >Aaron Davis</a>
*/
public class RollDice
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
PairOfDice dice = new PairOfDice();
// get amount of rolls from user
System.out.print("How many rolls do you want? ");
int numRolls = scan.nextInt();
int diceOne, diceTwo;
int boxCar, snakeEyes;
int j = 0, k = 0;
// rolls the dice the requested amount of times
for (int i = 0; i < numRolls; i++)
{
// first die to roll
diceOne = dice.roll();
// second die to roll
diceTwo = dice.roll();
// display rolled dice
System.out.println(diceOne + " " + diceTwo + "\n");
// store and display pairs of 1 rolls
if (diceOne == 1 && diceTwo == 1)
{
snakeEyes = ++j;
System.out.println("\nThe number of snake eyes you have is: "
+ snakeEyes + "\n");
}
// store and display pairs of 6 rolls
if (diceOne == 6 && diceTwo == 6)
{
boxCar = ++k;
System.out.println("\nThe number of box cars you have is: "
+ boxCar + "\n");
}
}
}
}
******************************************************************************
/*
the integers diceOne and diceTwo are my workarounds, my other package contains
public class PairOfDice extends Die
{
Die die1, die2;
public PairOfDice()
{
die1 = new Die();
die2 = new Die();
}
public PairOfDice(int face)
{
die1 = new Die(face);
die2 = new Die(face);
}
}
*/
******************************************************************************
// i am un-clear how to make "PairOfDice dice = new PairOfDice();" come out as two die
The
PairOfDiceclass does not represent your model, which is "a pair of dice". If you have a pair of dice, when you are rolling them, you get two different numbers, thus either:rollmethod must return two values. You can use aRollResultbean containing the two values for instancerollmethod can return just an integer from 2 to 12 and you can speculate over the dice rolling based on the sum of them: in your case it is always possible because you get a sum of 2 if and only if your dices are 1, 1; similarily, if you get a sum of 12 if and only if your dices are 6, 6. It would not work, for instance, if you would test against the condition "dice 1=3, dice2=4", as there are many combinations of rollings returning 3+4=7Hope this helps.
Based on the comments, we have to proceed with the first solution. Here it is an example that implements domain immutable objects and
rolldomain function that return the result of therollaction against a dice. Here in the example I show the possibilities of having multiple types of dices.