User Tools

Site Tools


This is an old revision of the document!

Table of Contents



One of the first problems we will tackle in this module is to design an abstraction that can conveniently represent a single playing card. In a standard deck of cards there are 52 distinct cards and any given card can be completely defined by its suit (Hearts, Spades, Diamonds, Clubs) and its rank (Ace, 2, 3,…,10, Jack, Queen, King).

In a program we can represent a playing card in many different ways. For example, using a single integer between 0 and 51 where the value of the integer somehow represents the card. Or, we could represent a card using a combination of 6 boolean values (insane but technically possible). Here to apply the principle of information hiding, we would organize our program structure so as to hide the decision of how exactly we represent a card in the program.

we started to design this type, we would quickly realize that our program also needs to manipulate two other types of values: suits and ranks. These types of values are a bit different because they are more like labels for domain objects than actual objects. What makes them feel like labels are that there are a finite number of them for a particular type of values (e.g., 4 for suits), and it appears to be useless to have two or more instances representing a given suit (e.g., clubs). In fact values of these types would be used more or less as constants in a program.

public enum Suit


Extend the Suit enumerated type in Card to include a method color() that returns the color of the suit. The return type should be a new enumerated type Color.

Extend version 7 of class Card to support the concept of a “Joker” (a special card that is not in any suit) while keeping the class as well-encapsulated as possible.

Add a method getCards() to the Deck class that returns the cards in the deck without breaking encapsulation.

Class Interactions Consider the following program:

class Game
   Deck aDeck;
public final class Deck
   private  Stack<Card> aCards = new Stack<>();
   public Deck( Deck pDeck ) {...}
   public void shuffle() {...]
   public Card draw() {...}
   public boolean isEmpty() {...}

Hand class

Design and implement a well-encapsulated abstraction to represent a “hand” of cards in a player's hand as a Java class Hand. A Hand should be able to contain between 0 and N cards, where N is a a parameterizable upper bound that will depend on the card game being played (e.g., 5 for draw poker, 13 for bridge, etc.). Implement the following services on a Hand: add(Card), remove(Card), contains(Card), isEmpty(), size(), and isFull(). Find a way to provide access to the cards in the hand. Ensure that all the rules of encapsulation seen in Module 1 are respected and use Design by Contract to clarify valid and invalid inputs

2019_2020/s3/concprogobjet/td/td3.1569166909.txt.gz · Last modified: 2019/09/22 17:41 by blay