Print Page Print Page

CH12 – Class Variables

  1. If you haven’t completed the previous exercises in Chapter 12 then do so now.
  2. Open Card.java and add the following class variables.
public static final String[] RANKS = {null,"Ace","2","3","4","5","6",
  "7","8","9","10","Jack","Queen","King"};
public static final String[] SUITS = {"Clubs","Diamonds","Hearts","Spades"};
  1. Comment the previous toString method’s content and replace it with the class variables.
  public String toString(){
    // String[] ranks = {null,"Ace","2","3","4","5","6","7","8","9",
    //   "10","Jack","Queen","King"};
    // String[] suits = {"Clubs","Diamonds","Hearts","Spades"};
    // String s = ranks[this.rank] + " of " + suits[this.suit];
    // return s;
    return RANKS[this.rank] + " of " + SUITS[this.suit];
  }
  1. Compile Card.java and run the CardDriver program.
$ javac Card.java
$ java CardDriver
1
2
Ace of Hearts