Thank you for everyone’s help!! This is what I went with.
3/30/2011-
import java.util.Arrays; public class
Deck {String [] cards = {"AH", "2H", "3H", "4H", "5H", "6H", "7H", "8H",“9H”, “10H”, “JH”, “QH”, “KH”,
“AC”, “2C”, “3C”, “4C”, “5C”, “6C”, “7C”, “8C”, “9C”,
“10C”, “JC”, “QC”, “KC”,
“AD”, “2D”, “3D”, “4D”, “5D”, “6D”, “7D”, “8D”, “9D”,
“10D”, “JD”, “QD”, “KD”,
“AS”, “2S”, “3S”, “4S”, “5S”, “6S”, “7S”, “8S”, “9S”,
“10S”, “JS”, “QS”, “KS”,
};Deck(){ } public void shuffle() { String [] temp = new String[52]; for ( int i = 0; i < 26; i++){ temp [2*i] = cards[i]; temp [2*i+1]= cards[i+26]; } cards = temp; } @Override public String toString() { String cards1 = ""; for ( int i = 0; i < cards.length; i++){ cards1 += cards[i] + " "; if ((i+1)%13==0){ cards1 += "\n"; } } return cards1; } public boolean equals(Deck other) { for (int i=0; i<cards.length; ++i) { if (!this.cards[i].equals(other.cards[i]))return false;
}
return true;
}}
Hi, I need some assistance with my lab hw.
B. It is said that if a deck of cards is given perfect shuffles enough times, it will return to its original order. A perfect shuffle is done by splitting the deck exactly in half and interleaving the cards from the two halves; that is, the first card is from the first half, the second from the second half, the third from the first half and so on.
I need to include the following methods.
-Deck() constructor that creates an unshuffled deck.
-A shuffle() method that does a perfect shuffle.
-A toString() method that print the deck
-An equals(Deck aDeck) method that compares itself with he given deck and returns true if all the cards in both decks are in the same order and false otherwise
I think I am having problems with the constructor part. I don’t know how to create a correct constructor for string arrays. I have three java books, and none of them touched on it.
public class DeckTester {
/** * @param args the command line arguments */ public static void main(String[] args) { Deck d1 = new Deck(); System.out.println(d1); } } public class Deck { String [] cards = {"AH", "2H", "3H", "4H", "5H", "6H", "7H", "8H",“9H”, “10H”, “JH”, “QH”, “KH”,
“AC”, “2C”, “3C”, “4C”, “5C”, “6C”, “7C”, “8C”,
“9C”, “10C”, “JC”, “QC”, “KC”,
“AD”, “2D”, “3D”, “4D”, “5D”, “6D”, “7D”, “8D”,
“9D”, “10D”, “JD”, “QD”, “KD”,
“AS”, “2S”, “3S”, “4S”, “5S”, “6S”, “7S”, “8S”,
“9S”, “10S”, “JS”, “QS”, “KS”,
};Deck(){
cards = new String []{}; }public void shuffle() { for ( int i = 0; i< cards.length; i++){ String temp = cards[ i ]; // swap cards[ i ] = cards[ i+25 ]; // the cards[ i+25 ] = temp; // cards } } }
One could assume that the cards will always be the same. So you could have a constant that defines the set of cards available.
Your constructor then simply allocates memory and copies the constant array to this member variable.