0

So I want to sort a deck of cards. The constructor DeckOfCards sets up and shuffles a deck of cards. But the cards are stored in a list of type List where Card type belongs to Card class. After calling DeckOfCards I should ask the user what kind of sorting algorithm should be used to sort the deck of cards. I have just included Insertion sort at the end for example. But the insertionsort function takes in a double array. So I need to convert List list to a double array. How can I do it?

Code:

import java.util.List;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;

// class to represent a Card in a deck of cards
class Card 
{    
public static enum Face { Ace, Deuce, Three, Four, Five, Six,
  Seven, Eight, Nine, Ten, Jack, Queen, King  };
public static enum Suit { Clubs, Diamonds, Hearts, Spades };

private final Face face; // face of card
private final Suit suit; // suit of card

// two-argument constructor
public Card( Face cardFace, Suit cardSuit ) 
{   
   face = cardFace; // initialize face of card
   suit = cardSuit; // initialize suit of card
} // end two-argument Card constructor

// return face of the card
public Face getFace() 
{ 
  return face; 
} // end method getFace

// return suit of Card
public Suit getSuit() 
{ 
  return suit; 
} // end method getSuit

// return String representation of Card
public String toString()
{
   return String.format( "%s of %s", face, suit );
} // end method toString
} // end class Card

// class DeckOfCards declaration
public class DeckOfCards 
{
private List<Card> list; // declare List that will store Cards

// set up deck of Cards and shuffle
public DeckOfCards()
{
  Card[] deck = new Card[ 52 ];
  int count = 0; // number of cards

  // populate deck with Card objects
  for ( Card.Suit suit : Card.Suit.values() )  
  {
     for ( Card.Face face : Card.Face.values() )   
     {
        deck[ count ] = new Card( face, suit );
        count++;
     } // end for
  } // end for

  list = Arrays.asList( deck ); // get List
  Collections.shuffle( list );  // shuffle deck
} // end DeckOfCards constructor

// output deck
public void printCards()
{
  // display 52 cards in two columns
  for ( int i = 0; i < list.size(); i++ )
     System.out.printf( "%-20s%s", list.get( i ),
        ( ( i + 1 ) % 2 == 0 ) ? "\n" : "" );
} // end method printCards

public static void main( String args[] )
{
  DeckOfCards cards = new DeckOfCards();
  cards.printCards();
  //add code here to take input from user and sort
  int a;
  System.out.println("\nSort the deck of cards");
  System.out.println("\nEnter your choice: \n1.Selection Sort \n2.Insertion Sort 
\n3.Merge       Sort\n");
  //take input
  Scanner reader = new Scanner(System.in);
  a=reader.nextInt();
  switch(a)
  {
    case 1:
        //call Selection sort
        break;
    case 2:
        //call Insertion sort
        break;
    case 3:
        //call Merge sort
        break;
  }
 } // end main  

//Insertion sort

} // end class DeckOfCards
public class InsertionSort {
/** The method for sorting the numbers */
public static void insertionSort(double[] list) {
for (int i = 1; i < list.length; i++) {
  /** insert list[i] into a sorted sublist list[0..i-1] so that
       list[0..i] is sorted. */
  double currentElement = list[i];
  int k;
  for (k = i - 1; k >= 0 && list[k] > currentElement; k--) {
    list[k + 1] = list[k];
  }

  // Insert the current element into list[k+1]
  list[k + 1] = currentElement;
}
}
}
Komal Rathi
  • 3,560
  • 11
  • 42
  • 88
  • possible duplicate of [Convert list to array in java](http://stackoverflow.com/questions/9572795/convert-list-to-array-in-java) – Andrew Feb 10 '13 at 23:39
  • From what magical place that `double[]` array sorting comes from in that code? – Rohit Jain Feb 10 '13 at 23:39
  • Write a sort method that takes a `List extends Comparable>` and have your Card implement Comparable. – assylias Feb 10 '13 at 23:49
  • @Andrew Its not a dupplicate, there Arrays of Objects are the result, he wants array of primitive type – AlexWien Feb 11 '13 at 00:52

2 Answers2

0

If you wanted to convert a list of Card objects, i.e. List<Card> list you could easily change it to a Card array (i.e. Card[]) using a method provided by List itself called toArray

However, you have some confusion in what you're asking. You are expecting that a method that expects an array of double suddenly magically knows how to treat objects of type Card. How can a Card suddenly behave like a double precision number?

You should change the method insertionSort() first (which you probably copied from somewhere without trying to understand what its doing) to take a Card[] array first, and then modify what its doing internally so that the comparison happens on the properties of the Card object rather than the double primitive.

On the other hand, you might be interested to know that Java already provides sorting functions through the Arrays class. You can have a look at the function Arrays.sort(). However you still need to implement the Comparable interface so that Java knows how to sort the array, it doesn't know how your Card objects should be sorted (by face? by suit? in which order? Java doesn't know how to play cards), so you need to make your Card objects implement Comparable and have a compareTo() method which helps Java decide which Card instance comes before which. You can use the same approach if you still want to implement the sorting yourself and change the method insertionSort() to take an array of Comparable items and inside it you call compareTo() to know which item comes before what.

jbx
  • 18,832
  • 14
  • 73
  • 129
0

If I understood right, you want to sort your Cards with different algorithms. And your implementation of algorithms accept only double array.

Two question you need to think about:

  • The rule of Card comparison, e.g. Heart Ace > Diamonds Ace?

  • if all sortings are comparison sort algorithm?

If the answer of the 2nd question is: YES

I suggest you stopping thinking about converting Card Array type to double array. Instead, making your all algorithm implementation more generic, for example, accept Comparable[] arr array, or even Card[] arr. Then make your Card class implement Comparable interface. Implement your card comparison rule in compareTo(Card c) method.

Kent
  • 173,042
  • 30
  • 210
  • 270
  • So you mean within the algorithm function body I should pass a Card[] arr instead of a double[] arr? – Komal Rathi Feb 11 '13 at 00:08
  • `Comparable[]` would be better, because it could be used for any case. of course, only if all your sorting are **comparison sort** – Kent Feb 11 '13 at 00:10
  • Thanks everyone. I figured it out. Everyone here has helped me in someway to get me through. THanks again! – Komal Rathi Feb 12 '13 at 22:18