deck class constructor

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blehhhhh
    New Member
    • May 2010
    • 13

    deck class constructor

    So I'm trying to work on this problem of deck of cards wherein I have a few classes along with a tester class. Everything else is done. I just need help with implementing the methods properly. Here's how the thing goes:

    I'm given the classes Suit.java, PlayingCard.jav a, FaceCard.java, NumberCard.java , Deck.java, CardTest.java

    Code:
    //Suit.java
    
    public class Suit {
        private Suit() { super(); }
        public final static Suit Clubs = new Suit();
        public final static Suit Diamonds = new Suit();
        public final static Suit Hearts = new Suit();
        public final static Suit Spades = new Suit();
        public String toString() {
            if (this == Clubs) return "Clubs";
            else if (this == Diamonds) return "Diamonds";
            else if (this == Hearts) return "Hearts";
            else if (this == Spades) return "Spades";
            else throw new IllegalStateException();
        }
    }
    
    // PlayingCard.java
    
    public abstract class PlayingCard {
        private int myRank;
        private Suit mySuit;
    
        public PlayingCard(int r, Suit s) {
            if (r < 2 || r > 14 || s == null) {
                throw new IllegalArgumentException();
            }
            this.myRank = r;
            this.mySuit = s;
        }
    
        public abstract String toString();
    
        public int getRank() {
            return this.myRank;
        }
    
        public Suit getSuit() {
           return this.mySuit;
        }
    
        public boolean equals(Object other) {
            return this.getRank() == ((PlayingCard)other).getRank()
                && this.getSuit() == ((PlayingCard)other).getSuit();
        }
    
    }
    
    //FaceCard.java
    
    public class FaceCard extends PlayingCard {
    
        public FaceCard(int r, Suit s) {
            super(r, s);
            if (r <= 10) {
                throw new IllegalArgumentException();
            }
        }
    
        public String toString() {
            return "" + "JQKA".charAt(this.getRank() - 11)
                + this.getSuit().toString().charAt(0);
        }
    
    }
    
    //NumberCard.java
    
    public class NumberCard extends PlayingCard {
    
        public NumberCard(int r, Suit s) {
            super(r, s);
            if (r > 10) {
                throw new IllegalArgumentException();
            }
        }
    
        public String toString() {
            return "" + this.getRank() + this.getSuit().toString().charAt(0);
        }
    }
    
    //Deck.java
    
    import java.util.NoSuchElementException;
    import java.util.Random;
    /**
     * Class representing a deck of playing cards
     *
     * @author [[put your name here]]
     */
    public class Deck {
      private int count;            // how many cards are currently in this deck
        private PlayingCard[] cards;  // the cards themselves
    /*...*/	private int SWAPS = 999999;
        /**
         * Constructor for Deck class -- the constructed deck will contain
         * 52 cards:  all the clubs on top, followed by all the diamonds,
         * followed by all the hearts, followed by all the spades.  Each
         * suit is in order from the 2 to the ace, with the 2 on top.
         * Thus, after construction, the top card is the 2 of clubs and
         * the bottom card is the ace of spades.
         */
    	    public Deck() {
    /*...*/		String [] suits =    {"SPADES","DIAMONDS","CLUBS","HEARTS"}; 
    		/*...*/ for ( int suit = 0; suit <= 3; suit++ ) { 
    			/*....*/for ( int rank = 2; rank <= 14;rank++ ) {
    /*...*/			 count = 0;
    /*...*/			  cards [count] = abstract PlayingCard(rank,suit);
    			   /*...*/count ++; 
    /*...*/			}
    /*...*/		 }  
    /*...*/
    		    count = 52;  
    			 
        }
    
        /**
         * Shuffle the deck -- rearrange the cards in the deck into a
         * random order.
         */
        public void shuffle() {
            /*...*/  Random gen = new Random();
          /*...*/PlayingCard temp;
          /*...*/int position1, position2;
    
          /*...*/for (int i=1; i <= SWAPS; i++)
       /*...*/   {
       /*...*/      position1 = gen.nextInt(52);
       /*...*/      position2 = gen.nextInt(52);
    
       /*...*/      temp = cards[position1];
       /*...*/      cards[position1] = cards[position2];
       /*...*/      cards[position2] = temp;
       /*...*/   }
    
        }
    
        /**
         * Report the number of cards left in the deck
         */
        public int getNumCardsLeft() {
            /*...*/return count;
        }
    
        /**    
    	  * Report on which card is on top of the deck
         * (the deck remains unchanged)
         */
        //public PlayingCard peekAtTopCard() {
          //  ...
        //}
    
        /**
         * Remove the top card from the deck, and return the removed card
         * (there is now one card fewer in the deck than before the call)
    
         */
        public PlayingCard dealNextCard() {
       /*...*/     if (count > 0){
       /*...*/      return cards[--count];
       /*...*/   } else {
       /*...*/      return null;
    	     }
        }
    }
    	public class CardTest {
    	public static void main(String[] args) {
    		Deck d = new Deck();
    		System.out.println(d.getNumCardsLeft()+ " cards left in deck.");
    		for (int i = 0; i<4; i++) {
    			for (int j = 0; j<13; j++) {
    				System.out.print(d.DealNextCard()+ " ");
    				}
    				System.out.println();
    			}
    			System.out.println(d.getNumCardsLeft() + "cards left in the deck.");
    		}
    	}
    What I have to do is implement the methods shuffle, dealNextCard, peekAtTopCard, getNumCardsLeft and also the constructor. I have worked my way through shuffle, dealNextCard and numCardsLeft methods I guess. Now i don't understand how do I go about the constructor class. What i need is that the constructor must initialize the deck to contain exactly 52 cards,
    13 cards in each of four suits (hearts, clubs, diamonds, spades). Each
    suit has nine number cards, with rank values of 2–10, inclusive. The
    remaining four cards in each suit are face cards: jack, queen, king,
    and ace (having rank values of 11–14, respectively). Each suit must
    be arranged in order, from 2 to ace (with the 2 on top and the ace
    on the bottom). The suits are to be arranged as follows (from top to
    bottom): clubs, diamonds, hearts, spades. Thus, the top card on the
    deck will be the 2 of clubs, the bottom card the ace of spades.

    I can add extra datafields if i wish to but i gotta initialise it in the constructor. and it should be declared private. The CardTest class works to check if the methods DealNextCard and getnumcardsleft is implemented properly.

    the things beginning with /*....*/ in the constructor is my work. Also u can check the other methods which are also marke by the /*....*/ for the chnages i made, For time being i didn't implement the peekattop card method. When I use the code above, i get the following error:
    Deck.java:25: PlayingCard is abstract; cannot be instantiated
    cards [count] = new PlayingCard(ran k,suit);
    ^
    1 error

    I hope whatever i wrote makes some sense and i get some help. Any kind of help would be appreciated
    Thanks
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    1. Make PlayingCard not abstract.

    2. Your constructor is
    public NumberCard(int r, Suit s) {
    so when you construct the deck, the 2nd argument should be a suit. Right now it is:
    cards [count] = abstract PlayingCard(ran k,suit);
    I think you want:
    cards [count] = abstract PlayingCard(ran k,suits[suit]);

    3. but you have to change the order of the suits.

    4. Further, move the count initialization before the for loops, otherwise you'll always only be setting cards[0], and not the whole deck.

    Comment

    • blehhhhh
      New Member
      • May 2010
      • 13

      #3
      Ok so i did the changes u asked me to..it stilll shows me the following error:
      Deck.java:26: illegal start of expression
      cards [count] = abstract PlayingCard(ran k,Suit[suits]);
      ^
      1 error
      the arrow is pointed at a of the abstract and the word cards comes before[count]

      here's how my program looks like now

      Code:
       public Deck() {
      		String [] suits = {"Clubs","Diamonds","Hearts","Spades"};
      		 count = 0;
       
      		for ( int suit = 0; suit <= 3; suit++ ) { 
      			for ( int rank = 2; rank <= 14;rank++ ) {
      						 cards [count] = abstract PlayingCard(rank,Suit[suits]);
      
      
      			   count ++; 
      			}
      		 }  
      
      		    count = 52;  
      			 
          }
      I cant remove the abstract part because its a part of playing card class and m not supposed to make changes to that class.

      Comment

      • blehhhhh
        New Member
        • May 2010
        • 13

        #4
        I think the removing abstract part is done....but i have another issue. That construction part still doesnt work.

        this is the error that shows
        Deck.java:26: cannot find symbol
        symbol : variable Suit
        location: class Deck
        cards [count] = PlayingCard( rank,Suit[suits]);
        ^
        Deck.java:26: incompatible types
        found : java.lang.Strin g[]
        required: int
        cards [count] = PlayingCard( rank,Suit[suits]);
        ^
        2 errors

        cards [count] = PlayingCard( rank,Suit[suits]);

        and this is the change i made. plzzz help!!!

        Comment

        • jkmyoung
          Recognized Expert Top Contributor
          • Mar 2006
          • 2057

          #5
          Sorry, I didn't notice a 2nd quick mistake with your suits declaration earlier: you want the actual Suit, not the strings.

          Suit[] suits = {Suit.Clubs,Sui t.Diamonds,Suit .Hearts,Suit.Sp ades}


          "I think you want:
          cards [count] = abstract PlayingCard(ran k,suits[suit]);"

          Comment

          • blehhhhh
            New Member
            • May 2010
            • 13

            #6
            not working..if i write this " cards [count] = abstract PlayingCard( rank,suits[Suit]); "
            this is the error:
            Deck.java:29: illegal start of expression
            cards [count] = abstract PlayingCard( rank,suits[Suit]);
            ^
            1 error

            if i remove the abstract, this is the error

            Deck.java:24: incompatible types
            found : java.lang.Strin g
            required: Suit
            Suit[] suits = {"Clubs","Diamo nds","Hearts"," Spades"};
            ^
            Deck.java:24: incompatible types
            found : java.lang.Strin g
            required: Suit
            Suit[] suits = {"Clubs","Diamo nds","Hearts"," Spades"};
            ^
            Deck.java:24: incompatible types
            found : java.lang.Strin g
            required: Suit
            Suit[] suits = {"Clubs","Diamo nds","Hearts"," Spades"};
            ^
            Deck.java:24: incompatible types
            found : java.lang.Strin g
            required: Suit
            Suit[] suits = {"Clubs","Diamo nds","Hearts"," Spades"};
            ^
            Deck.java:29: cannot find symbol
            symbol : variable Suit
            location: class Deck
            cards [count] = PlayingCard( rank,suits[Suit]);
            ^
            Deck.java:29: cannot find symbol
            symbol : method PlayingCard(int ,Suit)
            location: class Deck
            cards [count] = PlayingCard( rank,suits[Suit]);
            ^
            6 errors

            Comment

            • jkmyoung
              Recognized Expert Top Contributor
              • Mar 2006
              • 2057

              #7
              suits[suit] not suits[Suit].
              suit, the integer. Is problematic that you've assigned a local variable pretty much the same name as the class.

              You cannot assign to suits based on a string. The previous comment states Suit.Clubs, etc.. to get the actual class member that you've declared earlier.

              Comment

              • blehhhhh
                New Member
                • May 2010
                • 13

                #8
                that aint working either
                i don't know whats the problem with this line :
                Suit[] suits = (Clubs,Diamonds ,Hearts,Spades) ;
                This error shows up as ssoon as i write this:
                Deck.java:24: ')' expected
                Suit[] suits = (Clubs,Diamonds ,Hearts,Spades) ;
                ^
                Deck.java:24: ';' expected
                Suit[] suits = (Clubs,Diamonds ,Hearts,Spades) ;
                ^
                2 errors

                Comment

                • jkmyoung
                  Recognized Expert Top Contributor
                  • Mar 2006
                  • 2057

                  #9
                  Clubs, Diamonds, Hearts and Spades are all defined explicitly in the Suit class. The Deck class has no idea what these are; you need to reference them like
                  Suit.Clubs, etc.
                  I suggest a repost of that function after you make the change, just to see if there are any surrounding problems. Please use [ code ] tags [ / code] (the # sign).

                  Comment

                  • blehhhhh
                    New Member
                    • May 2010
                    • 13

                    #10
                    I'm sorry but i have no idea how do we go about the reference part. If u could gimme a hint regarding that...
                    thanks

                    Comment

                    • jkmyoung
                      Recognized Expert Top Contributor
                      • Mar 2006
                      • 2057

                      #11

                      Comment

                      • blehhhhh
                        New Member
                        • May 2010
                        • 13

                        #12
                        ???

                        here's the whole program along with the realted classes

                        Code:
                        //Suit.java
                        
                        public class Suit {
                            private Suit() { super(); }
                            public final static Suit Clubs = new Suit();
                            public final static Suit Diamonds = new Suit();
                            public final static Suit Hearts = new Suit();
                            public final static Suit Spades = new Suit();
                            public String toString() {
                                if (this == Clubs) return "Clubs";
                                else if (this == Diamonds) return "Diamonds";
                                else if (this == Hearts) return "Hearts";
                                else if (this == Spades) return "Spades";
                                else throw new IllegalStateException();
                            }
                        }
                        
                        //FaceCard.java
                        
                        public class FaceCard extends PlayingCard {
                        
                            public FaceCard(int r, Suit s) {
                                super(r, s);
                                if (r <= 10) {
                                    throw new IllegalArgumentException();
                                }
                            }
                        
                            public String toString() {
                                return "" + "JQKA".charAt(this.getRank() - 11)
                                    + this.getSuit().toString().charAt(0);
                            }
                        
                        }
                        
                        //NumberCard.java
                        
                        	public class NumberCard extends PlayingCard {
                        	
                        	    public NumberCard(int r, Suit s) {
                        	        super(r, s);
                        	        if (r > 10) {
                        	            throw new IllegalArgumentException();
                        	        }
                        	    }
                        	
                        	    public String toString() {
                        	        return "" + this.getRank() + this.getSuit().toString().charAt(0);
                        	    }
                        	}
                        	
                        	
                        
                        //PlayingCard.java
                        
                        public abstract class PlayingCard {
                            private int myRank;
                            private Suit mySuit;
                        
                            public PlayingCard(int r, Suit s) {
                                if (r < 2 || r > 14 || s == null) {
                                    throw new IllegalArgumentException();
                                }
                                this.myRank = r;
                                this.mySuit = s;
                            }
                        
                            public abstract String toString();
                        
                            public int getRank() {
                                return this.myRank;
                            }
                        
                            public Suit getSuit() {
                               return this.mySuit;
                            }
                        
                            public boolean equals(Object other) {
                                return this.getRank() == ((PlayingCard)other).getRank()
                                    && this.getSuit() == ((PlayingCard)other).getSuit();
                            }
                        
                        }
                        
                        
                        //Deck.java
                        
                        import java.util.NoSuchElementException;
                        import java.util.Random;
                        /**
                         * Class representing a deck of playing cards
                         *
                         * @author [[put your name here]]
                         */
                        public class Deck {
                          private int count;            // how many cards are currently in this deck
                            private PlayingCard[] cards;  // the cards themselves
                        /*...*/	private int SWAPS = 999999;
                            /**
                             * Constructor for Deck class -- the constructed deck will contain
                             * 52 cards:  all the clubs on top, followed by all the diamonds,
                             * followed by all the hearts, followed by all the spades.  Each
                             * suit is in order from the 2 to the ace, with the 2 on top.
                             * Thus, after construction, the top card is the 2 of clubs and
                             * the bottom card is the ace of spades.
                             */
                        	    public Deck() {
                        		 cards = new PlayingCard[52]; 
                        	 count = 0;
                        
                        		Suit[] suits = Suit.Clubs();
                        				Suit[] suits = Suit.Diamonds();
                        						Suit[] suits = Suit.Hearts();
                        								Suit[] suits = Suit.Spades();
                        	//****	 count = 0;
                         
                        		for ( int suit = 0; suit <= 3; suit++ ) { 
                        			for ( int rank = 2; rank <= 14;rank++ ) {
                        						 cards [count] = PlayingCard( rank,suits[suit]);
                        
                        
                        			   count ++; 
                        			}
                        		 }  
                        
                        		  // *** count = 52;  
                        			 
                            }
                        
                            /**
                             * Shuffle the deck -- rearrange the cards in the deck into a
                             * random order.
                             */
                            public void shuffle() {
                                /*...*/  Random gen = new Random();
                              /*...*/PlayingCard temp;
                              /*...*/int position1, position2;
                        
                              /*...*/for (int i=1; i <= SWAPS; i++)
                           /*...*/   {
                           /*...*/      position1 = gen.nextInt(52);
                           /*...*/      position2 = gen.nextInt(52);
                        
                           /*...*/      temp = cards[position1];
                           /*...*/      cards[position1] = cards[position2];
                           /*...*/      cards[position2] = temp;
                           /*...*/   }
                        
                            }
                        
                            /**
                             * Report the number of cards left in the deck
                             */
                            public int getNumCardsLeft() {
                                /*...*/return count;
                            }
                        
                            /**    
                        	  * Report on which card is on top of the deck
                             * (the deck remains unchanged)
                             */
                            //public PlayingCard peekAtTopCard() {
                              //  ...
                            //}
                        
                            /**
                             * Remove the top card from the deck, and return the removed card
                             * (there is now one card fewer in the deck than before the call)
                        
                             */
                            public PlayingCard dealNextCard() {
                           /*...*/     if (count > 0){
                           /*...*/      return cards[--count];
                           /*...*/   } else {
                           /*...*/      return null;
                        	     }
                            }
                        }
                        I badly need help with this!!!

                        Comment

                        • jkmyoung
                          Recognized Expert Top Contributor
                          • Mar 2006
                          • 2057

                          #13
                          Does your browser not move your window to the right spot?
                          Reread posts #2 and #5 again.

                          Comment

                          • blehhhhh
                            New Member
                            • May 2010
                            • 13

                            #14
                            I'm really sorry but i actually get it now! But there's this error again!!

                            Deck.java:31: PlayingCard is abstract; cannot be instantiated
                            cards [count] = new PlayingCard( rank,suits[suit]);
                            ^
                            1 error

                            here's d deck condtructor again!

                            Code:
                            public Deck() {
                            		 cards = new PlayingCard[52]; 
                            	 count = 0;
                            
                            	Suit[] suits = {Suit.Clubs,Suit.Diamonds,Suit.Hearts,Suit.Spades};
                            
                            
                            		//****	 count = 0;
                             
                            		for ( int suit = 0; suit <= 3; suit++ ) { 
                            			for ( int rank = 2; rank <= 14;rank++ ) {
                            						 cards [count] =  PlayingCard( rank,suits[suit]);
                            
                            
                            			   count ++; 
                            			}
                            		 }

                            Comment

                            • jkmyoung
                              Recognized Expert Top Contributor
                              • Mar 2006
                              • 2057

                              #15
                              Code:
                              public class Card extends PlayingCard {
                                   public String toString(){
                                  // your code here
                                  }
                              Replace all instantiations of PlayingCard with Card

                              Comment

                              Working...