0

I'm working on a assignment for school and I'm having an issue that for the life of me I cannot figure out.

This is just a simple "Card Game" assignment where we are basically just creating a deck of cards, shuffling and dealing out a couple cards.

My problem comes in when "dealing" the cards the Ace, Jack, Queen and King are still printing out as an int from 11-13. I believe the problem should be in the Card class under setValue() but it just all seems ok. I will post that class first and then post the remainder just in case you need to view that as well.

Thanks for any help!!

P.S. I'm was trying to look this up in the book but couldn't find the answer...The BlackJack class is partially source code from an earlier chapter in the book and I couldn't figure out if it was necessary to have super(); in the constructor there? I thought it only had to do if the parent class constructor had an argument? Any help on if it has to be there and if so what it's doing.

public class Card {

protected String suit;
protected int value;
protected String rank;
protected final int LOW_VALUE = 1;
protected final int HIGH_VALUE = 13;


public String getRank() {
    return rank;
}

public int getValue() {
    return value;
}

public String getSuit() { 
    return suit;
}

public void setSuit(String st) {
    suit = st;
}

public void setValue(int val) {
    if(val >= LOW_VALUE && val <= HIGH_VALUE) {
        value = val;
    }
    else {
        value = LOW_VALUE;
    }
    if(val == 1) {
        rank = "Ace";
    }
    else if(val == 11) {
        rank = "Jack";
    }
    else if(val == 12) {
        rank = "Queen";
    }
    else if(val == 13) {
        rank = "King";
    }
    else {
        rank = Integer.toString(value);
    }           
}   
}

CardGame Class

abstract public class CardGameFP {

int suitNum = 1;
int val = 1;
int player1;
int player2;
protected final int DECK_OF_CARDS = 52;
Card fullDeck[] = new Card[DECK_OF_CARDS];
protected final int LOW_VALUE = 1;
protected final int HIGH_VALUE = 13;
protected final int HIGH_SUIT = 4;
protected final int CARDS_IN_SUIT = 13;

public abstract void display();
public abstract void dealCards();

public CardGameFP() {
    for(int i = 0; i < fullDeck.length; i++) {
        fullDeck[i] = new Card();
        if(suitNum == 1) {
            fullDeck[i].setSuit("Spades");
        }
        else if(suitNum == 2) {
            fullDeck[i].setSuit("Hearts");
        }
        else if(suitNum == 3) {
            fullDeck[i].setSuit("Diamonds");
        }
        else {
            fullDeck[i].setSuit("Clubs");
        }
        fullDeck[i].setValue(val);
        val++;
        if(val > HIGH_VALUE) {
            suitNum++;
            val = 1;
        }            
    }//end for
}   
public void shuffle() {
    for(int firstCard = 0; firstCard < DECK_OF_CARDS; firstCard++ ) {
        firstCard = ((int)(Math.random() * 500) % DECK_OF_CARDS);
        int secondCard = ((int)(Math.random() * 500) % DECK_OF_CARDS);
        Card temp = fullDeck[firstCard];
        fullDeck[firstCard] = fullDeck[secondCard];
        fullDeck[secondCard] = temp;
    }
}
}

BlackJack Class

 public class BlackJack extends CardGameFP{

    public BlackJack() {
        **super();**
        player1 = 2;
        player2 = 2;
    }

    public void display() {
        System.out.println("BlackJack");
    }   
    public void dealCards() {
        //Player 1
        System.out.println("Player 1:");
        for(int x = 0; x < player1; x++) {
            System.out.println(fullDeck[x].getValue() + " of " + fullDeck[x].getSuit() );
            shuffle();
        }
        //Player 2
        System.out.println("\nPlayer 2:");
        for(int x = 0; x < player2; x++) {
            System.out.println(fullDeck[x].getValue() + " of " + fullDeck[x].getSuit() );
            shuffle();
        }
    }
    }

PlayerCardGame

    public class PlayCardGame {

    public static void main(String[] args) {
        Card CG = new Card();
        BlackJack BJ = new BlackJack();

        BJ.display();
        BJ.dealCards();
    }
    }
Kalyan Chavali
  • 1,330
  • 8
  • 24
NoobCoderChick
  • 617
  • 3
  • 21
  • 40

1 Answers1

3

Well, you declared and initialized the rank String (which seems to be coded well), but you are printing the value instead.

In the blackjack class, you have:

System.out.println(fullDeck[x].getValue() + " of " + fullDeck[x].getSuit() );

When you should have this:

System.out.println(fullDeck[x].getRank() + " of " + fullDeck[x].getSuit() );

Alexander Guyer
  • 2,063
  • 1
  • 14
  • 20
  • OH, ok. That makes sense...I think. But if I do getRank instead what about the values from 2-10? – NoobCoderChick Jul 29 '15 at 05:52
  • Exactly! Rank will work for all values because of the way you initialize it – Alexander Guyer Jul 29 '15 at 22:14
  • Thanks again Nerdizzle! Quick question if you get a second. Any chance you could read the question in the description beginning with P.S.? About the super() and constructor... – NoobCoderChick Jul 29 '15 at 23:20
  • That's somewhat correct. The parent constructor only needs to be called if it requires arguments -- but only where the compiler is concerned. In this case, you need to call it anyways so that you can set the suits and whatnot. To put it more simply -- the super method simply calls the parent constructor. In this case, the parent constructor cycles through the deck and sets the cards' suits. In other words, if you don't call the super method, the cards' suits will not be set. – Alexander Guyer Jul 30 '15 at 17:45