Question
This game implements blackJack game in Java...the codes that I have doubts are in Bold ....some of the methods are filled in. There are two
This game implements blackJack game in Java...the codes that I have doubts are in Bold....some of the methods are filled in. There are two classes for now (Card and CardPile) the instruction are wriiten above the methods.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Card.java
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public class Card implements Comparable { //Symbolic constants
public static final int CLUB = 0; public static final int DIAMOND = 1; public static final int HEART = 2; public static final int SPADE = 3; private int rank ; private int suit; boolean faceUp; /** * Construct a card of the given rank, suit and whether it is faceup or * facedown. The rank is an integer from 2 to 14. Numbered cards (2 to 10) * have a rank equal to their number. Jack, Queen, King and Ace have the ranks * 11, 12, 13, and 14 respectively. The suit is an integer from 0 to 3 for * Clubs, Diamonds, Hearts and Spades respectively. * * @param rank * @param suit * @param faceUp */ public Card(int rank, int suit, boolean faceUp) { this.rank = rank; this.suit = suit; this.faceUp = faceUp; }
/** * @return the faceUp */ public boolean isFaceUp() { return faceUp;
}
/** * @param faceUp the faceUp to set */ public void setFaceUp(boolean faceUp) { if(faceUp == true){ this.faceUp = faceUp; } this.faceUp = false; }
/** * @return the rank */ public int getRank() { return rank;
}
/** * @return the suit */ public int getSuit() { return suit; }
@Override public boolean equals(Object ob) { Card c = (Card) ob; if (c.rank == rank) { return true; } return false; }
@Override public int hashCode() {//DO NOT MODIFY int hash = 7; hash = 31 * hash + this.getSuit(); return hash; }
@Override public int compareTo(Object obj) {//DO NOT MODIFY return compareTo((Card) obj); }
public int compareTo(Card c) { int a = 0; if(c.rank > rank){ a = c.rank; } else if (c.rank < rank){ a = rank; } if (c.rank == rank){ if(c.suit > suit){ a = c.suit; } else if (c.suit < suit){ a = suit; } } return a; }
/** * Return the rank as a String. For example, the 3 of Hearts produces the * String "3". The King of Diamonds produces the String "King". * * @return the rank String */ public String getRankString() { return "" + rank; }
/** * Return the suit as a String: "Clubs", "Diamonds", "Hearts" or "Spades". * * @return the suit String */ public String getSuitString() { if(suit == CLUB){ return "Clubs"; } if(suit == DIAMOND){ return "Diamonds"; } if(suit == HEART){ return"Hearts"; } return "Spades"; }
/** * Return "?" if the card is facedown; otherwise, the rank and suit of the * card. * * @return the String representation */ @Override public String toString() { if( faceUp == false){ return "?"; } String s = "" + getRankString() + " of " + getSuitString(); return s ; }
public static void main(String[] args) { //Create 5 of clubs Card club5 = new Card(5, 0, true); System.out.println("club5: " + club5); Card spadeAce = new Card(14, SPADE, true); System.out.println("spadeAce: " + spadeAce); System.out.println("club5 compareTo spadeAce: " + club5.compareTo(spadeAce)); System.out.println("club5 compareTo club5: " + club5.compareTo(club5)); System.out.println("club5 equals spadeAce: " + club5.equals(spadeAce)); System.out.println("club5 equals club5: " + club5.equals(club5)); } }
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------CardPile.java
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public class CardPile { //Instance variables private ArrayListcards; public CardPile() { //Initialize the instance variable. } /** * Add a card to the pile. * @param card */ public void add(Card card) { //FIX THIS } /** * Remove a card chosen at random from the pile. * @return */ public Card removeRandom() { return null; //FIX THIS } /** * The string representation is a space separated list * of each card. * @return */ @Override public String toString() { //FIX THIS return ""; } /** * @return the cards */ public ArrayList getCards() { return cards; } public static void main(String[] args) { CardPile p = new CardPile(); p.add(new Card(2, 1, true)); p.add(new Card(3, 2, true)); p.add(new Card(4, 3, false)); p.add(new Card(14, 1, true)); System.out.println("Removed: " + p.removeRandom()); System.out.println("Removed: " + p.removeRandom()); System.out.println("Removed: " + p.removeRandom()); System.out.println("Removed: " + p.removeRandom()); System.out.println(""); CardPile deck = new CardPile(); for(int i = 2; i < 15; i++) { for(int j = 0; j < 4; j++) { deck.add(new Card(i, j, true)); } } for (int i = 0; i < 52; i++) { System.out.println((i+1) + ": " + deck.removeRandom()); } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started