1. Engineering
  2. Computer Science
  3. java 80 or greater thanks we revisit the single player...

Question: java 80 or greater thanks we revisit the single player...

Question details

(JAVA 8.0 or greater, thanks!)

We revisit the single player game Rummy introduced in ITI 1120/1520. We use the same set of rules, but we take this opportunity to apply the object-oriented programming concepts that we have just learnt.

In the game of Rummy, the main goal is to build melds, which consists of sets: two, three or four of a kind of the same rank; or progression, three or more cards in a sequence of consecutive ranks, of the same suit. So the set ♡10, ♢10, ♣10 forms three of a kind. And the set/sequence ♢7, ♢8, ♢9, ♢10, ♢11 forms a progression. The goal of the player is to get rid of all of the cards in as few rounds of the game as possible.

One of the first changes that we make will be to create a data type (a class) to represent the cards. Just like in ITI 1120/1520, the number of suits is fixed (4), but the number of ranks can be any value in the range from 3 to 99.

Next, we also create a data type to represent a deck of cards. Using arrays to store the cards would require you to write a large amount of code since the arrays have a fixed size and support a limited number of operations. Since you are already familiar with the concept of lists, the class Deck will use an object of the class ArrayList to store cards. The fact that one can use a class without knowing the details of its implementation is one of the greatest strengths of object-oriented programming. In the second part of the semester, we will learn about the implementation of lists.

Our implementation of the game consists of 6 classes: Die, Card, Deck, Game, Run, and Utils. In the following sections, we describe the requirements for each class.

2.2 Card (15 marks)

You must implement the class Card.

  • An object of the class Card stores a suit and a rank, both of type int.
  • The class declares one constructor. It receives the initial value for the suit and the rank as parameters.
  • Each object has two “getters”, getSuit and getRank, which returns the value of the suit and the rank, respectively.
  • An object of the class Card has a method equals with a parameter of type Object. If the object designated by the parameter is not of type Card, the method returns false, as these objects cannot be considered “equals”. Otherwise, the method returns true if and only if the object designated by parameter is a Card that has the same suit and the same rank. Here is a starting point for your implementation:

    1 public boolean equals(Object object) {
    3    if (! (object instanceof Card)) {
    4        return false;
    5    }
    7    Card other;
    8    other = (Card) object;
    10    // Complete the implementation...

  • The method toString returns a string with the suit and the rank of this card. In order to keep the representation compact when printing an entire deck of cards, no other information than the suit and the rank is used. See below.
  • Finally, the class Card declares four constants: DIAMOND, CLUB, HEART, and SPADE, with values 0, 1, 2, and 3, respectively.

Here is a small example showing the intended use.

1 Card a, b, c;
2 a = new Card(2, 7);
3 b = new Card(2, 7);
4 c = new Card(0, 9);
5 System.out.println(a);
6 System.out.println(a.equals(b));
7 System.out.println(a.equals(c));


Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution