Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #29514

Re: Help with Deck of cards assignment and getting it to deal...

From Mike Amling <mamling@chaff.us>
Newsgroups comp.lang.java.programmer
Subject Re: Help with Deck of cards assignment and getting it to deal...
Date 2014-08-03 07:38 -0500
Message-ID <c46opoF6ekvU1@mid.individual.net> (permalink)
References <lrhe0d$l3n$1@dont-email.me>

Show all headers | View raw


On 8/1/14 8:16 PM, Scott Dunning wrote:
> Hello, I have an assignment for my summer class.  FYI, I'm super new to
> programming in general and this is my first time with java and it's a
> summer class so it's going super fast (please be nice!) lol.
>
> Basically I started writing my code in one class, then I split it up
> into a Card class and a DeckOfCards class and I now need to figure out
> how to get it all to work together.  I get a little confused with
> calling methods sometimes, especially when separate classes are in play.
>   I think I just need a method to deal out five cards that also tells
> how many cards are left in the deck.  Then get it all working together
> correctly.  Also, I need a toString method but I honestly do not know
> how to go about that.  Any help is greatly appreciated!  If you could
> help explain things too that would be awesome!  I think I have
> everything SO FAR correct but I could be wrong and I'm sure there are
> better ways to write the code, I'll take any suggestions for a cleaner
> look too.  FYI, I think the prof would rather arrays then enums since
> we're dealing with arrays right now.
>
> Here are the directions...
> Design and implement a class called Card that represents a standard
> playing card. Each card has a suit and a face value. Then create a class
> called DeckOfCards that stores 52 objects of the Card class. Include
> methods to shuffle the deck, deal a card and report the number of cards
> left in the deck. The shuffle methods should assume a full deck. Create
> a driver class (CardsGame) with a main method that deals five cards from
> the shuffled deck, printing each card as it is dealt. Make sure to write
> the appropriate constructors, getters, setters, toString and other
> methods as required for both classes.
>
>
> The main class, CardsGame Class
>
>      import java.util.Scanner;
>
>      public class CardsGame {
>          public static void main (String [] args) {
>
>         }
>      }
>
> Card Class
>
>      class Card {
>         public static final int SPADE   = 4;
>         public static final int HEART   = 3;
>         public static final int CLUB    = 2;
>         public static final int DIAMOND = 1;
>
>         private int rank;
>         private int suit;
>         private static final String[] Suit = {"Hearts", "Clubs",
> "Spades", "Diamonds"};
>         private static final String[] Rank = {"Ace", "2", "3", "4", "5",
> "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
>
>         private int cardSuit;
>         private int cardRank;
>
>         public Card(int suit, int rank) {
   Better to throw an exception if suit or rank is outside their 
required range than to allow malformed instances.
>            if (rank == 1)
>               cardRank = 14;     // Give Ace the rank 14
>            else
>               cardRank = (int) rank;
>               cardSuit = (int) suit;
   Indentation does not match conditional execution.
>         }
>
>         public int suit() {
>            return this.cardSuit;
>         }
>
>         public String suitStr() {
>            return(this.Suit[ this.cardSuit ]);
>         }
>
>         public int rank() {
>            return this.cardRank;
>         }
>
>         public String rankStr() {
>            return ( Rank[ cardRank ] );
>         }
>
>
>         public String toString() {
>            return ( Rank[ cardRank ] + Suit[ cardSuit ] );
>         }
>      }
>
> DeckOfCards Class
>
>      class DeckOfCards {
>         public static final int NEWCARDS = 52;
   I suggest
           private static final Random CHANCE=new Random();
>         private Card[] deckOfCards;         // Contains all 52 cards
>         private int currentCard;            // deal THIS card in deck
>
>      public DeckOfCards( ) {
>        deckOfCards = new Card[NEWCARDS];
>        int i = 0;
>
>        for ( int suit = Card.DIAMOND; suit <= Card.SPADE; suit++ )
>           for ( int rank = 1; rank <= 13; rank++ )
>               deckOfCards[i++] = new Card(suit, rank);
>               currentCard = 0;
   Indented as if it were part of the for loop.
   I suggest using the braces for each and every if, else, for, while 
and do. You'll never regret it.
>       }
>
>
>         //shuffle(n): shuffle the deck
>         public void shuffle(int n) {
   Search for Fisher-Yates shuffling algorithm (sometimes misattributed 
to Knuth). There's no need for shuffle to have a parameter if it's 
intended to perform a random shuffle.
>            int i, j, k;
>            for ( k = 0; k < n; k++ ) {
>                i = (int) ( NEWCARDS * Math.random() );  // Pick 2 random
> cards
    java.util.Random is better for random integers. CHANCE.nextInt(n) 
returns a random integer in the range 0 .. n-1, which is what you'll want.
>                j = (int) ( NEWCARDS * Math.random() );  // in the deck?
>
>        //swap these randomly picked cards
>            Card temp = deckOfCards[i];
>            deckOfCards[i] = deckOfCards[j];
>            deckOfCards[j] = temp;
>         }
>        currentCard = 0;   // Reset current card to deal
>        }
   Don't you need something like
     Card deal() {
       if (currentCard==NEWCARDS) {
         throw IllegalStateException("All gone.");
       }
       return deckOfCards[currentCard++];
     }
>      }

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Help with Deck of cards assignment and getting it to deal... Scott Dunning <scottwd01@gmail.com> - 2014-08-01 18:16 -0700
  Re: Help with Deck of cards assignment and getting it to deal... Roedy Green <see_website@mindprod.com.invalid> - 2014-08-02 01:42 -0700
    Re: Help with Deck of cards assignment and getting it to deal... Martin Gregorie <martin@address-in-sig.invalid> - 2014-08-02 10:19 +0000
      Re: Help with Deck of cards assignment and getting it to deal... Roedy Green <see_website@mindprod.com.invalid> - 2014-08-03 15:54 -0700
        Re: Help with Deck of cards assignment and getting it to deal... Martin Gregorie <martin@address-in-sig.invalid> - 2014-08-04 13:00 +0000
          Re: Help with Deck of cards assignment and getting it to deal... Arne Vajhøj <arne@vajhoej.dk> - 2014-08-19 17:48 -0400
            Re: Help with Deck of cards assignment and getting it to deal... "Chris Uppal" <chris.uppal@metagnostic.REMOVE-THIS.org> - 2014-08-20 10:48 +0100
              Re: Help with Deck of cards assignment and getting it to deal... Arne Vajhøj <arne@vajhoej.dk> - 2014-09-07 21:42 -0400
              Re: Help with Deck of cards assignment and getting it to deal... Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-09-07 22:04 -0400
                Re: Help with Deck of cards assignment and getting it to deal... Arne Vajhøj <arne@vajhoej.dk> - 2014-09-07 22:08 -0400
  Re: Help with Deck of cards assignment and getting it to deal... Mike Amling <mamling@chaff.us> - 2014-08-03 07:38 -0500
  Re: Help with Deck of cards assignment and getting it to deal... Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-08-03 10:23 -0400
    Re: Help with Deck of cards assignment and getting it to deal... lipska the kat <lipskathekat@example.com> - 2014-08-03 18:52 +0100
      Re: Help with Deck of cards assignment and getting it to deal... Jeff Higgins <jeff@invalid.invalid> - 2014-08-03 14:16 -0400
        Re: Help with Deck of cards assignment and getting it to deal... lipska the kat <lipskathekat@example.com> - 2014-08-03 19:21 +0100
          Re: Help with Deck of cards assignment and getting it to deal... Jeff Higgins <jeff@invalid.invalid> - 2014-08-03 14:38 -0400
            Re: Help with Deck of cards assignment and getting it to deal... lipska the kat <lipskathekat@example.com> - 2014-08-03 19:41 +0100
    Re: Help with Deck of cards assignment and getting it to deal... Mike Amling <mamling@chaff.us> - 2014-08-03 16:05 -0500
  Re: Help with Deck of cards assignment and getting it to deal... Jeff Higgins <jeff@invalid.invalid> - 2014-08-03 11:08 -0400
    Re: Help with Deck of cards assignment and getting it to deal... Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-08-03 11:41 -0400
      Re: Help with Deck of cards assignment and getting it to deal... Jeff Higgins <jeff@invalid.invalid> - 2014-08-03 13:25 -0400
        Re: Help with Deck of cards assignment and getting it to deal... Jeff Higgins <jeff@invalid.invalid> - 2014-08-03 13:36 -0400
          Re: Help with Deck of cards assignment and getting it to deal... David Postill <david.postill@gmail.com> - 2014-08-04 09:00 +0200
        Re: Help with Deck of cards assignment and getting it to deal... Jeff Higgins <jeff@invalid.invalid> - 2014-08-03 13:47 -0400
          Re: Help with Deck of cards assignment and getting it to deal... Jeff Higgins <jeff@invalid.invalid> - 2014-08-03 13:52 -0400
            Re: Help with Deck of cards assignment and getting it to deal... Jeff Higgins <jeff@invalid.invalid> - 2014-08-03 14:10 -0400
        Re: Help with Deck of cards assignment and getting it to deal... Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-08-03 15:00 -0400
          Re: Help with Deck of cards assignment and getting it to deal... Arne Vajhøj <arne@vajhoej.dk> - 2014-08-03 15:26 -0400
            Re: Help with Deck of cards assignment and getting it to deal... lipska the kat <lipskathekat@example.com> - 2014-08-03 20:34 +0100
            Re: Help with Deck of cards assignment and getting it to deal... Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-08-03 16:49 -0400
              Re: Help with Deck of cards assignment and getting it to deal... Arne Vajhøj <arne@vajhoej.dk> - 2014-08-03 17:01 -0400
                Re: Help with Deck of cards assignment and getting it to deal... "Chris Uppal" <chris.uppal@metagnostic.REMOVE-THIS.org> - 2014-08-04 20:32 +0100
                Re: Help with Deck of cards assignment and getting it to deal... Arne Vajhøj <arne@vajhoej.dk> - 2014-08-19 17:45 -0400
                Re: Help with Deck of cards assignment and getting it to deal... "Chris Uppal" <chris.uppal@metagnostic.REMOVE-THIS.org> - 2014-08-20 10:50 +0100
                Re: Help with Deck of cards assignment and getting it to deal... Brixomatic <brixomatic@yahoo.com> - 2014-08-31 10:17 +0200
                Re: Help with Deck of cards assignment and getting it to deal... "Chris Uppal" <chris.uppal@metagnostic.REMOVE-THIS.org> - 2014-09-04 15:14 +0100
                Re: Help with Deck of cards assignment and getting it to deal... Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-09-04 10:55 -0400
                Re: Help with Deck of cards assignment and getting it to deal... "Chris Uppal" <chris.uppal@metagnostic.REMOVE-THIS.org> - 2014-09-04 16:12 +0100
                [OT] Re: Help with Deck of cards assignment and getting it to deal... Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-09-04 15:05 -0400
                Re: Help with Deck of cards assignment and getting it to deal... Gene Wirchenko <genew@telus.net> - 2014-09-05 10:03 -0700
                Re: Help with Deck of cards assignment and getting it to deal... Arne Vajhøj <arne@vajhoej.dk> - 2014-09-07 21:45 -0400
                Re: Help with Deck of cards assignment and getting it to deal... Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-09-07 22:13 -0400
              Re: Help with Deck of cards assignment and getting it to deal... Jeff Higgins <jeff@invalid.invalid> - 2014-08-03 17:19 -0400
                Re: Help with Deck of cards assignment and getting it to deal... Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-08-04 08:14 -0400
                Re: Help with Deck of cards assignment and getting it to deal... Jeff Higgins <jeff@invalid.invalid> - 2014-08-04 20:03 -0400
                Re: Help with Deck of cards assignment and getting it to deal... Jeff Higgins <jeff@invalid.invalid> - 2014-08-04 20:29 -0400
          Re: Help with Deck of cards assignment and getting it to deal... Jeff Higgins <jeff@invalid.invalid> - 2014-08-03 15:45 -0400
            Re: Help with Deck of cards assignment and getting it to deal... Jeff Higgins <jeff@invalid.invalid> - 2014-08-03 16:01 -0400
              Re: Help with Deck of cards assignment and getting it to deal... Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-08-03 17:05 -0400
                Re: Help with Deck of cards assignment and getting it to deal... Jeff Higgins <jeff@invalid.invalid> - 2014-08-03 17:37 -0400
                Re: Help with Deck of cards assignment and getting it to deal... Jeff Higgins <jeff@invalid.invalid> - 2014-08-03 18:19 -0400
                Re: Help with Deck of cards assignment and getting it to deal... Jeff Higgins <jeff@invalid.invalid> - 2014-08-03 18:34 -0400
                Re: Help with Deck of cards assignment and getting it to deal... Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-08-03 20:37 -0400
                Re: Help with Deck of cards assignment and getting it to deal... Jeff Higgins <jeff@invalid.invalid> - 2014-08-03 20:40 -0400
                Re: Help with Deck of cards assignment and getting it to deal... Jeff Higgins <jeff@invalid.invalid> - 2014-08-03 20:49 -0400
                Re: Help with Deck of cards assignment and getting it to deal... Jeff Higgins <jeff@invalid.invalid> - 2014-08-03 18:09 -0400
  Re: Help with Deck of cards assignment and getting it to deal... Roedy Green <see_website@mindprod.com.invalid> - 2014-08-04 18:43 -0700
    Re: Help with Deck of cards assignment and getting it to deal... "Chris Uppal" <chris.uppal@metagnostic.REMOVE-THIS.org> - 2014-08-05 11:19 +0100
      Re: Help with Deck of cards assignment and getting it to deal... "Chris Uppal" <chris.uppal@metagnostic.REMOVE-THIS.org> - 2014-08-05 11:28 +0100
  Re: Help with Deck of cards assignment and getting it to deal... lipska the kat <lipskathekat@example.com> - 2014-08-31 17:31 +0100

csiph-web