Card Class

Program Overview

For this lab, we will be completing a program that plays a two-player version of the card game Go Fish. In this game, each player is dealt an initial hand of cards, then they take turns asking the other player for cards. Players score a “book” whenever they collect four cards of the same rank (i.e., face value), and the winner is the player who accumulates the most books. A more complete description of the rules of the game can be found on Wikipedia.

By the end of the lab, we will have a program where two players play the game, which could be two different types of players: a random AI or a user-controlled player. You can allow the computer to play against itself, play vs. you, or you play vs. a friend!

We have provided you with several parts of the game already completed, including a Deck class that represents a deck of 36 cards, a Player interface that defines the methods of each player of the game, and a GoFish class that implements the rules of the game and allows two Player objects to interact and play the game.

We will begin the lab by recreating the Card class from the lecture and slides that represents each card of the game as a combination of a suit (Clubs, Diamonds, Hearts, or Spades) and a rank (the numbers 2-10).

Card Class

Begin this part of the lab by opening up the provided Card.java file in your GitHub repository in Visual Studio Code. We have already defined the class with the line public class Card for you, and we have also provided the Suit enum discussed in class that defines the four types of suits that a card can have (Clubs, Diamonds, Hearts, Spades).

Instance Variables

The first thing we implement when we create a new class is the instance variables that a class will have. In this case, we have two instance variables:

private Suit suit;

and

private int rank;

Recall that we make most instance variables private so that the class can control when their values are read and assigned. Copy the two lines above into your Card.java file, replacing the comment /* Your suit and rank instance variables go here. */.

ReadMe

You do not need to implement JavaDoc comments for your instance variables or methods since we haven’t learned in detail how to do so, yet. If you want to leave comments reminding yourself the purpose of each, that is good programming practice and a great habit to build.

Constructor Method

Afterwards, we need to be able to provide values for those instance variables. Replace the line /* Your constructor Card method goes here. */ with a Card constructor that takes two parameters: a Suit suit and a int rank and assigns the values of those two parameters to the instance variables you created above.

getSuit and getRank Methods

Next, we want to provide getter methods for our suit and rank instance variables so they can be read elsewhere in our program (e.g., by a Player who wants to see if they have any Cards with a rank asked by their opponent). Create a method getSuit that takes no parameters and returns the value of the suit instance variable. Then, create a similar method called getRank that takes no parameters and returns the value of the rank instance variable.

equals and toString Methods

Finally, we will create two utility methods that could be useful elsewhere in our program:

  1. equals that checks to see if another Card has the same suit and rank values as the current Card
  2. toString that creates a String that contains information about the current Card (e.g., for printing to a user during the GoFish game)

Here is the example of our equals method from lecture:

/**
 * Compares with another Card.
 *
 * @param another The other Card
 * @return true if the Cards are the same, else false
 */
public boolean equals(Object another) {
    // cast another to be a Card
    Card otherCard = (Card) another; 

    // convert to a Card
    return this.suit == otherCard.suit && this.rank == otherCard.rank;
}

and here is a skeleton of a toString method:

/**
 * Converts the Card to a displayable message.
 *
 * @return A String containing the information about the card
 */
public String toString() {
    String string = "Fill me in!";
    return string;
}

You should replace the message "Fill me in!" with a String that contains the suit and rank in a nice, human-readable format.

Testing and Committing Your Progress

At this point of our program, it is difficult to test whether our Card class works, but please compile the class with

javac Card.java

until there are no compliation errors (fix any that appear).

Do not forget to save your progress periodically to GitHub using the add, commit, and push commands with git.