Course Web Pages
Related Links
Free eBooks
|
Class Design
The videos below will step you through the design of a Deck class that
represents a deck of playing cards. The videos are based on Chapter 12,
"Class Design and Implementation" from Beginning C#
Programming with XNA Game Studio, by A.T. Chamillard.
The UML diagramming tool mentioned below is
creately
High Level Class Design, Part 1
deck.zip
High Level Class Design, Part 2
From the Deck Diagram to the Deck Header File
Deck Stubs and Tests
Implementing Deck's constructor and the Size and Print member functions
Implementing Deck's Cut and Shuffle member functions
Implementing Deck's TakeTopCard member functions
The Book Project
Exercises
This is your final project. Congratulations on making it this far in the course, and good luck!
The exercises below ask you to build a Book class based on
these files:
book.zip
- Add two accessor methods to the class—
GetAuthor and
GetTitle —that return the
author and title fields as their respective results. Test your class by creating some
instances and calling the methods.
- Add two methods,
PrintAuthor and PrintTitle , to the outline Book
class. These should print the author and title fields, respectively, to the terminal window.
- Add a field,
m_Pages , to the Book class to store the number of pages. This
should be of type int, and its initial value should be passed to the single constructor, along
with the author and title strings. Include an appropriate GetPages accessor method for
this field.
- Add a method,
PrintDetails , to the Book class. This should print details
of the author, title, and pages to the terminal window. It is your choice how the details are formatted. For instance, all three items could be printed on a single line, or each could be printed
on a separate line. You might also choose to include some explanatory text to help a user
work out which is the author and which is the title, for example
Title: Robinson Crusoe, Author: Daniel Defoe, Pages: 232
- Add a further field,
m_RefNumber , to the Book class. This field can store a
reference number for a library, for example. It should be of type string and initialized to the
zero length string ("") in the constructor, as its initial value is not passed in a parameter to the
constructor. Instead, define a mutator for it with the following header:
void SetRefNumber(string ref)
The body of this method should assign the value of the parameter to the refNumber field.
Add a corresponding GetRefNumber accessor to help you check that the mutator works
correctly.
- Modify your
PrintDetails method to include printing the
reference number. However, the method should print the reference number only
if it has been set—that is,
the RefNumber string has a non-zero length. If it has not been set,
then print the string "ZZZ"
instead. Hint: Use a conditional statement whose test calls the size
method on the m_RefNumber string.
- Modify your
SetRefNumber mutator so that it sets the m_RefNumber field
only if the parameter is a string of at least three characters. If it is less than three, then print an
error message and leave the field unchanged.
- Add a further integer field,
m_Borrowed , to the Book class. This keeps a count
of the number of times a book has been borrowed. Add a mutator, Borrow , to the class. This
should update the field by 1 each time it is called. Initialize the m_Borrowed field to zero in the
constructor. Include an accessor, GetBorrowed , that
returns the value of this new field as its result. Modify PrintDetails so that it includes the
value of this field with an explanatory piece of text.
- Add a further boolean field,
m_CourseText , to the
Book class. This records whether or not a book is being used as a
text book on a course. The field should be
set through a parameter to the constructor.
Provide an accessor
method for it called IsCourseText .
|