2

I'm making a poker game in Java, and our teacher doesn't know Java that well so his instructions were written by a teacher's aid. I'm trying to make a Hand.java class to serve as a storage space for two character Strings that will act as cards (ie. "2h" = "2 of hearts").

His instructions are as follows:

  • The Hand class is an object to be instantiated. It will represent a single hand within a game, and will mainly consist of an array of cards.
  • Field: ArrayList cards Storage space for cards in hand List item
  • Constructor: Hand(String[]) Initializes the cards field with an array of two-character abbreviations for cards.
  • Method: void addCard(String) Adds the card represented by an abbreviation to the cards field

Here's what I have so far:

import java.util.*;
public class Hand {

    public String[] array;
    ArrayList<String> cards = new ArrayList<String>();

public Hand(String[] array) {
    this.array = array;
}

public void addCard(String card) {
    cards.add(card);
}

(further instructions) The final test should use the following sequence of commands in the main program. The program segment:

Hand h = new Hand("3c", "4s", "5d", "6h", "7h"); //program is upset with this line
h.printHand();
h.addCard("8d");
h.addCard("3d");

Should output:

Printing Hand:  5 cards  –  3c  4s  5d  6h  7h
Adding Card:    Eight of Diamonds  (8d)
Adding Card:    Three of Diamonds  (3d)

Am I crazy, or do I actually need an array within the constructor of the Hand class? I don't understand his explanation of " Constructor: Initializes the cards field with an array of two-character abbreviations for cards" Any advice is much appreciated!

Yoleaux
  • 49
  • 6
  • 1
    consider `Hand h = new Hand(new String []{"3c", "4s", "5d", "6h", "7h"});` – Scary Wombat Nov 30 '16 at 05:44
  • 2
    Firstly, I think you either need `array` or `cards` and not both and the new Hand takes an array so `new Hand(new String[]{"3c", "4s", "5d", "6h", "7h"}); `should remove the compilation error. – Aimee Borda Nov 30 '16 at 05:45

3 Answers3

1

There is no need to save the passed array as a field inside Hand class, it won't be used anywhere. This array is just passed in the constructor in order to initialize the main cards list.

So every time you call the constructor, you're creating and initializing a new hand with new cards.

You can just initialize the cards field in the constructor from the passed array so your class would look like the following:

    import java.util.*;
public class Hand {
    ArrayList<String> cards = new ArrayList<String>();

public Hand(String[] array) {
    cards = Arrays.asList(array);
}

public void addCard(String card) {
    cards.add(card);
}

And in your main class, initiate the Hand class as the following:

Hand hand = new Hand(new String[]{"3c", "4s", "5d", "6h", "7h"})
Hussein Terek
  • 588
  • 9
  • 13
  • Sorry, I'm having a hard time following the first code snippet you posted. So I'd do this? `public class Hand { public String[] array; ArrayList cards = new ArrayList(); public Hand(String[] array) { cards = Arrays.asList(array); }` – Yoleaux Nov 30 '16 at 06:10
  • I've edited my answer, please check it now. My idea is to get rid of "array" field from your class since there is no need to save the passed array in separate field, it's content is already saved in the cards list. – Hussein Terek Nov 30 '16 at 06:27
  • If it works with you, please check my answer to help others, thanks. – Hussein Terek Nov 30 '16 at 06:29
1

If your teachers says, you need a Constructor with a paramater of an array of String. I think your approach is right, however, after you initialize an array of 2 character String in the Constructor. you need to add it to your ArrayList cards as well, so your cards is in the same container.

" Constructor: Initializes the cards field with an array of two-character abbreviations for cards"

I think that your teacher is saying in here that you need to initialize String of array and then put it in the cards fields which is an ArrayList. you can put it this way:

public Hand(String[] array) {
    cards.addAll(Arrays.asList(array));
}

and by the way, change your Initialization from this:

Hand h = new Hand("3c", "4s", "5d", "6h", "7h");

to this:

Hand h = new Hand(new String[]{"3c", "4s", "5d", "6h", "7h"});
msagala25
  • 1,806
  • 2
  • 17
  • 24
0

I too am working on the same problem. When I run my code I get null for everything. If you could help with with this please. I think it has to do with the card.java

Britt
  • 1
  • 3
  • IF you post your code I could take a look, if not, check out my code here: https://github.com/aepries/A8-Poker – Yoleaux Dec 22 '16 at 19:33