By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,812 Members | 856 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,812 IT Pros & Developers. It's quick & easy.

Need help with a deck of cards homework!

P: 6
I have been at this programme for hours trying to work out what is wrong. Any help would be very much appricated. Here is the breif I received.

The program
This week you are going to write three classes: Card.java, Deck.java and DeckTester.java. The specification for each class is given below.

Card.Java
This is a simple class that represents a playing card.

Card has two attributes:
• rank which is a String that represents the value of a card. It takes the values “ACE,”2”,”3”,…,”JACK”,”QUEEN”,”KING”; and
• suit a String which takes the values “SPADES”,”DIAMONDS”,”CLUBS”,”HEARTS”.

The class has a single constructor which takes two parameters: the first is an int that represents the rank in the range 1 to 13; the second a String representing the suit. The constructor must convert the int into an appropriate String value.

Additionally there are two methods that return String representations of the suit and rank respectively.

A skeleton of the class is provided. You have to complete the missing components. This file can be found on Blackboard with the homework specification.

Deck.java
This class represents a deck of playing cards.

This class has two attributes: an array of type Card holding the 52 cards of the deck; an int that holds the number of cards in the deck, (this attribute is not used in this homework but may be used later);

A skeleton of the class is also provided. You have to complete the missing components. This file can be found on Blackboard with the homework specification.

DeckTester.java
This class contains the main method. Its purpose is to allow you to demonstrate that your deck class works as anticipated. In particular you need to:
• Create an instance of the Deck class;
• Confirm that the constructor works correctly;
• Confirm that shuffle works correctly.


My tutor gave me a little bit of the code and some comments on where to fill in the missing code. So far I have this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. public class Deck
  3. {
  4.     private Card [] deck;
  5.     private int numberOfCards;
  6.  
  7.     /**
  8.      * Creates a deck of 52 playing cards
  9.      * The deck is set up in the order SPADES, DIAMONDS, CLUBS,HEARTS
  10.      */
  11.     public Deck()
  12.     {
  13.         String [] suits = {"SPADES","DIAMONDS","CLUBS","HEARTS"};
  14.  
  15.         for ( int suit = 0; suit <= 3; suit++ )
  16.         {
  17.             for ( int rank = 1; rank <= 13;rank++ )
  18.             {
  19.                 numberOfCards = 0;
  20.                 deck [numberOfCards] = new Card(rank,suit);
  21.                 numberOfCards ++;
  22.             }
  23.         }
  24.  
  25.        numberOfCards = 52;
  26.  
  27.     }
  28.  
  29.     /**
  30.      * shuffles the deck of cards.
  31.      * 
  32.      */
  33.   /*  public void shuffle()
  34.     {
  35.         Card temp;        // Temporary object to hold value whilst swapping
  36.  
  37.          //loop through each card in the deck and swap it with some random card.
  38.          //FOR EACH CARD IN THE DECK
  39.          {
  40.                  int rand = (int)(Math.random()*52);
  41.                  //TAKE THE CURRENT CARD...
  42.                  //AND SWAP IT WITH THE RAND CARD...
  43.            }
  44.       }  
  45.  
  46.     /**
  47.      * Returns a representation of the deck as a string
  48.      * one card per line
  49.      * @return     the deck as a String
  50.      */
  51.     public String toString()
  52.     {
  53.         String deckString = "New deck created ";
  54.         for (int cards =0; cards <52; cards++)
  55.         {
  56.             deckString = deckString + deck[cards] + "\n";
  57.  
  58.         }
  59.         System.out.println(deckString);
  60.         return deckString;
  61.     }
  62. }
  63.  
  64.  
  65. ----------------------------------------------------------------------------------------------
  66. /**
  67.  * This class represents a simple playing card.
  68.  * The attributes of the card are both of type String:
  69.  *   rank - takes values ACE,1,2,3,.., JACK, QUEEN, KING
  70.  *   suit - takes values SPADES, DIAMONDS,CLUBS, HEARTS
  71. */
  72. public class Card 
  73. {
  74.  
  75.     private String rank;
  76.     private String suit;
  77.  
  78.     /*Constructor for objects of class Card
  79.      * Each card has a rank and a suit.   No checks are made for invalid values
  80.      * @param  rank   an int representing the valus of the card, range 1..13
  81.      * @param  suit   a String representing the suit of card: values                      
  82.               "SPADES","DIAMONDS","CLUBS","HEARTS"
  83.      */
  84.     public Card(int rank, int suit)
  85.     {
  86.         if(suit == 0)
  87.         {
  88.             this.suit = "Spades";
  89.         }
  90.         else if(suit == 1)
  91.         {
  92.             this.suit = "Diamonds";
  93.         }
  94.         else if(suit ==2)
  95.         {
  96.             this.suit = "Clubs";
  97.         }
  98.         else
  99.         {
  100.             this.suit = "Hearts";
  101.         }
  102.  
  103.  
  104.         // must convert the integer value of rank to an appropriate String
  105.         if (rank == 1)
  106.         {
  107.             this.rank = "Ace";
  108.         }
  109.         else if (rank == 11)
  110.         {
  111.             this.rank = "Jack";
  112.         }
  113.         else if (rank == 12)
  114.         {
  115.             this.rank = "Queen";
  116.         }
  117.         else if (rank == 13)
  118.         {
  119.             this.rank = "King";
  120.         }
  121.         else
  122.         {
  123.             this.rank = "" + rank;
  124.           }
  125.     }
  126.     public String getSuit() 
  127.     {
  128.         return suit;
  129.     }
  130.     public String getRank() 
  131.     {
  132.         return rank;
  133.     }
  134.  
  135. }
  136. -------------------------------------------------------------------------------
  137. public class DeckTester
  138. {
  139.     public static void main(String[] args)
  140.     {
  141.         Deck deck1 = new Deck();;
  142.         System.out.println(deck1.toString());
  143.  
  144.     }
  145. }
  146.  
