下面我有关于作业的程序 卡类:
public class Card
{
// instance variables
private int value;
private String suit;
/**
* parameterized Card constructor -- gets called when an object of
* the Card class is instantiated sending a number as an argument -- it
* determines the value and suit of the card based upon the number received
* @param num a number that gets converted to a value between 1 and 13
* and a suit
*/
public Card(int num)
{
int suitNumber;
value = num % 13;
if (value == 0)
value = 13;
suitNumber = num / 13;
if (suitNumber == 0)
suit = new String("clubs");
else if (suitNumber == 1)
suit = new String("diamonds");
else if (suitNumber == 2)
suit = new String("hearts");
else if (suitNumber == 3)
suit = new String("spades");
else
suit = new String("ERROR");
}
/**
* getValue method -- returns what's stored in the instance variable value
* @return the state of the instance variable value
*/
public int getValue()
{
return value;
}
/**
* getSuit method -- returns what's stored in the instance variable suit
* @return a reference to a String that contains the state of the instance
* variable suit
*/
public String getSuit()
{
return suit;
}
/** equalValue method -- determines if the otherCard's value is the same
* as this card's value
* @param otherCard a reference to the Card object (assumes the object has been
* instantiated) to compare to this Card object
* @return true if the values are equal, false if the values are not equal
*/
public boolean equalValue(Card otherCard)
{
if (this.value == otherCard.value)
return true;
else
return false;
}
/**
* equalSuit method -- determines if the otherCards's suit is the same as
* this card's suit
* @param otherCard a reference to the Card object (assumes the object has been
* instantiated) to compare to this Card object
* @return true if the suits are the same, false if they are not
*/
public boolean equalSuit(Card otherCard)
{
if (this.suit.equals(otherCard.suit))
return true;
else
return false;
}
/**
* getPointValue method - this method returns the point value for the
* card - 10 for a face card, the actual value for cards 1 through 10
* @return the point value of the card
*/
public int getPointValue()
{
if (value > 10)
return 10;
else
return value;
}
/**
* equalCard method -- determines if the otherCard has the same value and
* suit as this card
* @param otherCard a reference to a Card object (assumes the object has been
* instantiated) to compare to this Card object
* @return true if the value and suits are the same, false if they are not
*/
public boolean equalCard(Card otherCard)
{
if (this.equalValue(otherCard) && this.equalSuit(otherCard))
return true;
else
return false;
}
/**
* isAFaceCard method -- determines if this card is a face card
* @return true if this card is a face card, false if it is not
*/
public boolean isAFaceCard()
{
if (value > 10)
return true;
else
return false;
}
/**
* toString method -- this method returns the state of the card object
* @return a reference to a String object that contains the values stored
* in the instance variables
*/
public String toString()
{
if (value == 1)
return (new String("Ace of " + suit));
else if (value == 11)
return (new String("Jack of " + suit));
else if (value == 12)
return (new String("Queen of " + suit));
else if (value == 13)
return (new String("King of " + suit));
else
return (new String(value + " of " + suit));
}
}
现在我遇到的麻烦就是关于这个课程
public class Hand
{
// instance variables
private Card[] cards;
private int numCards;
/**
* default Hand constructor --
* allocates an array that is capable of storing at most 10 Card references
* sets the number of cards to 0
*/
public Hand()
{
cards = new Card[10];
numCards = 0;
}
/**
* insertCard method --
* accepts a reference to the Card object to be stored in the next position in the
* cards array - checks to make sure the array isn't full
* @param theCard a reference to the Card object to be stored in the array
*/
public void insertCard(Card theCard)
{
if (numCards < cards.length)
{
cards[numCards] = theCard;
numCards++;
}
}
/**
* getCards method --
* makes a copy of the cards array and returns the address of the copy;
* the size of the array that is returned is based upon the number of
* cards currently in the hand
* @return a reference to the copy of the cards array
*/
public Card [] getCards()
{
Card [] cardsCopy = new Card[cards.length];
for(int i =0; i < cards.length; i++)
{
cardsCopy[i] = cards[i];
}
return cardsCopy;
}
/**
* replaceCard method --
* accepts a reference to a new Card object and the position in which
* the reference should be stored; the position is expected to be in the
* range 0 to (number of cards -1); position is validated to ensure that
* it is in this range
* @param pos the index where the card should be stored
* @param theCard the reference to the Card object to be stored in the array
*/
public void replaceCard(int pos, Card theCard)
{
}
/**
* searchCard method --
* accepts a reference to a Card to search for and determines whether
* or not that card exists in the hand
* @param theCard a reference to the Card to search for
* @return true if the card is found, false otherwise
*/
public boolean searchCard(Card theCard)
{
for(int i = 0; i < numCards; i++)
{
if(theCard.equalCard(cards[i]))
return true;
else
return false;
}
}
/**
* findNumFaceCards method --
* counts the number of face cards (ie. value of 11, 12 or 13) in the hand and
* returns the count
* @return the number of face cards in the hand
*/
public int findNumFaceCards()
{
int count =0;
for(int i =0; i < numCards; i++)
{
if(cards[i].getValue() >= 11)
count ++;
}
return count;
}
/**
* findLowCard method --
* finds and returns the position of the lowest card
* @return the index of the lowest card
*/
public int findLowCard()
{
int lowIndex = 0;
for(int i =0; i < numCards; i++)
{
if(cards[i].getValue() < lowIndex)
{
i = lowIndex;
}
}
return lowIndex;
}
/**
* replaceLowCard method --
* accepts a reference to a Card object and replaces the card
* having the lowest value with the new card
* @param theCard a reference to the Card object that will replace the
* lowest card
*/
public void replaceLowCard (Card theCard)
{
}
/**
* toString method --
* creates and returns a reference to a String with each of the card values
* on a separate line
* @return a reference to a String containing the state of the hand
*/
public String toString()
{
String str = "";
for(int i =0; i < numCards; i++)
str += "Card " + (i + 1) + ": " + cards[i] + "\n";
return str;
}
我正在做replaceCard,searchCard和replacelowCard方法错误(语法错误)。这些方法中的大多数都有我朋友写的描述,以便给出关于在体内写什么的提示。但是,我主要想知道在机构中使用哪些代码的错误。
例如,我还没有学会如何在阵列中搜索卡片。我知道有两种类型的搜索,但我不知道它们是否适合这种情况。
对于replaceLowCard方法,我被告知我需要this.lowCard调用来完成它。
对于replaceCard方法,我根本不知道该怎么做。任何帮助将不胜感激。 (P.S:对于这个节目,牌组中有52张牌。)
答案 0 :(得分:0)
我认为 ArrayList 是您正在寻找的。它的动态大小,因此所有插入 - 删除问题都将消失。做这样的事情:
class Hand {
private List<Card> cards;
private int maxCards = 10;
public Hand() {
this.cards = new ArrayList<Card>();
}
}
然后你可以cards.add(...);
插入,cards.remove(...);
删除等等...你可以阅读更多关于数组列表 here的内容。
我希望这会给你一个有用的提示。