Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Help with my code for project DeckOfCards

  1. #1
    Junior Member
    Join Date
    Oct 2017
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with my code for project DeckOfCards

    I need to make a flush method and I've been trying different things based off what I've read and what my instructor has told me.
    1. Add a method getSuit to class Card
    2. Modify the class TestGame to deal a five-card poker hand
    3. Modify the class DeckOfCards to include a method that determines a hand contains a flush



    I've added bold to the code that has error.


    ------------------------------------------------------------------------------------
    import java.security.SecureRandom;

    public class DeckOfCards {
    private static final SecureRandom randomNumbers = new SecureRandom();
    private static final int NUM_OF_CARDS = 52;

    private Card[] deck = new Card[NUM_OF_CARDS];
    int[] hand = new int[5];
    private int currentCard = 0;



    public DeckOfCards() {
    String[] faces = {"Ace", "Deuce", "Three", "Four", "Five", "Six",
    "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};

    String[] suits = {"Hearts", "Clubs", "Diamonds", "Spades"};

    // populate deck with card objects
    for (int count = 0; count < NUM_OF_CARDS; count++) {
    deck[count] = new Card(faces[count % 13], suits[count / 13]);
    }
    } // end of DeckOfCards constructor

    public void shuffle() {

    // after shuffling, dealing should start at deck{[0]
    currentCard = 0;// reinitliaze currentCard

    // for each Card, pick another random Card (0-51) and swap them
    for(int first = 0; first < deck.length; first++) {

    // select a random number between 0 and 51
    int second = randomNumbers.nextInt(NUM_OF_CARDS);

    Card temp = deck[first];
    deck[first] = deck[second];
    deck[second] = temp;
    } // end of loop
    } // end of method shuffle

    public Card dealCard() {
    if (currentCard < deck.length) {
    return deck[currentCard++];
    } else {
    return null;
    }
    }

    public Card dealHand() {
    if (currentCard < 5 ) {
    return deck[currentCard++];
    } else {
    return null;
    }
    } // end of method

    flush(hand);

    public Card flush(int hand[]) {
    String suit = hand{0}.getSuit();
    for(int i = 1; i < hand.length; i++){
    if (hand[i].getSuit() != suit){
    ret
    }
    return null;
    }
    }

    }// end of class

    --------------------------------------------------------------------

    public class Card {
    private final String face; //face of card ("Ace", "duce", ...)
    private final String suit; // suit of a card ("hearts:, "diamonds",...)

    // constructor of Card object
    public Card(String cardFace, String cardSuit) {
    this.face = cardFace;
    this.suit = cardSuit;
    } // end of constructor

    public String toString() {
    return face + " of " + suit;
    } // end of toSTring

    public String getSuit(){
    return this.suit;
    } // end of getSuit

    } // end of class



    -----------------------------------------------------------



    public class TestGame {
    public static void main(String[] args) {
    // TODO code application logic here
    DeckOfCards myDeck = new DeckOfCards();
    Card[] hand = new Card[5];

    for(int card = 0; card < 52; card++){
    System.out.printf("%-19s", myDeck.dealCard());
    if ((card % 4) == 0) {
    System.out.println();
    }
    } // end of for

    // shuffle the entire deck
    myDeck.shuffle();
    System.out.println("\n\n");
    System.out.println("Shuffled deck of cards:");
    for(int card = 0; card < 52; card++){
    System.out.printf("%-19s", myDeck.dealCard());

    if ((card % 4) == 0) {
    System.out.println();
    }
    } // end of for
    for(int i=0; i <= 5;i++){
    hand[i] = myDeck.dealHand();
    System.out.printf("%-19s", myDeck.dealHand());
    }
    } // end of main method
    } // end of class

  2. #2
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    277
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default Re: Help with my code for project DeckOfCards

    Where are your TestGame class? Please specific the errors too
    Whatever you are, be a good one

  3. #3
    Member
    Join Date
    Apr 2014
    Posts
    92
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default Re: Help with my code for project DeckOfCards

    Hi,

    You can't use == or != on Strings, you have to use the equals() method:
    if (hand[i].getSuit().equals(suit) == false){
    // etc.
    }
    Also, if the method is supposed to tell whether or not the hand is a flush, what kind of data type should it return? Are you sure it should return a Card?

    Another thing, curly braces are not the correct way to access an array element:
    hand{0}.getSuit();

Similar Threads

  1. Help me with my code for a project? Not sure how to fix the errors.
    By tyrion in forum What's Wrong With My Code?
    Replies: 12
    Last Post: June 13th, 2014, 08:45 AM
  2. Replies: 11
    Last Post: February 10th, 2012, 05:38 PM
  3. Problem with Java code for Project
    By JavaAsh in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 19th, 2011, 03:52 AM
  4. Add code to an existing project
    By atul.mathur31 in forum Java IDEs
    Replies: 1
    Last Post: January 5th, 2011, 06:50 PM

Tags for this Thread