I know I have not entered the code yet to shuffle the cards but I know how to do this and will add the code in later I just want to get the cards printing out in order first.

The project complies properly but when I run it I get this error:

Exception in thread "main" java.lang.NullPointerException
at Deck.<init>(Deck.java:20)
at DeckTester.main(DeckTester.java:5)
Press any key to continue...

I realise there is a problem at line 20 in the Deck class and line 5 in the DeckTester Class. or the problem is elsewhere causing these too problems. But I have sat here for hours trying to work out what is wrong and I cant see anything.

Yet again any help would be much appricated.
Mar 1 '08 #1
Share this Question
Share on Google+
8 Replies


Expert 100+
P: 849
You never initialize your deck array in the constructor, so it doesn't have a size or any contents. As such, the first time you try to access it, you get the NullPointerException. Add a statement "deck = new Card[size of the deck];" and then just make sure that you don't exceed the bounds of the array anywhere in your code to avoid these errors in the future.
Mar 1 '08 #2

100+
P: 539
what is
Expand|Select|Wrap|Line Numbers
  1. private Card [] deck;
@ line 3?
Can you discuss it for me? Why you just allocate it without initializing?

It looks familiar to me... Maybe you don't initialized it....

Wondering,
Sukatoa
Mar 1 '08 #3

P: 6
You never initialize your deck array in the constructor, so it doesn't have a size or any contents. As such, the first time you try to access it, you get the NullPointerException. Add a statement "deck = new Card[size of the deck];" and then just make sure that you don't exceed the bounds of the array anywhere in your code to avoid these errors in the future.
would I add that in to the card constructor? or too the deck class?
Mar 1 '08 #4

P: 6
what is
Expand|Select|Wrap|Line Numbers
  1. private Card [] deck;
@ line 3?
Can you discuss it for me? Why you just allocate it without initializing?

