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.java, FaceCard.java, NumberCard.java, Deck.java, CardTest.java -
//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.");
-
}
-
}
-
-
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(rank,suit);
^
1 error
I hope whatever i wrote makes some sense and i get some help. Any kind of help would be appreciated
Thanks
21 3784
1. Make PlayingCard not abstract.
2. Your constructor is
public NumberCard(int r, Suit s) {
so when you construct the deck, the 2nd argument should be a suit. Right now it is:
cards [count] = abstract PlayingCard(rank,suit);
I think you want:
cards [count] = abstract PlayingCard(rank,suits[suit]);
3. but you have to change the order of the suits.
4. Further, move the count initialization before the for loops, otherwise you'll always only be setting cards[0], and not the whole deck.
Ok so i did the changes u asked me to..it stilll shows me the following error:
Deck.java:26: illegal start of expression
cards [count] = abstract PlayingCard(rank,Suit[suits]);
^
1 error
the arrow is pointed at a of the abstract and the word cards comes before[count]
here's how my program looks like now -
public Deck() {
-
String [] suits = {"Clubs","Diamonds","Hearts","Spades"};
-
count = 0;
-
-
for ( int suit = 0; suit <= 3; suit++ ) {
-
for ( int rank = 2; rank <= 14;rank++ ) {
-
cards [count] = abstract PlayingCard(rank,Suit[suits]);
-
-
-
count ++;
-
}
-
}
-
-
count = 52;
-
-
}
-
-
I cant remove the abstract part because its a part of playing card class and m not supposed to make changes to that class.
I think the removing abstract part is done....but i have another issue. That construction part still doesnt work.
this is the error that shows
Deck.java:26: cannot find symbol
symbol : variable Suit
location: class Deck
cards [count] = PlayingCard( rank,Suit[suits]);
^
Deck.java:26: incompatible types
found : java.lang.String[]
required: int
cards [count] = PlayingCard( rank,Suit[suits]);
^
2 errors
cards [count] = PlayingCard( rank,Suit[suits]);
and this is the change i made. plzzz help!!!
Sorry, I didn't notice a 2nd quick mistake with your suits declaration earlier: you want the actual Suit, not the strings.
Suit[] suits = {Suit.Clubs,Suit.Diamonds,Suit.Hearts,Suit.Spades}
"I think you want:
cards [count] = abstract PlayingCard(rank,suits[suit]);"
not working..if i write this " cards [count] = abstract PlayingCard( rank,suits[Suit]); "
this is the error:
Deck.java:29: illegal start of expression
cards [count] = abstract PlayingCard( rank,suits[Suit]);
^
1 error
if i remove the abstract, this is the error
Deck.java:24: incompatible types
found : java.lang.String
required: Suit
Suit[] suits = {"Clubs","Diamonds","Hearts","Spades"};
^
Deck.java:24: incompatible types
found : java.lang.String
required: Suit
Suit[] suits = {"Clubs","Diamonds","Hearts","Spades"};
^
Deck.java:24: incompatible types
found : java.lang.String
required: Suit
Suit[] suits = {"Clubs","Diamonds","Hearts","Spades"};
^
Deck.java:24: incompatible types
found : java.lang.String
required: Suit
Suit[] suits = {"Clubs","Diamonds","Hearts","Spades"};
^
Deck.java:29: cannot find symbol
symbol : variable Suit
location: class Deck
cards [count] = PlayingCard( rank,suits[Suit]);
^
Deck.java:29: cannot find symbol
symbol : method PlayingCard(int,Suit)
location: class Deck
cards [count] = PlayingCard( rank,suits[Suit]);
^
6 errors
suits[suit] not suits[Suit].
suit, the integer. Is problematic that you've assigned a local variable pretty much the same name as the class.
You cannot assign to suits based on a string. The previous comment states Suit.Clubs, etc.. to get the actual class member that you've declared earlier.
that aint working either
i don't know whats the problem with this line :
Suit[] suits = (Clubs,Diamonds,Hearts,Spades);
This error shows up as ssoon as i write this:
Deck.java:24: ')' expected
Suit[] suits = (Clubs,Diamonds,Hearts,Spades);
^
Deck.java:24: ';' expected
Suit[] suits = (Clubs,Diamonds,Hearts,Spades);
^
2 errors
Clubs, Diamonds, Hearts and Spades are all defined explicitly in the Suit class. The Deck class has no idea what these are; you need to reference them like
Suit.Clubs, etc.
I suggest a repost of that function after you make the change, just to see if there are any surrounding problems. Please use [ code ] tags [ / code] (the # sign).
I'm sorry but i have no idea how do we go about the reference part. If u could gimme a hint regarding that...
thanks
???
here's the whole program along with the realted classes -
-
//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();
-
}
-
}
-
-
//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);
-
}
-
}
-
-
-
-
//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();
-
}
-
-
}
-
-
-
//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() {
-
cards = new PlayingCard[52];
-
count = 0;
-
-
Suit[] suits = Suit.Clubs();
-
Suit[] suits = Suit.Diamonds();
-
Suit[] suits = Suit.Hearts();
-
Suit[] suits = Suit.Spades();
-
//**** count = 0;
-
-
for ( int suit = 0; suit <= 3; suit++ ) {
-
for ( int rank = 2; rank <= 14;rank++ ) {
-
cards [count] = PlayingCard( rank,suits[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;
-
}
-
}
-
}
-
-
I badly need help with this!!!
Does your browser not move your window to the right spot?
Reread posts #2 and #5 again.
I'm really sorry but i actually get it now! But there's this error again!!
Deck.java:31: PlayingCard is abstract; cannot be instantiated
cards [count] = new PlayingCard( rank,suits[suit]);
^
1 error
here's d deck condtructor again! -
public Deck() {
-
cards = new PlayingCard[52];
-
count = 0;
-
-
Suit[] suits = {Suit.Clubs,Suit.Diamonds,Suit.Hearts,Suit.Spades};
-
-
-
//**** count = 0;
-
-
for ( int suit = 0; suit <= 3; suit++ ) {
-
for ( int rank = 2; rank <= 14;rank++ ) {
-
cards [count] = PlayingCard( rank,suits[suit]);
-
-
-
count ++;
-
}
-
}
-
- public class Card extends PlayingCard {
-
public String toString(){
-
// your code here
-
}
-
Replace all instantiations of PlayingCard with Card
you mean have a new class Card??.but we are not allowed to do that.!!
You have to keep the class abstract, but you're not allowed to have a new class?
You're screwed by a stupid assignment or unclear rules. Maybe the assignment is really to have you challenge stupid programming assumptions. Try talking to your teacher or teaching assistant.
yeah my professor sure loves giving such weird assignments n i gotta deal with it. He gives us a pre written progran and we gotta update it in soem way without changing what he has already done. But even though we arent allowed to have a new class we are allowed to have new methods or so. here's the link to the assignment, just in case case it gives a clearer view. ftp://eve.kean.edu/pub/leew/cps2231/hmwk3.pdf
FaceCard and NumberCard. - public class FaceCard extends PlayingCard {
-
public String toString(){
-
// your code here
-
}
-
-
....
-
-
-
for ( int suit = 0; suit <= 3; suit++ ) {
-
for ( int rank = 2; rank <= 10;rank++ ) {
-
cards [count] = NumberCard( rank,suits[suit]);
-
}
-
for (int rank = 11; rank <= 14;rank++ ) {
-
cards [count] = FaceCard( rank,suits[suit]);
hey may be u could just help me with this one last thing; the peekattopcard method. can u tell me how do i do that method?
That new way for the constructor still has the same issues. it still cannot find the symbol.pssh!!!
here's what i wrote
public Deck() {
cards = new PlayingCard[52];
count = 0;
Suit[] suits = {Suit.Clubs,Suit.Diamonds,Suit.Hearts,Suit.Spades} ;
//**** count = 0;
for ( int suit = 0; suit <= 3; suit++ ) {
for ( int rank = 2; rank <= 14;rank++ ) {
cards [count] = NumberCard( rank,suits[suit]);
}
for (int rank = 11; rank <= 14;rank++ ) {
cards [count] = FaceCard( rank,suits[suit]);
count ++;
}
}
Assuming you start from the 2 of clubs. Once you deal a card, it is gone forever?
I would have an index value of cards used so far.
When you start, with 0 cards dealth, the first card will be cards[0]
If you've dealt 4 cards, the next card will be cards[4]
continue until you reach the count.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: JC |
last post by:
I am very new to programming and learning on my own.
Why do I keep getting duplicate values using this code? I want to shuffle a
deck of 52 cards. The logic seems right to me.
Randomize
For...
|
by: CaseyB |
last post by:
If I wanted to create a game like Solitaire that would first randomly
shuffle a deck of cards, I figured out that all I had to use is the Random()
class or rnd and make sure I use the Randomize...
|
by: Arun Nair |
last post by:
Can any one help me with this im not getting it even after reading
books because there is not much of discussion anywhere
a>
Implement a calss that represents a playing card. The class should...
|
by: Pratik |
last post by:
For the time being, I'm doing a simple swap method for my deck of
cards using the random number generator in cstdlib. I've created a
dynamic array of a type class Card. When I go to shuffle it and...
|
by: tvance929 |
last post by:
Hey everyone,
I created a theDeck class that creates a 52 card int List. Inside of this class I have a ShuffleCards method.
I simply want 2 seperate decks that I can then shuffle and...
|
by: garyrowell |
last post by:
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...
|
by: Vneha |
last post by:
import java.util.*;
public class Deck {
public static int numSuits = 4;
public static int numRanks = 13;
public static int numCards = numSuits * numRanks;
private Card...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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...
|
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...
|
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: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |