Converting procedural design to object oriented design -
Blackjack
CPE 103 lab
Compile and execute this source
code for a Blackjack game.
Examine the source code and notice that it is entirely a procedural
oriented design. There is one big class that does all of the
work. It is decomposed into functions that perform
different tasks, but none of the data is encapsulated - it's all
global to the class.
The objective is to refactor the design into an object oriented
one. Perform the refactorings below in the order listed.
- Create a class to represent a Card. Use enums for rank
and suit, as shown in this
tutorial.
Remove the existing showCard() method (it will be replaced by Card's toString()
method). Note that an enum value already has a toString()
method, so you don't need a switch statement. It also
becomes trivially easy at this point to enhance the toString()
method to return both the rank and suit, e.g.,
"three of clubs". Also make each card
value know how many points it is worth (without using switch).
- Create a Hand class and a Deck class by moving pieces of
existing code into separate classes. The deck should be
changed from an array of int to an array of Card. The deck
should be initialized in the Deck class, not in the main class or
the card class.
- Take advantage of Generics and change all references to "
Vector"
to "Vector<Card>".
- Replace the
shuffle() method with a call to the
Collections framework shuffle() method.
- Refactor any procedural code that doesn't follow structured
programming principles (e.g. break or return
inside a control structure).
Execute your working program and capture a sample execution output
to a plain text file.
Print a class diagram of the classes in your solution. (In BlueJ,
Project -> Print -> Print Class Diagram).
Write a one page (max) description of your refactored solution and
explain the benefits your solution offers over the original. Be
specific, using concrete details from the problem domain of card
games. Clearly it takes more effort to produce the OO solution. Is
the effort worth the benefits? Justify your answer.
Submission
- Put both your names in @author tags in each source file.
- Concatenate all source code files into a single file.
- On PolyLearn there is an assignment named "Blackjack Lab".
- Submit your one page summary (as a PDF),
your source code file (plain text), and the execution
output.
- Each student makes a separate submission to PolyLearn.