Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am stuck on my lab and need some help. I need to create a Deck class that keep

ID: 3711218 • Letter: I

Question

I am stuck on my lab and need some help. I need to create a Deck class that keeps and ArrayList of cards. This is what I have so far, but its not correct. I need getting an error that int cannot be converted to Card.Suit when trying to create a new card object. I know what this means, but im not sure how to fix it. My professor suggested created an ArrayList of Enumerated types, but im lost.

So here is the API for the Card class and what I have so far(NOTE: the Suit must be an enumerated type -- this is where i am running into problems.)

Thank you in advance.

import java.lang. import java.util.*; public class Deck private ArrayList deck; private int cardsUsed; IItrack number of cards dealt public static final int DECK.SIZE = 52; //size of deck public Deck) deck new ArrayList Card> (DECK_SIZE); 1/create deck of 52 cards for (int suit 0; suit 3; suit++ ) { //to walk through suit for (int rank 1; rank13; ranktt)f //to walk through rank deck.add (new Card(suit, rank)) public boolean isDeckEmptyO (deck . size() return true; if cardsUsed 0){ _ == return false; public Card deal) ( if (deck. size() =-8){ throw new Deck0rHandEmptyException( The deck is empty"): return deck.remove (0):

Explanation / Answer

Given below is the code for the question.
I have also written a shuffle() method in Deck class so that the cards are shuffled and not in same order all the time.
The Card class also has compareTo() method from Comparable interface implemented.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you


Card.java
----------


public class Card implements Comparable<Card>{
public enum Suit{CLUBS, DIAMONDS, HEARTS, SPADES}


private int rank;
private Suit suit;

public Card(Suit suit, int rank)
{
this.suit = suit;
this.rank = rank;
if(rank < 1 || rank > 13)
{
this.rank = 1;
}
}

public int getRank()
{
return rank;
}

public Suit getSuit()
{
return suit;
}

@Override
public int compareTo(Card o) {
//first compare suits
if(suit.compareTo(o.suit) < 0)
return -1;
else if(suit.compareTo(o.suit) > 0)
return 1;
else //both are from same suit, compare ranks
{
if(rank < o.rank)
return -1;
else if(rank > o.rank)
return 1;
else
return 0; // same rank , same suit
}
}


}


Deck.java
---------

import java.util.ArrayList;
import java.util.Random;


public class Deck {
private ArrayList<Card> deck;
private int cardsUsed;

public static final int DECK_SIZE = 52;
public Deck(){
deck = new ArrayList<Card>();
Card.Suit[] suits = Card.Suit.values(); //get all the suit values from Card class

for(int i = 0; i < suits.length; i++)
{
for(int rank = 1; rank <= 13; rank++)
deck.add(new Card(suits[i], rank));
}
cardsUsed = 0;

shuffle();
}


private void shuffle()
{
Random rand = new Random();
for(int i= 0; i < DECK_SIZE; i++)
{
int j = rand.nextInt(DECK_SIZE);
Card temp = deck.get(i);
deck.set(i, deck.get(j));
deck.set(j, temp);
}
}

public boolean isDeckEmpty()
{
return deck.isEmpty();
}

public Card deal()
{
if(isDeckEmpty())
throw new DeckOrHandEmptyException("The deck is empty");
cardsUsed++;
return deck.remove(0);
}
}


public class DeckOrHandEmptyException extends RuntimeException {

public DeckOrHandEmptyException() {
}

public DeckOrHandEmptyException(String message) {
super(message);
}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote