Sorting a deck of cards by suite or rank.
My intentions are to provide a sorting function. The deck of cards can be sorted either by rank or suite. I have had a look at the comparable interface to provide this but it is suitable only for numeric sorting and not for String sorting. I have provided the 2 classes below:
Code Java:
package com.base.net;
public class Card
{
private String rank; // face of card ("Ace", "2", "Deuce", ...)
private String suit; // suit of card ("Hearts", "Diamonds", ...)
// two-argument constructor initializes card's face and suit
public Card( String cardRank, String cardSuite)
{
rank = cardRank; // initialize face of card
suit = cardSuite; // initialize suit of card
} // end two-argument Card constructor
//getter and setter methods
public String getRank()
{
return rank;
}
public void setRank(String rank)
{
this.rank = rank;
}
public String getSuit()
{
return suit;
}
public void setSuit(String suit)
{
this.suit = suit;
}
/*
public int compareTo(Card crd) {
if (crd instanceof Card) {
Card card = (Card) crd;
if (this.rank > card.getRank())
return 1;
else if (this.rank < card.getRank())
return -1;
}
return 0;
}
*/
// return String representation of Card
public String toString()
{
return rank + " of " + suit;
} // end method toString
} // end class Card
Code Java:
package com.base.net;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
public class Deck
{
private final String [] suite = {"Clubs", "Diamonds", "Hearts", "Spades"};
private final String [] rank = {"Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"};
private static List<Object> deckofcards = new ArrayList<Object>();
public Deck()
{
for(int i=0; i<suite.length; i++)
{
for(int j=0; j<rank.length; j++)
{
deckofcards.add(new Card(rank[j],suite[i]));
}
}
}
public List getDeckofcards()
{
return deckofcards;
}
public int getSuiteSize()
{
return suite.length;
}
public int getRankSize()
{
return rank.length;
}
public int getDeckSize()
{
return deckofcards.size();
}
public void viewDeck()
{
try
{
Iterator iter = deckofcards.iterator();
while (iter.hasNext())
{
System.out.println(iter.next());
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void shuffle()
{
// This method converts a list of objects into an array.
// Manipulates the array (shuffling).
// Converts the array back to a list (S.A.T - Simple As That).
try
{
Object [] cards = deckofcards.toArray();
Random randomNumbers = new Random();
int currentCard = 0; // reinitialize currentCard
for ( int first = 0; first < cards.length; first++ )
{
int second = randomNumbers.nextInt( cards.length );
Object temp = cards[ first ];
cards[ first ] = cards[ second ];
cards[ second ] = temp;
}
deckofcards = Arrays.asList(cards);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void pickRandomCard()
{
try
{
Random randomNumbers = new Random();
int rand = randomNumbers.nextInt( deckofcards.size() );
System.out.println(deckofcards.get(rand).toString());
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void resetCards()
{
deckofcards = null;
}
protected void finalise()
{
deckofcards = null;
System.gc();
}
public void orderDeckofCards(Deck d)
{
}
public static void main(String args[])
{
Deck d = new Deck();
//d.shuffle();
d.viewDeck();
//d.pickRandomCard();
//d.orderDeckbySuite();
}
}
Any help on this would be much and greatly appreciated. Many Thanks to your contribution.
Re: Sorting a deck of cards by suite or rank.
Quote:
Originally Posted by
coyne20
I have had a look at the comparable interface to provide this but it is suitable only for numeric sorting and not for String sorting.
That's incorrect. Take another look.
When posting code, make sure you use the code tags to preserve formatting. Also, code should be in the form of an SSCCE (if your question is about the Comparable interface, post code only related to that).