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++ 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.
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
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?
Instructions (In C++ Please) Verification This programming lab makes use of an array of characters. The...
Instructions (In C++ Please) Verification This programming lab makes use of an array of characters. The program will validate the characters in the array to see if they meets a set of requirements. Requirements: Must be at least 6 characters long (but not limited to 6, can be greater) Contains one upper case letter Contains one lower case letter Contains one digit Complete the code attached. Must use pointer while checking the characters. Download Source Lab 3 File: #include using...
Create a program in C that counts the number of characters in a word when a...
Create a program in C that counts the number of characters in a word when a user inputs a string. Use stdin to read an input string. For example, if a user inputs: “The dog is good” the output should be a= [The], b=3 a= [dog], b=3 a= [ is], b=2 a= [good], b=4 a= [ ], b=0000 Take into account EOF. If an EOF is reached at the end of the string then the output should be 0000. (example...
Create a program in C that counts the number of characters in a word when a...
Create a program in C that counts the number of characters in a word when a user inputs a string. Use stdin to read an input string. For example, if a user inputs: “The dog is good” the output should be a= [The], b=3 a= [dog], b=3 a= [ is], b=2 a= [good], b=4 a= [ ], b=0000 Take into account EOF. If an EOF is reached at the end of the string then the output should be 0000. (example...
Name: __________________________________________ Specifications: Create a class called WordMaker that is passed characters one at a time...
Name: __________________________________________ Specifications: Create a class called WordMaker that is passed characters one at a time and assembles them into words. As long as the characters passed to it are letters, it builds a word by placing each character on the end of a string. When a character that is not part of a word (i.e., is not a letter) is encountered, the class signals that the word is complete, and makes a string containing the word available to its...
Create a C++ code for the mastermind game using classes(private classes and public classes). Using this...
Create a C++ code for the mastermind game using classes(private classes and public classes). Using this uml as a reference.
Homework 3 – Programming with C++ What This Assignment Is About: • Classes and Objects •...
Homework 3 – Programming with C++ What This Assignment Is About: • Classes and Objects • Methods • Arrays of Primitive Values • Arrays of Objects • Recursion • for and if Statements • Insertion Sort 2. Use the following Guidelines • Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc). • User upper case for constants. Use title case (first letter is upper case) for classes. Use lower case with uppercase word separators for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT