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
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
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.");
}
}
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
Comment