Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Programming Assignment #1 ArrayLists I. The Assignment Card players typically like to keep the cards in their hand sorted by suit and within each suit

Programming Assignment #1

ArrayLists

I. The Assignment

Card players typically like to keep the cards in their hand sorted by suit and within each suit by face value. Assuming that a hand consists of 13 cards, write a Java program to insert each card into an initially empty hand in the proper order, so that there is no need to sort the hand.

The order of the suits is unimportant but within each suit the order of the cards should be 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A (i.e. the Ace is always the high card).

II. The Card Class

Begin by creating a class to model a playing card. The Card class will have two private instance variables: (1) the face value (aka: rank) stored as ints 2..14 and (2) the suit (Spades, Hearts, Diamonds, Clubs).

Your Card class will have these methods:

    1. a proper constructor that creates a Card with a given rank and suit
    2. an accessor (get) method that returns the rank
    3. an accessor (get) method that returns the suit
    4. a toString method that returns a String representation of a card as shown in these examples: A, 10, 7, Q
  1. To get the symbols for the suits, use the escape sequences for the Unicode characters:

\u2660 (Spade) \u2663 (Club)

\u2665 (Heart) \u2666 (Diamond)

III. The Deck Class

Create a class to model a standard 52-card deck. Your Deck class will have a private instance variable that is an ArrayList-of-Card. (You may have additional instance vars although none are needed)

Your Deck class will have these methods:

  1. a proper constructor that creates a standard Deck of 52 cards
  2. a method that deals the top card. I.e. returns the Card object on top of the Deck
  3. a method that shuffles the deck. To get credit for this method you must use this algorithm:

for each card in the deck

exchange it with the card at a randomly-selected index

(to mix em up good you might want to shuffle the deck a number of times)

IV. The Hand Class

Create a class to model a hand of 13 Cards. Your Hand class will have a private instance variable that is an ArrayList-of-Card, and these methods:

  1. a proper constructor that creates an empty Hand
  2. a method to fill a Hand with 13 Cards dealt from a Deck, with each one being inserted in its proper place in the Hand
  3. a toString method that returns a String representation of the Hand (Hint: call the Card class toString method for each Card in the Hand)

V. The Test Class

Your test class will have a main method that creates a Hand, calls the method that fills it, calls the toString method for the Hand, and prints the String returned. After each Hand is displayed, ask the user if she wants to see another.

VI. Specifications

  1. You must use a generic ArrayList of Card as the implementation of the Deck and the Hand
  1. No credit will be given if any other data structures, including arrays, are used anywhere in the program
  1. No credit for using any of the methods of Javas Collections class (e.g. shuffle). Use the shuffle algorithm given above
  2. Although the assignment calls for a Hand of 13 cards and a Deck of 52, you should parameterize your program by using defined constants for the number of cards in the Hand and in the Deck. That way, you can generate hands and decks of different sizes by changing only the constant declarations
  3. As specified above, none of the methods of your Deck, Hand, and Card classes do any output. All output is to be done in main
  1. Make sure your Deck, Hand, and Card classes all contain proper Java documentation comments and adhere to all the style and internal documentation standards discussed in class and covered in Unit 1

Algorithm to Create the Sorted Hand

Place the first card in the hand

For each additional card, do the following:

Get the suit

If the card is of a suit that is not yet in the Hand

Insert it as the new last card in the Hand

Else, do the following:

If the value is higher than all the cards of that suit

Insert it as the new last card of that suit

Else

Insert it just before the first card of that suit with a higher value

Optional Easier Algorithm to Create the Sorted Hand

Create an ArrayList of Cards for each suit

For each card, insert it in its proper place in the list for that suit, using the techniques described above

Add each suit list to the Hand

Hopefully Helpful Hints

1. Begin by creating all classes with method stubs only. A method stub is just the Javadoc comment, the method heading, and an empty body. If the method returns a value, use some temporary return value. This way, the entire program skeleton will compile and execute and you can fill in each method as you figure it out

2. Since the Hand class method fill is the most challenging, start with a temporary version that simply adds each card to the hand. That way, you can test and verify that all the other methods are correct and have the majority of the credit in the bank before turning your full attention to the full-blown fill

3. Hint: To enable your Hand class to seamlessly access the Deck, create a Deck object in your Hand class

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions