Card c1 = new Card(); //Ace of spades by default
    Card c2 = new Card(3, "Diamonds"); //3 of diamonds
    System.out.println(c1.toString());
    System.out.println(c2.toString());

Accessing Fields


    Card c1 = new Card(); //Ace of spades by default
    Card c2 = new Card(3, "Diamonds"); //3 of diamonds
    c1.rank = 3; //just changed card into rank 3
    c1.suit = "Diamonds"; //now c1 is a diamond
    System.out.println(c1.toString());
    System.out.println(c2.toString());

Checking Equality


    /* Code from previous slide(s) */
    if(c1 == c2)
      System.out.println("This won't happen!");

    /* Won't work because Card class needs an equals() method */
    if(c1.equals(c2))
      System.out.println("This should happen, but won't")

Equality of cards

 
public class Card{
  /* From previous slide Card class slide */

  /* Checks for equality of two cards */ 
  public boolean equals(Object other) {
    Card otherC = (Card)other;
    return    otherC.rank == this.rank
           && otherC.suit.equals(this.suit);
  }
}

Enums

 
/* An Enum is a variable type that has a finite set of values */
/* Let's use one for the suit of a card */
public enum Suit{
    Hearts, Diamonds, Spades, Clubs; 
  }

Using the enum in the Card class

 
public class Card{
  int rank; //1 (Ace) through 13 (King)
  Suit suit; //"Spades", "Hearts", "Clubs", "Diamonds"

  /* Default constructor. Ace of Sp. is default card */
  public Card() {
    this.rank = 1;
    this.suit = Suit.Spades;
  }
  
  /*
   * Constructor. Allows you to set the cards data when
   * creating it. This is called overloading a method
   */
  public Card(int rank, Suit suit) {
    this.rank = rank;
    this.suit = suit;
  }
  
  /**
   * This is a method, will return a description
   * of this card as a String 
   */
  public String toString() {
    String rank = "";
    switch(this.rank) {
      case 1:
        rank = "Ace";
        break;
      case 11:
        rank = "Jack";
        break;
      case 12:
        rank = "Queen";
        break;
      case 13:
        rank = "King";
        break;
      default:
        rank = "" + this.rank; //number and rank the same
        break;
    }
    
    return rank + " of " + this.suit;   
  }
  
  @Override
  public boolean equals(Object other) {
    Card otherC = (Card)other;
    return otherC.rank == this.rank && otherC.suit == this.suit;
  }
}

Updating the Card class

 
public Class Card{ 
  private int rank; //1 (Ace) through 13 (King)
  private Suit suit; //"Spades", "Hearts", "Clubs", "Diamonds"

  /* Default constructor. Ace of Sp. is default card */
  public Card() {
    this.rank = 1;
    this.suit = suit.Spades;
  }
  
  /*
   * Constructor. Allows you to set the cards data when
   * creating it. This is called overloading a method
   */
  public Card(int rank, Suit suit) {
    this.rank = 1; // in case setRank fails
    this.setRank(rank);
    this.suit = suit;
  }
  
  public int getRank() { return this.rank; }
  public Suit getSuit() { return this.suit; }
  
  public void setRank(int newRank) {
    /* Ignore if trying to set to illegal value */
    if(newRank < 1 || newRank > 13) return;
    
    /* Otherwise, set it */
    this.rank = newRank;
  }
  public void setSuit(Suit newSuit) {
    /* No stress, enum already must have valid value */
    this.suit = newSuit;
  }

  /* Other stuff here */
}