Need help with this program please! The classes are related so I have no choice
ID: 3918161 • Letter: N
Question
Need help with this program please! The classes are related so I have no choice but to post all sub questions for this program. If you can include comments as much as possible for better understanding that would be great.
Im not sure if number 4 is related to the first 3 parts, if it isint then discard it no need to answer that part! thanks will upvote!
Student # 101047893
CARD CLASS:
public abstract class Card implements Comparable<Card>{
/* handy arrays for ranks and suits */
/* do NOT change these */
public final static String[] RANKS = { "None", "Joker",
"2", "3", "4", "5", "6", "7", "8", "9", "10",
"Jack", "Queen", "King", "Ace"};
public final static String[] SUITS = { "Diamonds",
"Clubs", "Hearts", "Spades", "NONE"};
/** creates a card with specified suit and rank
*
* @param suit is the suit of the card (must be a string from Card.SUITS)
* @param rank is the rank of the card (must be a string from Card.RANKS)
*/
public Card(String suit, String rank){
// add code here if needed
}
/** the numerical representation of the rank of the current card
* <p>
* ranks have the numerical values
* Joker = 1,
* 2 = 2, 3 = 3, ..., 10 = 10
* Jack = 11, Queen = 12, King = 13, Ace = 14
*
* @return the numerical rank of this card
*/
public abstract int getRank();
/** the string representation of the rank of the current card
*
* @return the string representation of the rank of this card (must be a string from Card.RANKS)
*/
public abstract String getRankString();
/** the suit of the current card
*
* @return the suit of this card (must be a string from Card.SUITS)
*/
public abstract String getSuit();
@Override
public final String toString(){
// outputs a string representation of a card object
int r = getRank();
if( r >= 2 && r <= 14 ){
return r + getSuit().substring(0,1);
}
if (r == 1){
return "J";
}
return "invalid card";
}
}
HAND CLASS:
import java.util.List;
public class Hand{
protected List<Card> cards;
public Hand(List<Card> cards){
this.cards = cards;
}
public int numberOfCards(){
if( this.cards == null ){
return -1;
}else{
return this.cards.size();
}
}
public List<Card> getCards(){ return this.cards; }
/* remove and return the specified card from the hand */
/* return null if the card is not in the hand */
public Card remove(Card card){return new StandardCard(Card.SUITS[4], Card.RANKS[1]);}
/* add the specified card to the hand */
public void add(Card card){}
}
DECK CLASS:
import java.util.List;
/* finish all of the constructors and methods given here */
/* do NOT add any public attributes or methods */
/* add any protected/private attributes you need */
public class Deck{
public Deck(){}
public Deck(int nun_jokers){}
public List<Card> getCards(int num_cards){return null;}
public Card getCard(){return null;}
public void addCard(Card c){}
}
PLAYER CLASS: **THIS IS FOR NUMBER 4 IF IT IS RELATED TO THE PREVIOUS 3**
public abstract class Player{
protected Hand hand;
public Player(Hand hand){ this.hand = hand; }
/* play a card from the player's hand */
/* returns null if they cannot play a card and the deck becomes empty */
/* do NOT return null unless you cannot play a card and the deck is empty */
public abstract Card play(Card top_of_discard_pile, Deck deck);
public final int cardsLeft(){ return this.hand.numberOfCards(); }
}
Explanation / Answer
Card.java
package com.chegg.solution;
public abstract class Card implements Comparable<Card> {
/* handy arrays for ranks and suits */
/* do NOT change these */
public final static String[] RANKS = { "None", "Joker",
"2", "3", "4", "5", "6", "7", "8", "9", "10",
"Jack", "Queen", "King", "Ace" };
public final static String[] SUITS = { "Diamonds",
"Clubs", "Hearts", "Spades", "NONE" };
protected String suit;
protected String rank;
/**
* creates a card with specified suit and rank
*
*
*
* @param suit
* is the suit of the card (must be a string from Card.SUITS)
*
* @param rank
* is the rank of the card (must be a string from Card.RANKS)
*
*/
public Card(String suit, String rank) {
this.suit = suit;
this.rank = rank;
}
/**
* the numerical representation of the rank of the current card
*
* <p>
*
* ranks have the numerical values
*
* Joker = 1,
*
* 2 = 2, 3 = 3, ..., 10 = 10
*
* Jack = 11, Queen = 12, King = 13, Ace = 14
*
*
*
* @return the numerical rank of this card
*
*/
public abstract int getRank();
/**
* the string representation of the rank of the current card
*
*
*
* @return the string representation of the rank of this card (must be a string
* from Card.RANKS)
*
*/
public abstract String getRankString();
/**
* the suit of the current card
*
*
*
* @return the suit of this card (must be a string from Card.SUITS)
*
*/
public abstract String getSuit();
@Override
public final String toString() {
// outputs a string representation of a card object
int r = getRank();
if (r >= 2 && r <= 14) {
return r + getSuit().substring(0, 1);
}
if (r == 1) {
return "J";
}
return "invalid card";
}
}
Deck.java
package com.chegg.solution;
import java.util.ArrayList;
import java.util.List;
/* finish all of the constructors and methods given here */
/* do NOT add any public attributes or methods */
/* add any protected/private attributes you need */
public class Deck {
private List<Card> cards;
private int numJokers;
public Deck() {
cards = new ArrayList<>();
for(int i=0;i < Card.SUITS.length-1; i++) {
for(int j = 1; j< Card.RANKS.length; j++) {
this.cards.add(new StandardCard(Card.SUITS[i], Card.RANKS[j]));
}
}
}
public Deck(int nun_jokers) {
this();
this.numJokers = nun_jokers;
for(int i = 0; i< numJokers; i++) {
this.cards.add(new StandardCard(1, Card.SUITS[0]));
}
}
public List<Card> getCards(int num_cards) {
List<Card> subList = this.cards.subList(0, num_cards-1);
this.cards.removeAll(subList);
return subList;
}
public Card getCard() {
return this.cards.remove(0);
}
public void addCard(Card c) {
this.cards.add(c);
}
}
Hand.java
package com.chegg.solution;
import java.util.List;
public class Hand {
protected List<Card> cards;
public Hand(List<Card> cards) {
this.cards = cards;
}
public int numberOfCards() {
if (this.cards == null) {
return -1;
} else {
return this.cards.size();
}
}
public List<Card> getCards() {
return this.cards;
}
/* remove and return the specified card from the hand */
/* return null if the card is not in the hand */
public Card remove(Card card) {
Card removingCard = null;
if(this.cards.contains(card)) {
removingCard = this.cards.get(this.cards.indexOf(card));
this.cards.remove(card);
}
return removingCard;
}
/* add the specified card to the hand */
public void add(Card card) {
this.cards.add(card);
}
}
StandardCard.java
package com.chegg.solution;
import java.util.Arrays;
public class StandardCard extends Card {
public StandardCard(String suit, String rank) {
super(suit, rank);
if(Card.RANKS[1].equals(this.rank)) {
this.suit = Card.SUITS[4];
}
}
public StandardCard(int rank, String suit) {
this(suit, Card.RANKS[rank]);
if(rank == 1) {
this.suit = Card.SUITS[4];
}
}
@Override
public int compareTo(Card o) {
// TODO Auto-generated method stub
return 0;
}
@Override
public int getRank() {
int index = 0;
for(int i = 0; i< Card.RANKS.length; i++) {
if(this.rank.equals(Card.RANKS[i])) {
index = i;
}
}
return index;
}
@Override
public String getRankString() {
return this.rank;
}
@Override
public String getSuit() {
return this.suit;
}
}