2014-02-05 2 views
0

저는 Java에 익숙하지 않습니다. 교수님이 우리에게 주신 과제에 압도 당하고 있습니다. 그가 과제를 위해 미리 지정한 클래스와 메소드를 사용하는 방법을 정말로 이해합니다. Card 별도의 클래스의 이름입니다printHand (Card [] hand) 메서드 사용 방법 "Card"는 별도 클래스의 이름입니다.

public static void printHand(Card[] hand) 

:

나는 방법을 사용하기 위해 메소드 주에 제출해야하는 자료는 무엇인가요? 누구든지이 질문에 대한 간단한 설명을 줄 수 있다면 매우 감사 할 것입니다. 문자열

에서 System.out.println을 당신은 카드 클래스 느릅 나무에 toString 메소드를 사용

public class Card { 

    // Constants for representing the 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 for representing values of 
    // ace, jack, queen, and 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 will keep them from being changed 
    // after cards are constructed. 
    private final int value; 
    private final int suit; 

    /** 
    * Constructs a card with a specified suit and value. 
    * 
    * @param value 
    *   the value of the card. 2 through 10 are used to specify the 
    *   cards with those corresponding values, and constants exist for 
    *   specifying ace, jack, queen, and king 
    * @param suit 
    *   the suit of the card. Use one 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 a new card with the same value and suit as the original. 
    * @param original the card to be copied 
    */ 
    public Card(Card original) { 
     this(original.value, original.suit); 
    } 

    /** 
    * Gets this card's suit. 
    * 
    * @return the suit of this card 
    */ 
    public int getSuit() { 
     return suit; 
    } 

    /** 
    * Gets this card's value 
    * 
    * @return the value of this card 
    */ 
    public int getValue() { 
     return value; 
    } 

    /** 
    * Gets a letter representing the suit 
    * 
    * @return a single letter, either "C", "H", "S", or "D", representing 
    *   clubs, hearts, spades, and diamonds respectively 
    */ 
    private String getSuitString() { 
     return "" + "CHSD".charAt(suit); 
    } 

    /** 
    * Gets a one- or two-character string representing the 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 two cards have the same suit and value 
    * 
    * @param other 
    *   the other object to be compared 
    * @return true if the other object is a card with the same suit and value 
    *   as this card 
    */ 
    public boolean equals(Object other) { 
     if (!(other instanceof Card)) 
      return false; 
     if (this == other) { 
      return true; 
     } 

     Card that = (Card) other; 

     return this.suit == that.suit && this.value == that.value; 
    } 

    /** 
    * Returns a String representation of this card, by combining its value and 
    * suit (see getValueString() and getSuitString) 
    * 
    * @return a 2- or 3-character representation of this card (such as "JD" for 
    *   the jack of diamonds, or "10H" for the 10 of hearts 
    */ 
    public String toString() { 
     return getValueString() + getSuitString(); 
    } 
} 
+0

음, printHand()가'정적',이 클래스의 인스턴스를하지 않고 메인에서 호출 할 수 있기 때문이다. 'Card'를'Card.java'라는 별개의 파일에 정의하고 그 클래스의 인스턴스를 생성하고 그것을 메인 클래스 내부의'printHand()'메소드에 전달해야합니다. –

+0

'Card'는 클래스입니다. '[]'는 배열을 나타냅니다. 매개 변수는'Card' 객체의 배열입니다. –

답변

0

반환 (카드 : 그것은 질문에 대답에 어떤 도움이 경우

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 the following cards"); 

     for(Card card : hand) { 

      System.out.println(card.toString()); 
     } 
    } 

} 
0

먼저 당신은 정적 방법은 다른 정적 메서드 호출 할 수 있기 때문에, 주요 방법이나 할 것 정적 방법에있는 카드의 배열 (Card[])를 만들어야합니다.

그런 다음 해당 카드 배열을 printHand() 방법으로 전달할 수 있습니다.이 방법은 카드 배열에 추가 된 각 카드를 차례대로 인쇄해야합니다. 카드 배열에 Card 개체의 인스턴스를 채워야합니다.

public static void printHand(Card[] hand) { 
    for (Card card : hand) { 
     System.out.println(card); 
    } 
} 

public static void main(String[] args) { 
    Card[] cards = { 
     new Card(Card.ACE, Card.HEARTS), 
     new Card(Card.KING, Card.HEARTS), 
     new Card(Card.QUEEN, Card.HEARTS), 
     new Card(Card.JACK, Card.HEARTS), 
     new Card(Card.QUEEN, Card.SPADES) 
    }; 

    printHand(cards); 
} 

출력 :

AH 
KH 
QH 
JH 
QS 
관련 문제