Redo the Lucky Sevens dice-playing program so that it uses dice objects. That is, design and implement a Dice class. Each instance of this class should contain the die's current side. There should be an accessor method for a die's current value. The method roll is the only mutator method. Be sure to test the Dice class in a simple tester program before incorporating it into the application.

All right, so I did the Lucky Sevens project a while ago and I did it really well and the teacher said I did a good job figuring it out and adding informative comments. Now, we're doing classes and I am having a hard time figuring out how to make a class for this program. I visited it some websites for instructions and I made one and it doesn't seem to be working. Actually, I am not too sure if I know what it's supposed to do based on the directions. Here is what I have. Any help is greatly appreciated with the Dice class.

LUCKY SEVENS:

``````package LuckySevens;

/*DIRECTIONS:
Simulate the game of lucky sevens until all funds are depleted.
1) Rules:
roll two dice
if the sum equals 7, win \$4, else lose \$1
2) The inputs are:
the amount of money the user is prepared to lose
3) Computations:
use the random number generator to simulate rolling the dice
loop until the funds are depleted
count the number of rolls
keep track of the maximum amount
4) The outputs are:
the number of rolls it takes to deplete the funds
the maximum amount
*/

import java.util.Scanner;
import java.util.Random;

public class CS1LuckySevens {
public static void main (String [] args) {

Scanner reader = new Scanner(System.in);
Random generator = new Random();

int die1, die2,       // two dice.
dollars,          // initial number of dollars (input).
count,            // number of rolls to reach depletion.
maxDollars,       // maximum amount held by the gambler.
countAtMax;       // count when the maximum is achieved.

// Request the input:
System.out.print("How many dollars do you have? ");
dollars = reader.nextInt();

// Initialize variables:
maxDollars = dollars;
countAtMax = 0;
count = 0;

// Loop until the money is gone.
while (dollars > 0){
count++;

// Roll the dice.
die1 = generator.nextInt (6) + 1; // 1-6
die2 = generator.nextInt (6) + 1; // 1-6

// Calculate the winnings or loses.
if (die1 + die2 == 7)
dollars += 4;
else
dollars -= 1;

// If this is a new maximum, remember it!!
if (dollars > maxDollars){
maxDollars = dollars;
countAtMax = count;
}
}

// Display the results
System.out.println
("You are broke after " + count + " rolls.\n" +
"You should have quit after " + countAtMax +
" rolls when you had \$" + maxDollars + ".");
}
}``````

ATTEMPTED DICE CLASS:

``````/*Redo the Lucky Sevens dice-playing program so that it uses dice objects. That is, design and implement a Dice class. Each instance of this class should contain the die's current side. There should be an accessor method for a die's current value. The method roll is the only mutator method. Be sure to test the Dice class in a simple tester program before incorporating it into the application.*/
package LuckySevens;

import java.util.Random;

public class Dice {
private int diceLength;
private int[] rolls;
private int[] sides;
private int numRolls = 0;
public Dice (int nDice) {
diceLength = nDice;
rolls = new int[nDice];
sides = new int[nDice];
}

void addResult(int val, int sides) {
rolls[numRolls] = val;
this.sides[numRolls++] = sides;
}

/**
* Assumes the dice have been rolled.
* @param d
*/
void addResult(Die... d) {
for (int i = 0; i < d.length; i++) {
addResult(d[i].getLastRoll(), d[i].getSides());
}
}

public int getRoll(int i) {
if (i < 0 || i >= numRolls) throw new IllegalArgumentException("No such die!");
return rolls[i];
}

public int getSides(int i) {
if (i < 0 || i >= numRolls) throw new IllegalArgumentException("No such die!");
return sides[i];
}

public int getSum() {
int sum = 0;
for (int i = 0; i < numRolls; i++) {
sum += rolls[i];
}
return sum;
}

public int getDiceLength() {
return diceLength;
}

public int getNumRolls() {
return numRolls;
}
}``````

## Recommended Answers

Reguarding to you decription you may start as follow:

``````class Dice{
// Each instance of this class should contain the die's current side.
int curretSide;
//should be an accessor method for a die's current value
public int getCurretSide(){
return curretSide;
}
// The method roll
public void …``````

## All 2 Replies

Reguarding to you decription you may start as follow:

``````class Dice{
// Each instance of this class should contain the die's current side.
int curretSide;
//should be an accessor method for a die's current value
public int getCurretSide(){
return curretSide;
}
// The method roll
public void roll(){
//doSOmething
}
}``````

and try to add more complicate things.

I believe he asked for you to set the face value and heres a simple way to do it.

``````private final int MAX = 6;  // highest possible dice face value
private int faceValue;  // current value shown on the dice face
/**
Constructor: Sets the initial face value.
*/
public Dice()
{ faceValue = 1;
}``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.