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 are an important and useful topic but no longer part of the AP CS A curriculum. */
public class Card implements Comparable
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
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 return 0; // place holder } public static ArrayList return null; // place holder } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
