/* * Card.java * * Computer Science 112, Boston University * * A blueprint class for objects that represent a single playing card. */ public class Card { // Constant array containing the string representations // of all of the card ranks. // The string for the numeric rank r is given by RANK_STRINGS[r]. public static final String[] RANK_STRINGS = { "", "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" }; /* * rankNumFor: * * Understand the difference between == and .equals on reference types * */ public static int rankNumFor(String rankString) { int index = -1; for (int i = 1; i < RANK_STRINGS.length; i++) { if (RANK_STRINGS[i] == rankString) { index = i; break; } } return index; } // Simple test main // public static void main( String[] args ) { // Test 1 // // This test will seem to work correctly and produce the expected results, but only because // the array and the input passed to the methid are // literal strings (referencing the same string object in the constant pool). // System.out.println( "Test #1" ); System.out.println( rankNumFor( "J" ) ); System.out.println( rankNumFor( "A" ) ); System.out.println( rankNumFor( "6" ) ); // Test 2 // // This test will not work because the inputs being passed to // the method are new String objects. // System.out.println( "Test #2" ); System.out.println( rankNumFor( new String("J") ) ); System.out.println( rankNumFor( new String("A") ) ); System.out.println( rankNumFor( new String("6") ) ); } }