In week 5 you were asked to create a \'Die\' class. This class simulated the rol
ID: 3745654 • Letter: I
Question
In week 5 you were asked to create a 'Die' class. This class simulated the rolling of a single die, which can have a variable number of sides, and the number of sides is passed to the die on construction.
Your task this week is to create a 'Dice' class which contains a collection of 'Die' as a private variable and simulates the rolling of multiple dice.
You should include your 'Die' class in the code, in the same namespace as the 'Dice' class, and make a private variable to store an array of these 'Die' in the 'Dice' class.
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), and the same default face value (1).
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, and the same default face value as the 'Die' class (1). 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.
public int GetFaceValue()
This method must return the total face value of all the dice (e.g. if there was 2 dice with face values 3 and 6, this should return 9)
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. Your Dice class will be instantiated and its methods called by the AMS in order to test it.
If you still have difficulties, here are some tips when writing programs for AMS:
Do not write your program directly into the text box. You should copy and paste the sample code into Visual Studio, write your program there, run it and test it to make sure it works, then paste it into AMS. You only get a limited number of attempts at each AMS exercise, so make sure they count.
Sample code:
private int numFaces = 6;
private int faceValue = 1;
private static Random random = new Random();
/// <summary>
/// Represents one die (singular of dice) with faces showing values between
/// 1 and the number of faces on the die.
/// </summary>
public Die()
{
this.numFaces = 6;
this.faceValue = 1;
}
public Die(int faces)
{
if (faces >= 3)
{
this.numFaces = faces;
this.faceValue = 1;
}
else
{
this.numFaces = 6;
this.faceValue = 1;
}
}
/*This method must roll the die and store the value resulting from the roll internally
- i.e. the new face value. Hint - the Random class will be helpful here*/
public void RollDie()
{
faceValue = 1 + random.Next(numFaces);
}
//This method should be used to get the current face value of the die
public int GetFaceValue()
{
return faceValue;
}
//This method should be used to get the number of faces of the die.
public int GetNumFaces()
{
return numFaces;
}
}//End Die class
Explanation / Answer
please do upvote.If you have any problem do comment and i shall be happy to help you.Thanks for using chegg.
------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DiceRoller
{
public class Die
{
private int numFaces = 6;
private int faceValue = 1;
private static Random random = new Random();
/// <summary>
/// Represents one die (singular of dice) with faces showing values between
/// 1 and the number of faces on the die.
/// </summary>
public Die()
{
this.numFaces = 6;
this.faceValue = 1;
}
public Die(int faces)
{
if (faces >= 3)
{
this.numFaces = faces;
this.faceValue = 1;
}
else
{
this.numFaces = 6;
this.faceValue = 1;
}
}
/*This method must roll the die and store the value resulting from the roll internally
- i.e. the new face value. Hint - the Random class will be helpful here*/
public void RollDie()
{
faceValue = 1 + random.Next(numFaces);
}
//This method should be used to get the current face value of the die
public int GetFaceValue()
{
return faceValue;
}
//This method should be used to get the number of faces of the die.
public int GetNumFaces()
{
return numFaces;
}
}//End Die class
public class Dice
{
// Implement your 'Dice' class here
// ...
private Die[] d;
public Dice(int dice)
{
d = new Die [dice];
//iterate through the whole array and create and store a new object
for (int i = 0; i < d.Count(); i++)
{
d[i] = new Die();
}
}
public Dice(int dice, int faces)
{
d = new Die[dice];
//iterate through the whole array and create and store a new object
for (int i = 0; i < d.Count(); i++)
{
d[i] = new Die(faces);
}
}
//rolls all die in array d
public void RollDice()
{
//iterate through the whole array and roll each die
for (int i = 0; i < d.Count(); i++)
{
d[i].RollDie();
}
}
//return sum of face values
public int GetFaceValue()
{
int totalFaceValue = 0;
//iterate through the whole array and add face values
for (int i = 0; i < d.Count(); i++)
{
totalFaceValue = totalFaceValue + d[i].GetFaceValue();
}
return totalFaceValue;
}
public static void Main()
{
//testing class
Dice myDice1 = new Dice(1);
Dice myDice2 = new Dice(2, 4);
//displaying default facevalues
Console.WriteLine(" default face values ");
Console.WriteLine("myDice1 has face Value: {0}", myDice1.GetFaceValue());
Console.WriteLine("myDice2 has face Value: {0}", myDice2.GetFaceValue());
myDice1.RollDice();
myDice2.RollDice();
//displaying new facevalues after rolling
Console.WriteLine(" face values after rolling ");
Console.WriteLine("myDice1 has face Value: {0}", myDice1.GetFaceValue());
Console.WriteLine("myDice2 has face Value: {0}", myDice2.GetFaceValue());
}
}// end class Dice
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.