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 8 of 8

Thread: Guys please Help me find answers on A and B below

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Guys please Help me find answers on A and B below

    please i have this task to to but i can only manage to display five cards but i cant compare my result, here is what should be done:

    (Card Shuffling and Dealing) Modify the application to deal a five-card poker
    hand. Then modify class "DeckOfCards" to include methods that determine whether a
    hand contains :

    a) a pair
    b) two pairs
    c) three of a kind (e.g., three jacks)
    d) four of a kind (e.g., four aces)
    e) a flush (i.e., all five cards of the same suit)
    f) a straight (i.e., five cards of consecutive face values)
    g) a full house (i.e., two cards of one face value and three cards of another face value)

    also and also Use the methods developed to write an application
    that deals two five-card poker hands, evaluates each hand and determines which is better.

    below is the codes of classes

    i have tried introducing this code bu it gives me 3 errors:"

    "if(myDeckOfCards.dealCard(face + " of " + suit).equals("Duece of Hearts"))
    {
    System.out.printf( "A Pair" );


    }"


    <//  Card.java
    // Card class represents a playing card.
     
    public class Card 
    {
       private String face; // face of card ("Ace", "Deuce", ...)
       private String suit; // suit of card ("Hearts", "Diamonds", ...)
     
       // two-argument constructor initializes card's face and suit
       public Card( String cardFace, String cardSuit )
       {
          face = cardFace; // initialize face of card
          suit = cardSuit; // initialize suit of card
       } // end two-argument Card constructor
     
       // return String representation of Card
       public String toString() 
       { 
          return face + " of " + suit;
       } // end method toString
    } // end class Card
     
    >

    <// DeckOfCards.java
    // DeckOfCards class represents a deck of playing cards.
    import java.util.Random;
     
    public class DeckOfCards
    {
       private Card[] deck; // array of Card objects
       private int currentCard; // index of next Card to be dealt
       private static final int NUMBER_OF_CARDS = 52; // constant # of Cards
       // random number generator
       private static final Random randomNumbers = new Random();
     
       // constructor fills deck of Cards
       public DeckOfCards()
       {
          String[] faces = { "Ace", "Deuce", "Three", "Four", "Five", "Six",
             "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
          String[] suits = { "Hearts", "Diamonds", "Clubs", "Spades" };
     
          deck = new Card[ NUMBER_OF_CARDS ]; // create array of Card objects
          currentCard = 0; // set currentCard so first Card dealt is deck[ 0 ]
     
          // populate deck with Card objects
          for ( int count = 0; count < deck.length; count++ )
             deck[ count ] =
                new Card( faces[ count % 13 ], suits[ count / 13 ] );
     
     
       } // end DeckOfCards constructor
     
       // shuffle deck of Cards with one-pass algorithm
       public void shuffle()
       {
          // after shuffling, dealing should start at deck[ 0 ] again
          currentCard = 0; // reinitialize currentCard
     
          // for each Card, pick another random Card and swap them
          for ( int first = 0; first < deck.length; first++ )
          {
             // select a random number between 0 and 51
             int second =  randomNumbers.nextInt( NUMBER_OF_CARDS );
     
             // swap current Card with randomly selected Card
             Card temp = deck[ first ];
             deck[ first ] = deck[ second ];
             deck[ second ] = temp;
     
     
     
     
          } // end for
       } // end method shuffle
     
       // deal one Card
       public Card dealCard()
       {
          // determine whether Cards remain to be dealt
          if ( currentCard < deck.length )
             return deck[ currentCard++ ]; // return current Card in array
          else
             return null; // return null to indicate that all Cards were dealt
     
     
     
       } // end method dealCard
     
     
     
    } // end class DeckOfCards>

    <// DeckOfCardsTest.java
    // Card shuffling and dealing.
     
    public class DeckOfCardsTest
    {
       // execute application
       public static void main( String[] args )
       {
          DeckOfCards myDeckOfCards = new DeckOfCards();
          myDeckOfCards.shuffle(); // place Cards in random order
     
          // print all 52 Cards in the order in which they are dealt
          for ( int i = 1; i <= 5; i++ )
          {
             // deal and display a Card
             System.out.printf( "%-19s", myDeckOfCards.dealCard() );
     
    		 if ( i % 1 == 0 ) // output newline every 4 cards
    		    System.out.println();
     
          } // end for
     
         /* if(myDeckOfCards.dealCard(face + " of " + suit).equals("Duece of Hearts"))
             {
     
             }*/
       } // end main
    } // end class DeckOfCard>


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Guys please Help me find answers on A and B below

    it gives me 3 errors:
    Please copy the full text of the error messages and pasted them here.
    if(myDeckOfCards.dealCard(face + " of " + suit).equals("Duece of Hearts"))
    That is really an ugly way to compare two cards. You should make numeric values to represent the cards.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Guys please Help me find answers on A and B below

    like how norm, the error i'm getting is:C:\Users\Vukosi\Desktop\fig07_09_11\DeckOfCards Test.java:23: error: cannot find symbol
    if(myDeckOfCards.dealCard(face + " of " + suit).equals("Duece of Hearts"))
    ^
    symbol: variable face
    location: class DeckOfCardsTest
    C:\Users\Vukosi\Desktop\fig07_09_11\DeckOfCardsTes t.java:23: error: cannot find symbol
    if(myDeckOfCards.dealCard(face + " of " + suit).equals("Duece of Hearts"))
    ^
    symbol: variable suit
    location: class DeckOfCardsTest
    2 errors

    Process completed.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Guys please Help me find answers on A and B below

    The compiler can not find definitions for the variables: face and suit that are in scope where they are being used. Where are those variable defined?

    Where is the dealCard() method defined that has a String argument?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Guys please Help me find answers on A and B below

    OK i'm just getting more confuse as you ask me question because i'm just a beginner, so what i'm asking now is how will you solve the given assignment by at least answering Question a please

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Guys please Help me find answers on A and B below

    Change the Card class's constructor to take int values instead of Strings. The int values will be easier to work with.


    What is "question a"?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Apr 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Guys please Help me find answers on A and B below

    (Card Shuffling and Dealing) Modify the application to deal a five-card poker
    hand. Then modify class "DeckOfCards" to include methods that determine whether a
    hand contains a )pair

    - A Pair is when at least 2 cards are similar out of 5 cards that have dealt in a hands

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Guys please Help me find answers on A and B below

    Some considerations:
    all these hands contain a pair:
    four of a kind
    full house
    three of a kind
    two pair
    one pair

    One approach would be to start at the top of that list and work down, eliminating each possibility.
    Sorting the cards would make the search easier.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Multiple answers to a question
    By jps in forum Totally Off Topic
    Replies: 0
    Last Post: October 6th, 2012, 01:43 PM
  2. Need help with understanding a homework assignment, not asking for answers
    By NewbieJavaProgrammer in forum Object Oriented Programming
    Replies: 22
    Last Post: September 14th, 2012, 11:12 PM
  3. How do I get my answers to out with 2 decimal places?
    By dunnage888 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 8th, 2012, 12:23 PM
  4. Yes or No answers,
    By tyb97 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: September 18th, 2011, 05:57 PM
  5. Is My answers correct??
    By Java.Coder() in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 28th, 2010, 06:22 AM