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: -
-
public class Deck
-
{
-
private Card [] deck;
-
private int numberOfCards;
-
-
/**
-
* Creates a deck of 52 playing cards
-
* The deck is set up in the order SPADES, DIAMONDS, CLUBS,HEARTS
-
*/
-
public Deck()
-
{
-
String [] suits = {"SPADES","DIAMONDS","CLUBS","HEARTS"};
-
-
for ( int suit = 0; suit <= 3; suit++ )
-
{
-
for ( int rank = 1; rank <= 13;rank++ )
-
{
-
numberOfCards = 0;
-
deck [numberOfCards] = new Card(rank,suit);
-
numberOfCards ++;
-
}
-
}
-
-
numberOfCards = 52;
-
-
}
-
-
/**
-
* shuffles the deck of cards.
-
*
-
*/
-
/* public void shuffle()
-
{
-
Card temp; // Temporary object to hold value whilst swapping
-
-
//loop through each card in the deck and swap it with some random card.
-
//FOR EACH CARD IN THE DECK
-
{
-
int rand = (int)(Math.random()*52);
-
//TAKE THE CURRENT CARD...
-
//AND SWAP IT WITH THE RAND CARD...
-
}
-
}
-
-
/**
-
* Returns a representation of the deck as a string
-
* one card per line
-
* @return the deck as a String
-
*/
-
public String toString()
-
{
-
String deckString = "New deck created ";
-
for (int cards =0; cards <52; cards++)
-
{
-
deckString = deckString + deck[cards] + "\n";
-
-
}
-
System.out.println(deckString);
-
return deckString;
-
}
-
}
-
-
-
----------------------------------------------------------------------------------------------
-
/**
-
* This class represents a simple playing card.
-
* The attributes of the card are both of type String:
-
* rank - takes values ACE,1,2,3,.., JACK, QUEEN, KING
-
* suit - takes values SPADES, DIAMONDS,CLUBS, HEARTS
-
*/
-
public class Card
-
{
-
-
private String rank;
-
private String suit;
-
-
/*Constructor for objects of class Card
-
* Each card has a rank and a suit. No checks are made for invalid values
-
* @param rank an int representing the valus of the card, range 1..13
-
* @param suit a String representing the suit of card: values
-
"SPADES","DIAMONDS","CLUBS","HEARTS"
-
*/
-
public Card(int rank, int suit)
-
{
-
if(suit == 0)
-
{
-
this.suit = "Spades";
-
}
-
else if(suit == 1)
-
{
-
this.suit = "Diamonds";
-
}
-
else if(suit ==2)
-
{
-
this.suit = "Clubs";
-
}
-
else
-
{
-
this.suit = "Hearts";
-
}
-
-
-
// must convert the integer value of rank to an appropriate String
-
if (rank == 1)
-
{
-
this.rank = "Ace";
-
}
-
else if (rank == 11)
-
{
-
this.rank = "Jack";
-
}
-
else if (rank == 12)
-
{
-
this.rank = "Queen";
-
}
-
else if (rank == 13)
-
{
-
this.rank = "King";
-
}
-
else
-
{
-
this.rank = "" + rank;
-
}
-
}
-
public String getSuit()
-
{
-
return suit;
-
}
-
public String getRank()
-
{
-
return rank;
-
}
-
-
}
-
-------------------------------------------------------------------------------
-
public class DeckTester
-
{
-
public static void main(String[] args)
-
{
-
Deck deck1 = new Deck();;
-
System.out.println(deck1.toString());
-
-
}
-
}
-
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.
8 29124
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.
what is
@ 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
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?
what is
@ 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
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?
Right so I am still at this I have updated the card and deck class they now look like this -
-
public class Deck
-
{
-
private Card [] deck;
-
private int numberOfCards;
-
-
/**
-
* Creates a deck of 52 playing cards
-
* The deck is set up in the order SPADES, DIAMONDS, CLUBS,HEARTS
-
*/
-
public Deck()
-
{
-
deck = new Card[52];
-
numberOfCards = 0;
-
String [] suits = {"SPADES","DIAMONDS","CLUBS","HEARTS"};
-
for ( int suit = 0; suit <= 3; suit++ )
-
{
-
for ( int rank = 1; rank <= 13;rank++ )
-
{
-
-
deck [numberOfCards] = new Card(rank,suits[suit]);
-
numberOfCards ++;
-
}
-
}
-
-
-
-
}
-
-
/**
-
* shuffles the deck of cards.
-
*
-
*/
-
/* public void shuffle()
-
{
-
Card temp; // Temporary object to hold value whilst swapping
-
-
//loop through each card in the deck and swap it with some random card.
-
//FOR EACH CARD IN THE DECK
-
{
-
int rand = (int)(Math.random()*52);
-
//TAKE THE CURRENT CARD...
-
//AND SWAP IT WITH THE RAND CARD...
-
}
-
}
-
-
/**
-
* Returns a representation of the deck as a string
-
* one card per line
-
* @return the deck as a String
-
*/
-
public String toString()
-
{
-
String deckString = "New deck created\n";
-
for (int cards =0; cards <=51; cards++)
-
{
-
deckString = deckString + deck[cards] + "\n";
-
}
-
return deckString;
-
}
-
}
-
-
-
-
-
-
-
-
-
/**
-
* This class represents a simple playing card.
-
* The attributes of the card are both of type String:
-
* rank - takes values ACE,1,2,3,.., JACK, QUEEN, KING
-
* suit - takes values SPADES, DIAMONDS,CLUBS, HEARTS
-
*/
-
public class Card
-
{
-
-
private String rank;
-
private String suit;
-
// MISSING CODE
-
-
/*Constructor for objects of class Card
-
* Each card has a rank and a suit. No checks are made for invalid values
-
* @param rank an int representing the valus of the card, range 1..13
-
* @param suit a String representing the suit of card: values
-
"SPADES","DIAMONDS","CLUBS","HEARTS"
-
*/
-
public Card(int rank, String suits)
-
{
-
this.suit = suits;
-
-
-
// must convert the integer value of rank to an appropriate String
-
if (rank == 1)
-
{
-
this.rank = "Ace";
-
}
-
else if (rank == 11)
-
{
-
this.rank = "Jack";
-
}
-
else if (rank == 12)
-
{
-
this.rank = "Queen";
-
}
-
else if (rank == 13)
-
{
-
this.rank = "King";
-
}
-
else
-
{
-
this.rank = "" + rank;
-
}
-
}
-
public String getSuit()
-
{
-
return suit;
-
}
-
public String getRank()
-
{
-
return rank;
-
}
-
public String toString() //add to the .toString method to show all attributes of rectangle
-
{
-
String deckString = new String (rank + " " + suit + super.toString());
-
return deckString;
-
}
-
-
}
-
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?
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: -
public String toString() { return rank + " " + suit; }
-
kind regards,
Jos
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: -
public String toString() { return rank + " " + suit; }
-
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!
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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:
...
|
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) ,...
|
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...
|
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...
|
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...
|
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...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
| |