java - How to use the method printHand(Card[] hand) where "Card" is the name of a separate class -
i'm new java , i'm becoming overwhelmed assignment professor gave because don't understand how use classes , methods predesignated assignment.
what need provide in method main in order use method:
public static void printhand(card[] hand)
where card
name of separate class? if give me quick explanation of question, grateful.
this card
class if answering question:
public class card { // constants representing suits public final static int clubs = 0; public final static int hearts = 1; public final static int spades = 2; public final static int diamonds = 3; // constants representing values of // ace, jack, queen, , king. public final static int ace = 1; public final static int jack = 11; public final static int queen = 12; public final static int king = 13; // final keep them being changed // after cards constructed. private final int value; private final int suit; /** * constructs card specified suit , value. * * @param value * value of card. 2 through 10 used specify * cards corresponding values, , constants exist * specifying ace, jack, queen, , king * @param suit * suit of card. use 1 of card.clubs, card.hearts, * card.spades, or card.diamonds */ public card(int value, int suit) { if (value < ace || value > king) { throw new illegalargumentexception("illegal card value: " + value); } if (suit < clubs || suit > diamonds) { throw new illegalargumentexception("illegal card suit: " + suit); } this.value = value; this.suit = suit; } /** * constructs new card same value , suit original. * @param original card copied */ public card(card original) { this(original.value, original.suit); } /** * gets card's suit. * * @return suit of card */ public int getsuit() { return suit; } /** * gets card's value * * @return value of card */ public int getvalue() { return value; } /** * gets letter representing suit * * @return single letter, either "c", "h", "s", or "d", representing * clubs, hearts, spades, , diamonds respectively */ private string getsuitstring() { return "" + "chsd".charat(suit); } /** * gets one- or two-character string representing value * * @return either "2" through "10" or "a", "j", "q", or "k" */ private string getvaluestring() { return "a 2 3 4 5 6 7 8 9 10j q k ".substring(2 * (value - 1), 2 * value).trim(); } /** * returns whether 2 cards have same suit , value * * @param other * other object compared * @return true if other object card same suit , value * card */ public boolean equals(object other) { if (!(other instanceof card)) return false; if (this == other) { return true; } card = (card) other; return this.suit == that.suit && this.value == that.value; } /** * returns string representation of card, combining value , * suit (see getvaluestring() , getsuitstring) * * @return 2- or 3-character representation of card (such "jd" * jack of diamonds, or "10h" 10 of hearts */ public string tostring() { return getvaluestring() + getsuitstring(); } }
you use tostring method in card class wich returns string
system.out.println(card.tostring);
public class main { public static void main(string[] args) { // todo auto-generated method stub card[] myhand = new card[3]; //king of spades myhand[0] = new card(card.king, card.spades); //ace of spades myhand[1] = new card(card.ace, card.spades); //two of hearts myhand[2] = new card(2, card.hearts); printhand(myhand); } public static void printhand(card[] hand) { system.out.println("the hand concists of following cards"); for(card card : hand) { system.out.println(card.tostring()); } } }
Comments
Post a Comment