ComputerScienceExpert

(11)

$18/per page/

About ComputerScienceExpert

Levels Tought:
Elementary,Middle School,High School,College,University,PHD

Expertise:
Applied Sciences,Calculus See all
Applied Sciences,Calculus,Chemistry,Computer Science,Environmental science,Information Systems,Science Hide all
Teaching Since: Apr 2017
Last Sign in: 103 Weeks Ago, 3 Days Ago
Questions Answered: 4870
Tutorials Posted: 4863

Education

  • MBA IT, Mater in Science and Technology
    Devry
    Jul-1996 - Jul-2000

Experience

  • Professor
    Devry University
    Mar-2010 - Oct-2016

Category > Programming Posted 03 May 2017 My Price 8.00

Building a complex loop, or nested loop

C# program using enums, files attached.

....................

 

 

1) Building a complex loop, or nested loop.
2) Creating, using, and setting properties which are not your normal sting or int
properties, they are properties of a new data types that you define using enum.
(Please use all the same names for classes, methods, and variables that I do, otherwise it
becomes hard to follow along with me in class, and for me to grade your homework.)
A. Start with the Visual Studio project I provided. It has a PokerProject console app, and a Class
Library, called CardLibrary.
B. In the CardLibrary is already defined a Super (Parent) class SuperCard that will be used to create
the 4 child classes, one for each card suit.
C. We will be using 2 enumerations, enums, to define both Suit and Rank. They are already defined
inside of this same file containing the SuperCard class. D. Edit the SuperCard class:
a. Change the class definition to be public abstract (why abstract? We’ll cover this soon.)
b. Create a new public get set property called CardRank of type Rank
c. Create a new public abstract property called CardSuit of type Suit (note the use of
abstract for this property, which means we MUST provide an override version for this
property in any subclass that uses this SuperClass.
d. Note that CardRank is not abstract, so the child classes will use the parents, no
overriding is required.
e. (There is no constructor since this is an abstract class, and it cannot be instantiated.)
E. Now code the 4 child classes in the same CardLibrary. Each card class will have effectively 2
properties, one to describe its suit and one for its rank. So:
a. create 4 new classes, one each called CardClub, CardDiamond, CardHeart, and
CardSpade.
b. For each one, define them to inherit the SuperCard class.
c. In each one, define a field of type Suit for each, such as:
private Suit _CardSuit = Suit. Club; (with the other 3 having appropriate names). Note
this a property, but not an int property, nor a string property, this is a property of type Suit, one of
our 2 enums.
d. Code the required public override property called CardSuit, which returns the value of
_CardSuit
e. Define a public constructor in each of the 4 child classes, all the constructor needs to do
is accept an input parameter of type Rank, and then use that param to set the property
CardRank = to that passed in value. (Note this child class does not have a CardRank field
defined, but it does have one inherited from its parent)
f. After you complete this, we will be able to create (instantiate) child card objects (any
one of 4 suit types) which determines the CardSuit, and where the constructor will set the CardRank to be one of the 13 legal enum values. So with these 4 child classes, our
code will be able to make 52 unique cards (4 suits x 13 Ranks)
F. Make sure all these classes and the 2 enums are declared as public.
G. Now create another public class in the CardLibray, call it CardSet As in, the set of all cards. It
will hold a “deck of 52 cards”, and eventually provide various methods for manipulating our deck
of cards. (In card games, the set of all cards is usually called the deck.)
a. Define a public field (normally they are private!), called cardArray, which is a 1
dimensional array of type SuperCard (This cardArray will be what our console program
will think of as the deck of cards.) Just define the variable type and name, do not use an
= sign and actually create the array yet.
b. Define a constructor for this Class that takes in no parameters. In this constructor, first
finish the definition of that cardArray Set it to be of size 52.
c. and then also in this constructor:
i. Create a loop (or nested loops) which fill the array of 52 elements of this new
array with one of each card type. That’s 13 spades from deuce to Ace, 13
hearts, etc, so that cardArray has all unique 52 cards. It is fine if they are in
perfect order, the array does not have to be shuffled. There are many way to fill
in this array of 52, but I will not accept a brute force list of 52 lines of array
assignments. You must use loops of some kind. Note carefully, you will be
inserting 13 objects of type CardClub, and 13 objects of type CardHeart, etc, not
52 objects of type SuperCard. You will never instantiate a SuperCard (esp. since
it’s an abstract class), it is just common code used by the 4 children. We are
using inheritance here.
ii. HINT: While there are many ways of doing this, most of them will require you to
cast some kind of a loop counter into an enum value, something like this:
cardArray[index1] = new cardClub((rank)(index2));
where index1 and index2 are various loop counters, say with values at this
moment of 37 and 6 H. Now verify you did all that correctly. Open your Program.cs in the CardGameConsole project.
a. Create an instance of your CardSet class, and call it myDeck.
b. Build a loop that does a Console.WriteLine 52 times. Have it write out this:
Console.WriteLine(myDeck.cardArray[i].CardRank + " of " + myDeck.cardArray[i].CardSuit); If you enlarge your output window, it should look something like this: The cards may be in a different order, depending on how you built your loop inside the constructor.
HINT: You need a loop that calls each of the 4 child class constructors 13 times (good!), or 4 loops that
call one of the child class constructors 13 times (ok). But be careful in the parameter you pass into the
children’s constructors, it’s not quite as simple as passing the loop counter ( i) since the cardArray goes
from 0 to 51, but each Suit enum starts at 2 not 0, and only goes to 14. Also, you are filling ONE array
with 4 different suits, so your pointer to your array that you are loading needs to somehow traverse
from 0 to 51, first filling in 13 of one suit, then 13 of another, then 13 of another, and finally 13 of the last
suit.
=================================================

Attachments:

Answers

(11)
Status NEW Posted 03 May 2017 04:05 AM My Price 8.00

-----------

Not Rated(0)