It looks familiar to me... Maybe you don't initialized it....

Wondering,
Sukatoa

It was given to me in the skelton of the code from my tutor. I have no Idea what it does
Mar 1 '08 #5

P: 6
would I add that in to the card constructor? or too the deck class?
Ahhh Ive worked out where that goes thanks! Now however I have a different problem the string prints out giberish! any ideas?
Mar 1 '08 #6

P: 6
Right so I am still at this I have updated the card and deck class they now look like this
Expand|Select|Wrap|Line Numbers
  1.  
  2. public class Deck
  3. {
  4.     private Card [] deck;
  5.     private int numberOfCards;
  6.  
  7.     /**
  8.      * Creates a deck of 52 playing cards
  9.      * The deck is set up in the order SPADES, DIAMONDS, CLUBS,HEARTS
  10.      */
  11.     public Deck()
  12.     {
  13.         deck = new Card[52];
  14.         numberOfCards = 0;
  15.         String [] suits = {"SPADES","DIAMONDS","CLUBS","HEARTS"};
  16.         for ( int suit = 0; suit <= 3; suit++ )
  17.         {
  18.             for ( int rank = 1; rank <= 13;rank++ )
  19.             {
  20.  
  21.                 deck [numberOfCards] = new Card(rank,suits[suit]);
  22.                 numberOfCards ++;
  23.             }
  24.         }
  25.  
  26.  
  27.  
  28.     }
  29.  
  30.     /**
  31.      * shuffles the deck of cards.
  32.      * 
  33.      */
  34.   /*  public void shuffle()
  35.     {
  36.         Card temp;        // Temporary object to hold value whilst swapping
  37.  
  38.          //loop through each card in the deck and swap it with some random card.
  39.          //FOR EACH CARD IN THE DECK
  40.          {
  41.                  int rand = (int)(Math.random()*52);
  42.                  //TAKE THE CURRENT CARD...
  43.                  //AND SWAP IT WITH THE RAND CARD...
  44.            }
  45.       }  
  46.  
  47.     /**
  48.      * Returns a representation of the deck as a string
  49.      * one card per line
  50.      * @return     the deck as a String
  51.      */
  52.     public String toString()
  53.     {
  54.         String deckString = "New deck created\n";
  55.         for (int cards =0; cards <=51; cards++)
  56.         {
  57.             deckString = deckString + deck[cards] + "\n";
  58.         }
  59.         return deckString;
  60.     }
  61. }
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. /**
  71.  * This class represents a simple playing card.
  72.  * The attributes of the card are both of type String:
  73.  *   rank - takes values ACE,1,2,3,.., JACK, QUEEN, KING
  74.  *   suit - takes values SPADES, DIAMONDS,CLUBS, HEARTS
  75. */
  76. public class Card 
  77. {
  78.  
  79.     private String rank;
  80.     private String suit;
  81.     // MISSING CODE
  82.  
  83.     /*Constructor for objects of class Card
  84.      * Each card has a rank and a suit.   No checks are made for invalid values
  85.      * @param  rank   an int representing the valus of the card, range 1..13
  86.      * @param  suit   a String representing the suit of card: values                      
  87.               "SPADES","DIAMONDS","CLUBS","HEARTS"
  88.      */
  89.     public Card(int rank, String suits)
  90.     {       
  91.         this.suit = suits;
  92.  
  93.  
  94.         // must convert the integer value of rank to an appropriate String
  95.         if (rank == 1)
  96.         {
  97.             this.rank = "Ace";
  98.         }
  99.         else if (rank == 11)
  100.         {
  101.             this.rank = "Jack";
  102.         }
  103.         else if (rank == 12)
  104.         {
  105.             this.rank = "Queen";
  106.         }
  107.         else if (rank == 13)
  108.         {
  109.             this.rank = "King";
  110.         }
  111.         else
  112.         {
  113.             this.rank = "" + rank;
  114.           }
  115.     }
  116.     public String getSuit() 
  117.     {
  118.         return suit;
  119.     }
  120.     public String getRank() 
  121.     {
  122.         return rank;
  123.     }
  124.     public String toString() //add to the .toString method to show all attributes of rectangle
  125.     {
  126.         String deckString = new String (rank + " " + suit + super.toString());
  127.         return deckString;
  128.     }
  129.  
  130. }
  131.  
