teaching machines

CS 145 Lab 7

October 24, 2011 by . Filed under cs145, fall 2011, labs.



Each of today’s problems involves a deck of cards. Though we use card terminology, you do not need to have ever touched a deck of cards to follow the instructions. However, if you confused by any of the terminology, we’re happy to answer your questions.

We’ve provided Deck and Card classes for you to use. Copy and paste them into your lab7 package. (Eclipse is smart. If you copy the entire code listing for a class, select lab7, and paste, it’ll automatically create a Java source file with the appropriate name.

Here’s Card.java:

public class Card {
  public static int ACE = 1;
  public static int JACK = 11;
  public static int QUEEN = 12;
  public static int KING = 13;
  public static int HEARTS = 0;
  public static int DIAMONDS = 1;
  public static int SPADES = 2;
  public static int CLUBS = 3;

  /** Card's suit */
  private int suit;

  /** Card's rank */
  private int rank;
   * Creates a new card.
   * @param suit Card's suit
   * @param rank Card's rank
  public Card(int suit, int rank) {
    this.suit = suit;
    this.rank = rank;
   * Gets the card's rank.
   * @return One of ACE, JACK, QUEEN, KING, and 2-10.
  public int getRank() {
    return rank;
   * Gets the card's suit.
   * @return One of HEARTS, DIAMONDS, SPADES, and CLUBS.
  public int getSuit() {
    return suit;
   * Gets a string representation of this card.
   * @return Text description of card, e.g., "queen of spades."
  public String toString() {
    String suitName;
    if (suit == HEARTS) {
      suitName = "hearts";
    } else if (suit == DIAMONDS) {
      suitName = "diamonds";
    } else if (suit == SPADES) {
      suitName = "spades";
    } else {
      suitName = "clubs";
    String rankName;
    if (rank == ACE) {
      rankName = "ace";
    } else if (rank == KING) {
      rankName = "king";
    } else if (rank == QUEEN) {
      rankName = "queen";
    } else if (rank == JACK) {
      rankName = "jack";
    } else {
      rankName = "" + rank;
    return rankName + " of " + suitName;

A Card is an Object like a Scanner is an object. You can invoke methods on it. For example:

Card card = new Card(Card.HEARTS, Card.QUEEN);
System.out.println(card.getSuit() == Card.SPADES);

Here’s the Deck class:

import java.util.Collections;
import java.util.Stack;

public class Deck {
  /** Set of cards in deck */
  private Stack<Card> cards;
   * Creates a new shuffled deck.
  public Deck() {
    cards = new Stack<Card>();
    for (int s = 0; s < 4; ++s) {
      for (int r = Card.ACE; r <= Card.KING; ++r) {
        cards.push(new Card(s, r));

   * Checks whether or not the deck is empty.
   * @return True if empty, false otherwise.
  public boolean isEmpty() {
    return cards.isEmpty();
   * Draws a card. Assumes at least one card is left in deck.
   * @return Card drawn
  public Card draw() {
    return cards.pop();

Decks are shuffled upon creation. Drawing a card involves no magic that you haven’t already seen:

Deck deck = new Deck();
Card card = deck.draw();


Solve three of the following problems. Complete each in its own class. Write pseudocode first.

  1. Play Hot-Cold. Have the computer draw a random card. Ask the user to guess what it is by typing input of the form “1 H”, “13 S”, or “9 D”. If the rank is wrong, report whether the guess is too high or too low. Also indicate if the suit is wrong. Repeat until she succeeds.
  2. Play a modified game of War. While the deck is not empty, have each player draw a card. If the rank of player A’s card is greater than player B’s, player A gets a point. And vice versa. If the ranks are the same, no one gets a point. Display the scores and winner at the end of the game.
  3. Determine short-suitedness. One is said to be short-suited when has no cards of a particular suit. Draw five cards and indicate which suits are not represented in your hand.
  4. Check for a flush, as in poker. Draw five cards and report whether all cards have the same suit.
  5. Deal the entire deck out to four players. Indicate which player got the 2 of clubs.
  6. Suppose you have a variable “int trump” which is one of HEARTS, DIAMONDS, SPADES, or CLUBS. Draw 5 cards and print out which ones belong to the trump suit. A jack of the same color as the trump suit is also considered to be trump. That is, if trump is hearts, the jack of diamonds is also trump.

Checkpoint #1: show your TA or instructor your completed solutions.


  1. Complete HW2 while you still have time to address issues in office hours.