Print Page Print Page

Ch12 – Card Objects

  1. Create a new subdirectory named cards and change to that directory.
  2. Create a new class named Card. Add a constructor and two getter methods.
public class Card {
  private int rank;
  private int suit;
  
  public int getRank(){        
    return this.rank;
  }

  public int getSuit(){
    return this.suit;
  }

  public Card(int rank, int suit){
    this.rank = rank;
    this.suit = suit;
  }
}
  1. Create a new Class named CardDriver.
  2. Add a main method that creates a Card and then tests its values
public class CardDriver {
  public static void main(String[] args){
    Card myCard = new Card(1,2);
    System.out.println(myCard.getRank());
    System.out.println(myCard.getSuit());
  }
}
  1. Compile CardDriver.java and run the program. Note that compiling CardDriver also results in Card being compiled.
$ javac CardDriver.java
$ java CardDriver
1
2