Question: I'm having trouble figuring out what's wrong with this code. I'm getting 2 errors both of the same kind. error: incompatible types: Object cannot be
I'm having trouble figuring out what's wrong with this code. I'm getting 2 errors both of the same kind. error: incompatible types: Object cannot be converted to Card. I am supposed to create a deck class, representing a deck of 52 cards. It should have at a minimum, a data sctruture that holds 52 card objects and at a minimum, a method to draw a card and to shuffle the deck. As someone checks this can you also see if I have those requirements as well, while checking the error, thanks.
import java.util.*; public class Deck { public static final int HEARTS = 0; public static final int DIAMONDS = 1; public static final int SPADES = 2; public static final int CLUBS = 3; public static final int JACK = 11; public static final int QUEEN = 12; public static final int KING = 13; public static final int ACE = 14; private ArrayList deck;
//This creates a deck. A deck starts as a list of 52 cards. //We loop through each suit and rank and construct a card and //add it to the deck. public Deck() { deck = new ArrayList();
for(int rank = 2; rank <= ACE; rank++) { for(int suit = HEARTS; suit <= CLUBS; suit++) { Card card = new Card(rank, suit); deck.add(card); } } }
//This getter method returns the ArrayList of cards. public ArrayList getCards() { return deck; }
//This deals the first card from the deck by removing it. public Card deal() { deck.remove(0); }
//This shuffles the deck by making 52 swaps of cards positions. public void shuffle() { for(int i = 0; i < deck.size(); i++) { int randomIndex = Randomizer.nextInt(52); Card one = deck.get(i); Card two = deck.get(randomIndex); deck.set(i,two); deck.set(randomIndex,one); } }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
