/* * @topic T00837 Using DicePair with rollSeries() Nov 10 2015 * @brief DicePair class with rollSeries() method */ package diceroll; public class DicePair { // data fields private Die dieOne; private Die dieTwo; int[] totalStats; // constructors: // construct dice with equal number of sides public DicePair(int dieSideCount) { dieOne = new Die( dieSideCount ); dieTwo = new Die( dieSideCount ); totalStats = new int[ 1 + dieSideCount * 2 ]; // create an array of ints having enough space // for a range of dice combinations between // 2 and dieSideCount*2 (e.g. 2 through 12) // plus 1 element which is unused index [0] } // construct dice with unequal number of sides public DicePair(int numberOfSides, int numberOfSides2) { dieOne = new Die(numberOfSides); dieTwo = new Die(numberOfSides2); } // operations public void rollSeries( int numberOfRolls ) { while ( numberOfRolls-- > 0 ) { int total = roll(); ++totalStats[ total ]; } } public void displayTotalStats() { for ( int total = 2; total <= (dieOne.getSideCount()+dieTwo.getSideCount()); ++total ) { System.out.println( total + " rolled " + totalStats[ total ] + " times" ); } } public void resetStats() { for ( int total = 2; total <= (dieOne.getSideCount()+dieTwo.getSideCount()); ++total ) { totalStats[ total ] = 0; } } // generate new combination public int roll() { int total = dieOne.roll() + dieTwo.roll(); return total; } // obtain reference to a particular die object public Die getDieObject( int index ) { if ( index == 1 ) { return dieOne; } // TODO -- validate that index is 2. // If not valid -- report application logic error // and throw an exception. return dieTwo; } }//class DicePair