473,395 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Need help with a deck of cards homework!

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
8 29125
Laharl
849 Expert 512MB
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
sukatoa
539 512MB
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
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
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
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
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
JosAH
11,448 Expert 8TB
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
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

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

Similar topics

0
by: Adam Haskell | last post by:
Ok heres the situation: We have a linked server from SQL 2000 to a foxpro dbf. In our test enviroment we had the Foxpro and SQL server on the same machine. The linked worked perfect. Now we are...
2
by: John Smith | last post by:
This is what I'm trying to do: In a form, a user enters a value (e.g. John) and clicks on submit. Submit opens up a query with one of its values (e.g. Name) set to the value of the control...
2
by: Esteban404 | last post by:
My WinForm application is activating menus based on the user's roles in AD. I've created the groups to use the same 3 letter abbreviation. Is there a way to do something like this: ...
25
by: Mark | last post by:
I'm just starting out in an introductory ASP.Net course, and am trying to run a simple program but keeping getting an error. I'm running XP, have installed Internet Information Services (5.1) ,...
1
by: Arvind P Rangan | last post by:
Hi, I have created a class library which i need to use in my ASPX file. when i say: Dim mylib As TestLib = new TestLib() it gives me an error saying type required. How do we use a class...
0
by: saravanan_article | last post by:
Hi I am newbie to C#, i am using C# 2005 and DataGridView in my Application. The problem is described here I am using DataGrid and I placed some Headers like Column1,Column2,Column3.... What i...
3
by: vengeboyz | last post by:
Hi everybody, My name's Jack and I'm a newbie in this forum... Nice to meet you all and i hope i can get along in this forum... And I also hope that you can help me with my problem... I'm a very...
7
by: MonkeyHater | last post by:
Okay, so I am have a bit of trouble with my program. The program itself will deal two hands of cards, shuffle the deck, and then deal another two hands. At the moment I have most of the code down I...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.