We will also see an example on how such an instance method is used.
|
Note:
|
|
public class TestCard2 { public static void main(String[] args) { Card a = new Card ( Card.CLUB , 1 ); Card b = new Card ( Card.SPADE, 1 ); Card c = new Card ( Card.CLUB , 1 ); if ( a.equals(b) ) System.out.println(a + " is equal to " + b); else System.out.println(a + " is NOT equal to " + b); if ( a.equals(c) ) System.out.println(a + " is equal to " + b); else System.out.println(a + " is NOT equal to " + b); } } |
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; private static final String[] Suit = { "*", "d", "c", "h", "s"}; private static final String[] Rank = { "*", "*", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"}; private byte cardSuit; private byte cardRank; public Card( int suit, int rank ) { if ( rank == 1 ) cardRank = 14; // Give Ace the rank 14 else cardRank = (byte) rank; cardSuit = (byte) suit; } /* ---------------------------------------------------- equal(): Compare if 2 cards are equal ---------------------------------------------------- */ public boolean equals( Card x ) { ... I see ONLY ONE Card object: x ... Where is the other Card object that I need to ... compare with ???? } public String toString() { return ( Rank[ cardRank ] + Suit[ cardSuit ] ); } } |
Some cs170 students are puzzled by the fact that they only see one Card parameter variable
and they need to compare two (2) cards !!!!
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; private static final String[] Suit = { "*", "d", "c", "h", "s"}; private static final String[] Rank = { "*", "*", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"}; private byte cardSuit; private byte cardRank; public Card( int suit, int rank ) { if ( rank == 1 ) cardRank = 14; // Give Ace the rank 14 else cardRank = (byte) rank; cardSuit = (byte) suit; } /* ---------------------------------------------------- equal(): Compare if 2 cards are equal You only see ONE card object: x Students often forget that INSTANCE methods ALWAYS have an IMPLICIT parameter "this" --- that's the other card !!! ---------------------------------------------------- */ public boolean equals( Card x ) { if ( this.cardSuit == x.cardSuit && this.cardRank == x.cardRank ) return ( true ); else return ( false ); } public String toString() { return ( Rank[ cardRank ] + Suit[ cardSuit ] ); } } |
Output:
Ac is NOT equal to As Ac is equal to As |
How to run the program:
|
|
public boolean equals( Card x ) { if ( this.cardSuit == x.cardSuit && this.cardRank == x.cardRank ) return ( true ); else return ( false ); } |
Local and parameter variables defined in equals():
|
|
So we can use the short hand notation and drop the this. object variable without any problems:
public boolean equals( Card x ) { if ( cardSuit == x.cardSuit && cardRank == x.cardRank ) return ( true ); else return ( false ); } |
|