using System; using System.Collections.Generic; using System.Linq; using System.
ID: 3732682 • Letter: U
Question
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DieRoller {
/// <summary>
/// Represents one die (singular of dice) with faces showing values between
/// 1 and the number of faces on the die.
/// </summary>
public class Die {
// Implement your 'Die' class here
}// end Class Die
public class Program {
public static void Main() {
// You may want to test your Die class here.
Die myDie = new Die();
}
}
}
Explanation / Answer
Have a look at the C# code below. I have added comments for better understanding.If you still have any doubt please feel free to ask.
Code:
using System;
class Die {
private int face_value, faces;
// Default constructor
public Die() {
this.faces = 6;
this.face_value = 1;
}
// Constructor with faces
public Die(int faces) {
// If faces are less than 3
if (faces < 3) {
this.faces = 6;
this.face_value = 1;
} else {
this.faces = faces;
this.face_value = 1;
}
}
public void rollDie() {
Random r = new Random();
this.face_value = r.Next(1, this.faces);
}
// Get the current face value
public int getFaceValue() {
return this.face_value;
}
// Get the total number of faces
public int getFaces() {
return this.faces;
}
public static void Main() {
Die d1 = new Die();
Console.WriteLine("Default Die is created with " + d1.getFaces() + " faces and " + d1.getFaceValue() + " facevalue");
d1.rollDie();
Console.WriteLine("Die is rolled and " + d1.getFaceValue() + " is current facevalue");
Die d2 = new Die(9);
Console.WriteLine("Die is created with " + d2.getFaces() + " faces and " + d2.getFaceValue() + " facevalue");
d2.rollDie();
Console.WriteLine("Die is rolled and " + d2.getFaceValue() + " is current facevalue");
}
}
Output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.