public class Card { public static final int SPADE = 4; public static final int HEART = 3; public static final int CLUB = 2; public static final int DIAMOND = 1; public Card( int suit, int rank ) ... public int suit() ... public String suitStr() ... public int rank() ... public String rankStr() ... public String toString() ... } |
From the definitions of the public methods, we can tell how to use them
|
Other constants are used in a similar fashion.
|
|
The above discussion also applies to the other instance methods (because they are similar).
public class TestCard1 { public static void main(String[] args) { Card x; int r; String s; x = new Card( Card.CLUB, 1); // Create a card r = x.suit(); // Get the suit of a card System.out.println("x.suit() = " + r); r = x.rank(); // Get the rank of a card System.out.println("x.rank() = " + r); s = x.suitStr(); // Get the suit of a card (Str) System.out.println("x.suitStr() = " + s); s = x.rankStr(); // Get the rank of a card (Str) System.out.println("x.rankStr() = " + s); System.out.println(x.toString()); // Convert a card into a String System.out.println(x); // toString() invoked automatically // by Java to convert object to String!!! } } |
Output:
x.suit() = 2 (2 = club) x.rank() = 14 (rank of Ace) x.suitStr() = c (club) x.rankStr() = A (Ace) Ac Ac |
How to run the program:
|
|