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”,”DIAMO NDS”,”CLUBS”,”H EARTS”.
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:
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.NullP ointerException
at Deck.<init>(Dec k.java:20)
at DeckTester.main (DeckTester.jav a: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.
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”,”DIAMO NDS”,”CLUBS”,”H EARTS”.
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:
Code:
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());
}
}
The project complies properly but when I run it I get this error:
Exception in thread "main" java.lang.NullP ointerException
at Deck.<init>(Dec k.java:20)
at DeckTester.main (DeckTester.jav a: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.
Comment