mplement and use the methods for a class called Card. Card class The Card class
ID: 3766039 • Letter: M
Question
mplement and use the methods for a class called Card.
Card class
The Card class is used to represent a single playing card.
Data Members
The class contains two data members.
An integer that holds the value of the face of the card
A character variable that holds the suit of the card.
The range of possible values for the integer data member (the face of the card) is 1 through 13 inclusive, with 1 representing an Ace, 11 representing a Jack, 12 representing a Queen, and 13 representing a King. All of the other values represent that specific card.
The possible values for the character data member are: 'C' to represent clubs, 'D' to represent diamonds, 'H' to represent hearts, and 'S' to represent spades.
Constructor
This class has one constructor, a default constructor (ie. one that takes no arguments). It should create a random card by using the random number generator.
For the face value (the integer), generate a random number between 1 and 13, inclusive.
For the suit value (the character), generate a random number between 1 and 4, inclusive. If the random number is 1, assign 'C' to the character data member. If the random number is 2, assign 'D' to the character data member. If the random number is 3, assign 'H' to the character data member. If the random number is 4, assign 'S' to the character data member.
Note: DO NOT set the seed value for the random number generator in the constructor. It has already been done in main.
Methods
void setCard( int newFace, char newSuit )
This method sets both the face and the suit for the Card object. It takes two arguments: an integer that represents the new face value for the card and a character that represents the new suit value for the card. It returns nothing.
No error checking is required. Simply take the passed-in values and assign them to the corresponding data members.
int getFace()
This accessor method returns the face value of the Card. It takes no arguments and returns an integer.
char getSuit()
This accessor method returns the suit value of the Card. It takes no arguments and returns a character.
void displayCard()
This method displays a text version of the Card. It takes no arguments and returns nothing.
For the face value, if the value is:
1, display "Ace"
11, display "Jack"
12, display "Queen"
13, display "King"
any other value, display the value
For the suit value, if the value is
'C' display "Clubs"
'D' display "Diamonds"
'H' display "Hearts"
'S' display "Spades"
So if a Card object has a face value of 6 and suit value of 'H',
should be displayed. Or if a Card object has a face value of 12 and suit value of 'C',
should be displayed.
Part 1: Testing the Card class
Before using the Card class as part of a larger project, it should be tested to make sure that the constructor and all of the methods work. A short program has been written that will test each one of the methods individually.
The test program can be downloaded from http://faculty.cs.niu.edu/~byrnes/csci240/pgms/240test10.cpp
The output that is produced by the test program:
If the output, using your Card class, matches the above output, move on to part 2 of this assignment. Otherwise, fix the constructor/methods until the output does match.
Explanation / Answer
/***************************************************************
CSCI 240 Program 10 Fall 2015
Programmer:
Section:
Date Due:
Purpose:
***************************************************************/
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
#include <algorithm>
using namespace std;
/********** Put the Card class definition between these lines **********/
class Card
{
public:
int face;
char suit;
Card()
{
int x=rand()%(13-1)+1 ;
int y=rand()%(4-1)+1;
face=x;
if(y==1)
suit='C';
else if(y==2)
suit='D';
else if(y==3)
suit='H';
else
suit='S';
}
void setCard( int newFace, char newSuit )
{
face=newFace;
suit=newSuit;
}
int getFace()
{return face;}
char getSuit()
{return suit;}
void displayCard()
{
string val;
string suit_val;
if(face==1)
val="Ace";
else if(face==11)
val="jack";
else if(face==12)
val="Queen";
else if(face==13)
val="King";
if(suit=='C')
suit_val="Clubs";
else if(suit=='D')
suit_val="Diamonds";
if(suit=='H')
suit_val="Hearts";
if(suit=='S')
suit_val="Spades";
if(face!=1 || face!=11 || face!=12 || face!=13)
cout<<face<<" of "<<suit_val<<endl;
else
cout<<val<<" of "<<suit_val<<endl;
}
};
/***************************************************************************/
int main()
{
//Set the seed value for the random number generator
srand(3);
//Test 1: Constructor and displayCard method
//Create the 3 Card objects
Card firstCard;
Card secondCard;
Card thirdCard;
//Display the 3 objects
cout << "Test 1: Constructor and displayCard method" << endl << endl
<< " The first card should be the 10 of Clubs. It is the ";
firstCard.displayCard();
cout << "." << endl << endl << " The second card should be the King of Spades. It is the ";
secondCard.displayCard();
cout << "." << endl << endl << " The third card should be the Queen of Diamonds. It is the ";
thirdCard.displayCard();
//Test 2: setCard method on the first object
cout << endl << endl << "Test 2: setCard method" << endl << endl;
//Change the first card to the Ace of Hearts
firstCard.setCard( 1, 'H' );
cout << " The first card should be the Ace of Hearts. It is now the ";
firstCard.displayCard();
//Test 3: getFace and getSuit methods on the second object
cout << endl << endl << "Test 3: getFace and getSuit methods" << endl << endl;
cout << " The second card should have a face value of 13. It is "
<< secondCard.getFace() << endl
<< " The suit value should be S. It is " << secondCard.getSuit() << endl;
getchar();
return 0;
}
/********** Code the Card class methods between these lines **********/
/*************************************************************************/
//Note : Please be informed that you have mention that we should use random generator in default constructor then it means it can generate any random value.Then in main method() how can you specify << "Test 1: Constructor and displayCard method" << endl << endl
<< " The first card should be the 10 of Clubs. like this.
Because we are using random generator even we dont know what value it generate. So any random card can come out.
Hope you understand.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.