Connor Mathis
Connor Mathis

Reputation: 3

Card - Deck - Hand Java Project

I know everyone gets skeptical whenever people put homework on here but I've run out of options and could really use some direction. I have a project where I have to create a deck of cards and allow the user to pick the size of the hand and then fill that hand with random cards and display that to the user. I've found plenty of answers using ArrayLists but mine requires an array and I've tried everything I know and my code is either completely wrong or throws a bunch of errors. So here are the problems I have:

1) The addCard method in the Hand class can be used to add one Card object at a time to the hand array until it is full. It should increment the cardsInHand counter each time a Card object is added to the Hand as long as there is room for the Card to fit into the Hand.

Here is the code for the Hand class:

public class Hand
{

   private int handSize;         //Holds the size of the hand
   private int cardsInHand;      //Holds the number of cards allowed in the hand
   private Card[] hand;          //Array of card objects

   public Hand()
   {
      this.handSize = 5;
      this.cardsInHand = 0;
      this.hand = new Card[52];
   }//end Default Constructor

   public Hand(int handSize)
   {
      this.handSize = handSize;
   }//end Parameterized Constructor

   public Hand(Hand handIn)
   {
      this.handSize = handIn.handSize;
      this.cardsInHand = handIn.cardsInHand;
      this.hand = handIn.hand;
   }//end Copy Constructor

   public void addCard(Card card)
   {
      hand[card]; //--> throws a type mismatch exception (change card param to an int) 

   }//end addCard()

   public int getHandSize()
   {
      return handSize;
   }

   public void setHandSize(int handSize)
   {
      this.handSize = handSize;
   }

   public int getCardsInHand()
   {
      return cardsInHand;
   }

   public void setCardsInHand(int cardsInHand)
   {
      this.cardsInHand = cardsInHand;
   }

   public Card[] getHand()
   {
      return hand;
   }

   public void setHand(Card[] hand)
   {
      this.hand = hand;
   }

   public String toString()
   {
      String msg = "";

      return msg;
   }//end toString()

}//end class

My addCard method is just all janky and I could really use some help trying to fill it so my program works. Any help or even a pointing in the right direction would be appreciated!

Upvotes: 0

Views: 1398

Answers (2)

Sukma Wardana
Sukma Wardana

Reputation: 550

@MadProgrammer already give better answer, I only want to chime a little. Knowing the OP project assignment using Java I want to comment a little about the Hand class design.

The task clearly said that user can pick hand with custom size, then the user will add card into the hand until the hand is full. Thus, I would propose the Hand class design like below.

public class Hand {

    private int size; // Hold the amount of card can be hold by hand.
    private int counter; // Count how many card added.
    private Card[] cards; // The card on hand.

    public Hand(int size) {
        this.counter = 0;
        this.size = size;
        this.cards = new Card[size];
    }

    public void addCard(Card card) {
        if (this.counter > this.size) {
            throw new IllegalStateArgument("The hand is full of card!");
        }
        this.cards[this.counter] = card;
        this.counter++;
    }

    public String show() {
        StringBuilder result = new StringBuilder("The card on hand is: \n");
        for (int i=0; i<this.size; i++) {
            result.append(this.cards[i].toString()).append("\n");
        }
        return result.toString();
    }
}

In my opinion the Hand class more easy to understand and achieve the goals of the Hand purpose from the task. However, just use this as reference and write code that you understand well.

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347332

My addCard method is just all janky and I could really use some help trying to fill it so my program works. Any help or even a pointing in the right direction would be appreciated

Sometimes the best thing to do is stop, turn the screen off, get a pen and piece of paper and just nut it out without any code. Try to understand the problem and get the logic straight in your head.

Basically, you have a series of buckets into which you can put a Card. Before you can put a Card in a bucket, you need to know if you have any free buckets available.

If there are, you need to add the Card to the next available bucket (which should be pointed to by cardsInHand) and increment cardsInHand

Your error is because you're trying reference a "bucket" using a Card, but you can only reference a "bucket" by an index (number) so...

hand[card];

should be more like...

hand[cardsInHand] = card;

but only after you've determined if there is a free "bucket" available, and you should increment cardsInHand AFTER this statement

I'd also be worried about your constructors

public Hand(int handSize)
{
   this.handSize = handSize;
}//end Parameterized Constructor

isn't initialising hand, so that's going to be null. A better solution might be to use existing constructors where possible to build a common "initialisation" path

public Hand() {
    this(5);
}//end Default Constructor

public Hand(int handSize) {
    this.handSize = handSize;
    this.cardsInHand = cardsInHand;
    this.hand = new Card[handSize];
}//end Parameterized Constructor

Also

public Hand(Hand handIn)
{
   this.handSize = handIn.handSize;
   this.cardsInHand = handIn.cardsInHand;
   this.hand = handIn.hand;
}//end Copy Constructor

is worrying, as it's possible for some external class to make a change to handIn's hand and that change will be reflected by this instance as well (as they are pointing to the same array).

A "copy" constructor should be making a "copy" of the data. Realistically, this should probably be a "deep" copy, so any changes to Card don't mess with the Hand as well, but I'll start with a simple "shallow" copy to get you started

public Hand(Hand handIn) {
    this.handSize = handIn.handSize;
    this.cardsInHand = 0;
    this.hand = new Card[this.handSize];
    // Yes, I know there is a better way to do this, but
    // I want the OP to learn something
    for (int index = 0; index < handIn.hand.length; index++) {
        Card card = handIn.hand[index];
        if (card != null) {
            hand[cardsInHand] = card;
            cardsInHand++;
        }
    }
}//end Copy Constructor

Upvotes: 1

Related Questions