Question

In: Computer Science

C# programming Create a classes called AlphabetDice that simulates a multi-sided dices with alphabet characters on...

C# programming

Create a classes called AlphabetDice that simulates a multi-sided dices with alphabet characters on each side. For instance, a 10-sided AlphabetDice will random return 'a','b','c','d','e','f','g','h','i' or 'j'.

The custom constructor should require a random seed and the number of sides.

It should also have the following:

  • A read-only property Sides that give us the number of sides
  • A method char Next() that returns an appropriate random character (char)

On a side note, to cast an integer to a char, use (char). e.g.

int x = 66;

char c = (char) x;

You can use the following in main to test your class:

AlphabetDice d = new AlphabetDice(999,4); // 999 is the seed

         

Console.WriteLine(d.Sides);

for(int i=0;i<100;i++)

     Console.WriteLine(d.Next());

If your class is correct, adding the following line to the end of main should cause a compilation error:

d.Sides = 100;

Copy and paste the complete program here;

Solutions

Expert Solution

-----------------------------I used Visual Studio 2013, C# Language, Console Application-------------------

First we need to keep in mind that the Dice comes with 4,6,8,10,12 or 20 sides, If we are giving other side value means no meaning in that. For example  if we are giving side = 7, we can make the program work but in real life there is no dice that comes with 7 sides.

If we try to set value to a read only property (which contains only get accessor), we get compilation error.
If we need to set values then we need to write set accessor or another way is to create private variable and then return the private variable in read only property.(See below image :: read only property Sides return private variable _sides). We can give value to the private variable _sides in constructor.

--------------------------OUTPUT------------------

------------------------AlphabetDice.cs----------------

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

namespace AlphaDiceProgram
{
class AlphabetDice
{
Random random;
//Below boolean success is used to tell to main method that sides are valid,
//For ex: If parameterized constructor passed side = 5, then success is set to false.
bool success;
int _sides;
//Default constructor
public AlphabetDice()
{
success = false;
_sides = 0;
}
//parameterized constructor with parameter seed and side
public AlphabetDice(int seed, int side)
{
//Dice side is between even number 4 and 12
//For ex: if side passed is 7, then if condition is false;
if (side <= 12 && side >= 4 && (side % 2 == 0))
{
//If side is 4,6,8,10,12 then new random seed is passed ,
//side value is given to private variable _sides,
//Set success to true to tell to main method entered side is valid.
random = new Random(seed);
_sides = side;
success = true;
}
//If side is 20
else if (side == 20)
{
random = new Random(seed);
_sides = side;
success = true;
}
//If side is other than 4,6,8,10,12,20 then success set to false
else
{
success = false;
}

}
char Next()
{
int min = 0;
int max = Sides;
//If side entered is 4, then min = 0 and max = 4
//In random.Next(0, 4) will return 4 digits - 0,1,2,3
int randomNumber = random.Next(min, max);
//To start with small letter 'a', ASCII start with 97
//97 = a, 98 = b, 99 = c ....
char ch = (char)(97 + randomNumber);
return ch;
}

public int Sides
{
get
{
return _sides;
}
}

static void Main(string[] args)
{
Console.Write("Enter Seed : ");
int seed = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter Sides(4,6,8,10,12,20) : ");
int sides = Convert.ToInt32(Console.ReadLine());

AlphabetDice AD = new AlphabetDice(seed, sides);
AD.Sides = 100;
if (AD.success)
{
Console.WriteLine(AD.Sides);
for (int i = 0; i < 100; i++)
{
Console.WriteLine(AD.Next());
}
}
else
{
Console.WriteLine("Sorry, Dice comes with 4,6,8,10,12 or 20 sides only!!!");
}
Console.ReadLine();
}
}
}


Related Solutions

C++ Programming. Create a class hierarchy to be used in a university setting. The classes are...
C++ Programming. Create a class hierarchy to be used in a university setting. The classes are as follows: The base class isPerson. The class should have 3 data members: personID (integer), firstName(string), and lastName(string). The classshould also have a static data member called nextID which is used to assignanID numbertoeach object created(personID). All data members must be private. Create the following member functions: Accessor functions to allow access to first name and last name (updates and retrieval). These functions should...
C++ HW Aim of the assignment is to write classes. Create a class called Student. This...
C++ HW Aim of the assignment is to write classes. Create a class called Student. This class should contain information of a single student. last name, first name, credits, gpa, date of birth, matriculation date, ** you need accessor and mutator functions. You need a constructor that initializes a student by accepting all parameters. You need a default constructor that initializes everything to default values. write the entire program.
*OBJECT ORIENTED PROGRAMMING* *JAVA PROGRAMMING* Create a program that simulates a race between several vehicles. Details...
*OBJECT ORIENTED PROGRAMMING* *JAVA PROGRAMMING* Create a program that simulates a race between several vehicles. Details don't matter code must just have the following: Design and implement an inheritance hierarchy that includes Vehicle as an abstract superclass and several subclasses. Include a document containing a UML diagram describing your inheritance hierarchy. Include at least one interface that contains at least one method that implementing classes must implement. Include functionality to write the results of the race to a file; this...
Project 1 - OO Programming Create a program that simulates cars racing and allows users to...
Project 1 - OO Programming Create a program that simulates cars racing and allows users to bet on the races ( start users with $100 ) In main, prompt the user to enter details needed to create 2 instances of the Car class you create. Ask the user to bet on which car will win the race ( use the overridden << method to display car details ), ensure the bet is between 1 and the amount of money the...
In C++ Create a class that simulates a school calendar for a course and has a...
In C++ Create a class that simulates a school calendar for a course and has a warner that provides the school administration with the option of warning students when the last day is that a student is permitted to drop the course. (Allow administrators to warn or not, as they wish. Do not make this a required function.) You will assume in this calendar that there are 12 months in a year, 4 weeks in a month, and 7 days...
C# Programming create a Hash Function
C# Programming create a Hash Function
Write a program in C++ called RollDice.cpp that simulates rolling a pair of dice until the...
Write a program in C++ called RollDice.cpp that simulates rolling a pair of dice until the total on the dice comes up to be a given number. Ask the user for the number that you are rolling for. To have your program roll two dice, use the rand() function this way: die1 = rand() % 6 + 1; die2 = rand() % 6 + 1; Your program then computes and prints the number of rolls it takes to get the...
C# Programming Discuss how to build a Multi-Form Windows application in C#. That is an App...
C# Programming Discuss how to build a Multi-Form Windows application in C#. That is an App that has more than one form, like say maybe 5 forms. How do you connect these forms and pass data objects between these forms?
C# programming Create a class called A with private integer field x, protected integer field y,...
C# programming Create a class called A with private integer field x, protected integer field y, public integer field z. Create a class B derived from class A with public integer field d and protected integer field e and private field f. Write a main (in a THIRD class called Program) that create an object B and assign all publicly accessible fields of the object with value of 1. Which fields will have a value of 1? Create a method...
C# programming Create a class called A with private integer field x, protected integer field y,...
C# programming Create a class called A with private integer field x, protected integer field y, public integer field z. Create a class B derived from class A with public integer field d and protected integer field e and private field f. Write a main (in a THIRD class called Program) that create an object B and assign all publicly accessible fields of the object with value of 1. Which fields will have a value of 1? Create a method...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT