Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Sorting a deck of cards by suite or rank.

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:

    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

    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.
    Last edited by helloworld922; December 13th, 2010 at 10:13 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,607
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Sorting a deck of cards by suite or rank.

    Quote Originally Posted by coyne20 View Post
    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).

Similar Threads

  1. [SOLVED] Sorting an Array
    By petemyster in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 12th, 2010, 11:07 AM
  2. sorting name using Selectionsort
    By asdfg in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 20th, 2010, 10:44 AM
  3. JDBC API Test suite 1.3.1
    By prabhuaakannan in forum JDBC & Databases
    Replies: 0
    Last Post: March 26th, 2010, 10:57 AM
  4. [SOLVED] sorting
    By kite98765 in forum Algorithms & Recursion
    Replies: 8
    Last Post: February 4th, 2010, 08:34 AM