Question: Card.Java /* The Card class implements the Comparable interface. Interfaces are an important and useful topic but no longer part of the AP CS A

 Card.Java /* The Card class implements the Comparable interface. Interfaces arean important and useful topic but no longer part of the AP

Card.Java

/* The Card class implements the Comparable interface. Interfaces are an important and useful topic but no longer part of the AP CS A curriculum. */

public class Card implements Comparable{ private String suit; // spades, hearts, diamonds, clubs private String name; // e.g. ace, king, ... two private int rank; // ace = 14, king = 13, ... two = 2

public Card( String suit, String name, int rank ){ this.suit = suit.toLowerCase(); this.name = name.toLowerCase(); this.rank = rank; } public String getSuit(){ return suit; } public int getRank(){ return rank; } public String toString(){ String symbol = ""; if (suit.equals("spades")) //symbol = "\u2660"; unicode does not render correctly in codeboard //symbol = "spades "; symbol = "♠"; // this is an HTML entity, it works because codeboard renders the program output as HTML else if (suit.equals("hearts")) //symbol = "\u2665"; //symbol = "hearts "; symbol = "♥"; // this is an HTML entity, it works because codeboard renders the program output as HTML else if (suit.equals("diamonds")) //symbol = "\u2666"; //symbol = "diamonds"; symbol = "♦"; // this is an HTML entity, it works because codeboard renders the program output as HTML else //symbol = "\u2663"; //symbol = "clubs "; symbol = "♣"; // this is an HTML entity, it works because codeboard renders the program output as HTML // the following is done so that returned string always has a length of 11 if ( rank >= 2 && rank king > queen > jack ... return diffRanks; // if the ranks are the same if ( this.suit.equals( other.suit ) ) return 0; // otherwise spades > hearts > diamonds > clubs if ( this.suit.equals( "spades" ) ) return 1; if ( other.suit.equals( "spades" ) ) return -1; // neither one is spades if ( this.suit.equals( "hearts" ) ) return 1; if ( other.suit.equals( "hearts" ) ) return -1; // neither one is hearts, but the suits are different // therefore one is diamonds and the other is clubs if ( this.suit.equals( "diamonds" ) ) return 1; else return -1; } // The equals method code involves a number of concepts // that we have not covered yet // You are not responsible for its content public boolean equals( Object x ){ if ( !(x instanceof Card) ) return false; Card other = (Card)x; return this.suit.equals( other.suit ) && this.rank == other.rank; } }

Deck.Java

import java.util.ArrayList; import java.util.Random;

public class Deck{ private ArrayList cards; public Deck( int seed ){ fillDeck(); shuffle( seed ); } public Card pull(){ int lastIndex = cards.size() - 1; return cards.remove( lastIndex ); } public int numCardsLeft(){ return cards.size(); } public void reset(int seed ){ fillDeck(); shuffle( seed ); } private void fillDeck(){ cards = new ArrayList(); fillSuit( "spades" ); fillSuit( "hearts" ); fillSuit( "diamonds" ); fillSuit( "clubs" ); } private void fillSuit( String s ){ String [] names = {"two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "jack","queen", "king", "ace"}; for ( int k = 2; k = 1; k-- ){ int i = ran.nextInt( 52 ); // gets a random int in the range [0,52) Card temp = cards.get( k ); cards.set( k, cards.get(i ) ); cards.set( i, temp ); // swap the cards at indices k and i } } }

Main.Java

import java.util.ArrayList;

public class Main { public static void main(String[] args) { Deck deck = new Deck( 388 ); // you may change the seed int numCards = 9; // you may change the number of cards ArrayList hand = new ArrayList(); for ( int k = 0; k nums = selectionSort( hand ); for ( Card c : hand ) System.out.println( c.toString() ); System.out.println( nums ); } public static int smallestCard(ArrayList list, int j){ /* This returns the index of the smallest Card in the ArrayList from index j to the end of the array list. Precondition: 0

return 0; // place holder } public static ArrayList selectionSort( ArrayList list ){ /* Implement the selection sort algorithm as outlined in the notes. Every time the smallestCard method is called, add the value to an ArrayList. After the ArrayList is sorted, return the ArrayList. */

return null; // place holder } }

Implement a selection sort on an ArrayList of Card objects. This program uses the same Card and Deck classes as 7.05. First write the following helper method: public static int smallestCard(ArrayList list, int j}{ Return the index of the smallest Card from indexj to the end of the array list. Precondition: 0 selectionSort( ArrayList list) /* Implement the selection sort algorithm as outlined in the notes. Every time the smallestCard method is called, add the value to an ArrayList. After the ArrayList is sorted, return the ArrayList. */ } Note. The only reason the selectionSort method is returning that list of numbers is to check that you implemented the algorithm correctly. Three sample runs are on the next page. Here are sample outputs. You have to change the seed and number of cards to see samples 2 and 3 seed: 388 seed: 24 seed: 899 numCards: 9 numCards: 7 numCards: 11 o 10 K > 5 00 A 8 5 10 10 4 8 A 7 o

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!