Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Your task is to create a 'Dice' class which contains an instance of 'Die' as a private variable and simulates the rolling of multiple dice.

Your task is to create a 'Dice' class which contains an instance of 'Die' as a private variable and simulates the rolling of multiple dice.

The code for the original 'Die' class is provided as part of the sample code. You should not touch this code; instead, write your own 'Dice' class according to the specification below.

The 'Dice' class must have 2 constructors:

public Dice(int dice)

This constructor creates an instance of Dice with the specified number of dice. These dice should have the same default number of faces as the 'Die' class (6).

public Dice(int dice, int faces)

This constructor creates an instance of Dice with the specified number of dice, each with faces number of faces. As with the 'Die' class, the minimum number of faces is 3.

The class must also have two other methods, which are similar to the methods of the 'Die' class:

public void RollDice()

This method must roll the dice and store the value resulting from the roll internally.

public int GetFaceValue()

This method must return the result from the last roll of the dice.

Now, given the defaults, the code Dice myDice = new Dice(1); should create a single six-sided die, and rolling this should produce values between 1 and 6.

Dice myDice = new Dice(2, 4);, however, should create two four-sided dice. Rolling these dice should produce values between 2 and 8.

Note that no Main() function is provided. You should create your own Main() function for debugging your new class.

using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace DiceRoller { ///

/// Represents one die (singular of dice) with faces showing values between /// 1 and the number of faces on the die. /// /// Modified from Java version in Lewis/Loftus "Java Software Solutions." /// by Mike Roggenkamp March 2009 /// /// Updated XML comments: MGR April 2016 /// /// public class Die {

private const int SIX_SIDED = 6; private const int DEFAULT_FACE_VALUE = 1; private const int MIN_FACES = 3; ///

/// /// private int numFaces; //number of sides on die private int faceValue; // which side is showing private static Random randomNumber = new Random();

public Die() { numFaces = SIX_SIDED; faceValue = DEFAULT_FACE_VALUE; }

///

/// Allows user to specify the number of sides on a Die. /// If "faces" is less than 3, a six-sided die is instantiated. /// /// the numberr of sides public Die(int faces) {

if (faces >= MIN_FACES) { numFaces = faces; } else { numFaces = SIX_SIDED; }

RollDie(); }

///

/// Simulates the rolling of a Die. /// public void RollDie() { faceValue = randomNumber.Next(1, numFaces + 1); } // end RollDie

///

/// Die accessor /// /// The current face of the Die public int GetFaceValue() { return faceValue; } //end GetFaceValue

}// end Class Die

public class Dice { // Implement your 'Dice' class here // ... } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Visual Basic6 Database Programming

Authors: John W. Fronckowiak, David J. Helda

1st Edition

ISBN: 0764532545, 978-0764532542

More Books

Students also viewed these Databases questions

Question

What are the basic financial decisions ?

Answered: 1 week ago

Question

What is meant by 'Wealth Maximization ' ?

Answered: 1 week ago