Need help with these classes please! will upvote! Student # 101047681 Deck Class
ID: 3917557 • Letter: N
Question
Need help with these classes please! will upvote!
Student # 101047681
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){}
}
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){}
}
Explanation / Answer
needs Card class for this..please comment the Card class..if i was to assume Card class then
/**
* Card
*/
public class Card {
String card;
String suit;
Card(String c,String suit)
{
this.card=c;
this.suit=suit;
}
}
/*
Deck
*/
import java.util.*;
/* 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{
List<Card> deck= new ArrayList<Card>();
public Deck(){
String[] suits={"spade","leaf","heart","diamond"};
for(String var:suits)
{
for(int i=0;i<13;i++)
{
if(i+1==1)
this.deck.add(new Card("A",var));
else if(i+1==11)
this.deck.add(new Card("J",var));
else if(i+1==12)
this.deck.add(new Card("Q",var));
else if(i+1==13)
this.deck.add(new Card("K",var));
else
this.deck.add(new Card(String.valueOf(i+1),var));
}
}
}
public Deck(int nun_jokers){
String[] suits={"spade","leaf","heart","diamond"};
for(String var:suits)
{
for(int i=0;i<13;i++)
{
if(i+1==1)
this.deck.add(new Card("A",var));
else if(i+1==11)
this.deck.add(new Card("J",var));
else if(i+1==12)
this.deck.add(new Card("Q",var));
else if(i+1==13)
this.deck.add(new Card("K",var));
else
this.deck.add(new Card(String.valueOf(i+1),var));
}
}
for(int i=0;i<nun_jokers;i++)
this.deck.add(new Card("JOKER",null));
}
public List<Card> getCards(int num_cards){
List<Card> sets=this.deck.subList(0, num_cards);
for(int i=0;i<num_cards;i++)
this.deck.remove(i);
return sets;
}
public Card getCard(){
card cd=this.deck.get(0);
this.deck.remove(0);
return cd;
}
public void addCard(Card c){
this.deck.add(c);
}
}
/*
Hand
*/
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){
if(this.cards.contains(card))
{ int ind=this.cards.indexOf(card);
Card cd=this.cards.get(ind);
this.cards.remove(card);
return cd;
}
else
return null;
}
/* add the specified card to the hand */
public void add(Card card){
this.cards.add(card);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.