now I am getting an output with the rank and suit of the card however I also get something else afterwards that I cant get rid of which is this:

New deck created
Ace SPADESCard@82ba41
2 SPADESCard@923e30
3 SPADESCard@130c19b
4 SPADESCard@1f6a7b9
5 SPADESCard@7d772e
6 SPADESCard@11b86e7
7 SPADESCard@35ce36
8 SPADESCard@757aef
9 SPADESCard@d9f9c3
10 SPADESCard@9cab16
Jack SPADESCard@1a46e30
Queen SPADESCard@3e25a5
King SPADESCard@19821f
Ace DIAMONDSCard@addbf1
2 DIAMONDSCard@42e816
3 DIAMONDSCard@9304b1
4 DIAMONDSCard@190d11
5 DIAMONDSCard@a90653
6 DIAMONDSCard@de6ced
7 DIAMONDSCard@c17164
8 DIAMONDSCard@1fb8ee3
9 DIAMONDSCard@61de33
10 DIAMONDSCard@14318bb
Jack DIAMONDSCard@ca0b6
Queen DIAMONDSCard@10b30a7
King DIAMONDSCard@1a758cb
Ace CLUBSCard@1b67f74
2 CLUBSCard@69b332
3 CLUBSCard@173a10f
4 CLUBSCard@530daa
5 CLUBSCard@a62fc3
6 CLUBSCard@89ae9e
7 CLUBSCard@1270b73
8 CLUBSCard@60aeb0
9 CLUBSCard@16caf43
10 CLUBSCard@66848c
Jack CLUBSCard@8813f2
Queen CLUBSCard@1d58aae
King CLUBSCard@83cc67
Ace HEARTSCard@e09713
2 HEARTSCard@de6f34
3 HEARTSCard@156ee8e
4 HEARTSCard@47b480
5 HEARTSCard@19b49e6
6 HEARTSCard@10d448
7 HEARTSCard@e0e1c6
8 HEARTSCard@6ca1c
9 HEARTSCard@1bf216a
10 HEARTSCard@12ac982
Jack HEARTSCard@1389e4
Queen HEARTSCard@c20e24
King HEARTSCard@2e7263

Press any key to continue...

any ideas on how to get rid of that?
Mar 1 '08 #7

Expert 10K+
P: 11,448
public String toString() //add to the .toString method to show all attributes of rectangle
{
String deckString = new String (rank + " " + suit + super.toString());
return deckString;
}
[/code]

now I am getting an output with the rank and suit of the card however I also get something else afterwards that I cant get rid of which is this:

New deck created
Ace SPADESCard@82ba41
// etc etc

Press any key to continue...

any ideas on how to get rid of that?
Why are you calling method super.toString()? The superclass of the Card class
is the Object class. Its toString() method prints the class name (a Card), an @-sign
and the hash code of the object, so that's what you see. Leave that call out, simply
do:

Expand|Select|Wrap|Line Numbers
  1. public String toString() { return rank + " " + suit; }
  2.  
kind regards,

Jos
Mar 1 '08 #8

P: 6
Why are you calling method super.toString()? The superclass of the Card class
is the Object class. Its toString() method prints the class name (a Card), an @-sign
and the hash code of the object, so that's what you see. Leave that call out, simply
do:

Expand|Select|Wrap|Line Numbers
  1. public String toString() { return rank + " " + suit; }
  2.  
kind regards,

Jos
Fantastic that works! Its always something so minor, i had left super in by accident because I copied from an old programme of mine.

Thanks very very much everyone!
Mar 1 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.