TDT4102, Spring 2009

Exercise 4

Deadline: 20.02.2009


The objective of this exercise:

General requirements:

Recommended reading:


Part 1: Enumerations (10 pt.)

In this part of the exercise you will create an enumeration and use it in a switch-statement

a) Create the enumeration beatle that has the possible values JOHN, PAUL, GEORGE and RINGO.

b) Create the function isAlive() that takes as input a beatle as defined in a) and returns a boolean stating if the person is currently alive ( Paul and Ringo are currently alive ). Use the switch statement to check against the enum.

Part 2: Structures (20 pt.)

In this part of the exercise you will create some Structures and try some of the functionality of these.

a) Create the struct musician that has the following values:

name, an string representing the musician's name.
weight, an integer representing the musician's weight.
height, an integer representing the musician's height.
beardLength, an integer representing the length of the musician's beard.


b) Create the struct band that has the following values:

name, an string representing the band's name.
rockBand, a boolean representing if the band is a rock band or not.
releases, an integer representing the number of record releases by the band.
musicians, an array of Musicians ( struct from a) ) containing the 4 members of the band. As all real bands have 4 members, this size is fixed.


c) Create a function totalBeardLength() that takes as input a band in the form of the struct defined in b). The function should calculate the the total length of the beard of all the musicians in the band, only if the band is a rock band. If the band is not a rock band it is of no interest to us, and you should return -1 to indicate this.

d) Create a function testBeardThingie() that creates two bands, using the structs defined previously. The bands should contain several musicians with different heights, weights and beard lengths. The bands should also have a couple of releases, and different names. One of the bands should be a rock band, and the other a non-rock band.
Use the bands to test the totalBeardLength(). Use the assert macro to ensure that the function has the correct behavior.

Part 3: using the string Class (15 pt.)

In this part of the exercise you will test some of the functionality of an existing class, the string.

At this point we should point out that there are two different strings in c++, c-strings and the standard class string. In this exercise we will be using the latter, so be sure to #include <string>, and use the std namespace.

a) Create the function nameFixer(). The function should take a string as input and return a string.
The function should convert a name in the form FIRSTNAME LASTNAME to the form LASTNAME, FIRSTNAME.

Example of correct behavior:
Input: Thomas Edison - Output: Edison, Thomas


Use the string class' various functions to implement this functionality.

Part 4: Card Class (20 pt.)

In this part of the exercise you will implement a Card class that we will use in the next part to build a deck of cards, with some basic functionality.

a) Create the class Card. The class should have the following variables. For now you can let them all be public:

value, an integer representing the value of the card ( typically 1 - 13 ).
suit, a string representing the suit of the card ( typically S, H, D or C ).


b) Create the member function getFace(). This should be a public function that returns the suit and value of the card as a string.

Example returned string: "S5".


Converting an integer to a string is, unfortunately, not straightforward. For this you can use a stringstream ( remember to #include <sstream> ), in the following way:

int number = 42;
string numberasstring;
stringstream ss;
ss << number;
numberasstring = ss.str();


Edit: The last line was originally ss >> numberasstring;, but using ss.str() is better, since the first version will only give you the first word of the stream if there are several words. See the code example from the exercise lecture.

Optional part c) A better way to handle the suit of the card would be to use an enumeration, as in part 1. Create the enum suit, and change the Card class so it uses this enum to handle the suit. You will have to make some changes to the getFace() function, as you cannot directly convert an enum to a string. Using a switch statement is recommended.

Part 5: CardDeck Class (35 pt.)

In this part of the exercise you will implement a CardDeck class that will use the Card class to represent a deck of cards. We will then implement some easy functionality for this deck.

a) Create the class CardDeck. The class should have the following member variable:

cards, an array of 52 Card objects, representing our deck. This variable should be private, so no evil, external forces may change our deck.


b) Create the member function buildDeck(). This function should take no input and return nothing, but it will build our deck.
The function should populate the cards array with all the cards, with values from 1 - 13 in all 4 suits.

c) Create the member function shuffle(). This function should shuffle the contents of the cards array. It takes no input and returns nothing, it's effect being the rearranging of the cards array.

d) Create a constructor for the deck. The constructor should call the buildDeck() function.

e) Create a second constructor for the deck. This constructor should take a boolean shouldShuffle as input. The constructor should call the buildDeck() function. if the input boolean shouldShuffle is true, the shuffle() function should also be called.

Edit: changed the name of the input boolean to shouldShuffle, since there is a member function called shuffle.

f) Create the member function dealHand(). This function should deal a hand of cards from the deck if there is enough cards remaining.

To achieve this you will have to add a new variable to the class, numberdealt, an integer counting the number of cards that have been dealt from the deck.
The function should take as input an array of cards that we'll use for returning the result. This is called an array parameter, you might want to read up on this.
The function should also take an integer stating the number of cards wanted. This should be checked together with the numberdealt variable to see if there are sufficient undealt cards remaining in the deck. If there are not sufficient cards remaining, do nothing. If there are enough cards remaining, fill the array taken as input with the correct cards from the deck. You are free to choose which end of the deck to start dealing from.

g) Test your work from the main() function. Create a deck, then try withdrawing consecutive hands from the deck, printing the output to screen with help of the getFace() function of each card.