Skip to content

Activity 7: Exceptions

Instructions

Example Code

Model 1

1
2
3
4
5
6
7
8
public class Test {

    public static void main(String[] args) {
        Card c1 = new Card(1, 2);
        System.out.println(c1);
    }

}
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
public class Card {

    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"};

    private final int rank;

    private final int suit;

    /**
     * Constructs a card of the given rank and suit.
     * 
     * @param rank the rank
     * @param suit the suit
     */
    public Card(int rank, int suit) {
        if (rank < 1 || rank > 13) {
            throw new IllegalArgumentException("invalid rank: " + rank);
        }
        if (suit < 0 || suit > 3) {
            throw new IllegalArgumentException("invalid suit: " + suit);
        }
        this.rank = rank;
        this.suit = suit;
    }

    /**
     * Constructs a card of the given rank and suit.
     *
     * @param rank string representation of the rank
     * @param suit string representation of the suit
     */
    public Card(String rank, String suit) {
        this.rank = parseRank(rank);
        this.suit = parseSuit(suit);
    }

    /**
     * Compares this card with that card.
     * 
     * @param that the card to compare
     * @return -1 if this card comes before that card, 0 if the two cards are
     *     equal, or +1 if this card comes after that card
     */
    public int compareTo(Card that) {
        int result;
        if (this.suit < that.suit) {
            result = -1;
        } else if (this.suit > that.suit) {
            result = 1;
        } else if (this.rank < that.rank) {
            result = -1;
        } else if (this.rank > that.rank) {
            result = 1;
        } else {
            result = 0;
        }
        return result;
    }

    /**
     * Returns true if the given card has the same rank and same suit.
     * 
     * @param obj the other Card
     * @return true if the two are equal; false otherwise
     */
    @Override
    public boolean equals(Object obj) {
        if (obj instanceof Card) {
            Card that = (Card) obj;
            return this.rank == that.rank && this.suit == that.suit;
        }
        return false;
    }

    public int getRank() {
        return this.rank;
    }

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

    /**
     * Determines the integer rank from the given string rank.
     *
     * @param str the string representation of the rank
     * @return the rank integer, or -1 if invalid
     */
    public static int parseRank(String str) {
        for (int i = 0; i < RANKS.length; i++) {
            if (str.equals(RANKS[i])) {
                return i;
            }
        }
        return -1;
    }

    /**
     * Determines the integer suit from the given string suit.
     *
     * @param str the string representation of the suit
     * @return the suit integer, or -1 if invalid
     */
    public static int parseSuit(String str) {
        for (int i = 0; i < SUITS.length; i++) {
            if (str.equals(SUITS[i])) {
                return i;
            }
        }
        return -1;
    }

    /**
     * Gets the card's index in a sorted deck of 52 cards.
     * 
     * @return index of the card
     */
    public int position() {
        return this.suit * 13 + this.rank - 1;
    }

    @Override
    public String toString() {
        return RANKS[this.rank] + " of " + SUITS[this.suit];
    }

}

Model 2

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import java.util.Scanner;

public class User {

    public static void main(String[] args) {

        // read input from the user
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a rank: ");
        String rankStr = in.nextLine();
        System.out.print("Enter a suit: ");
        String suitStr = in.nextLine();

        // try to construct the card
        Card card;
        try {
            int rankInt = Integer.parseInt(rankStr);
            int suitInt = Integer.parseInt(suitStr);
            card = new Card(rankInt, suitInt);
        } catch (NumberFormatException exc) {
            card = new Card(rankStr, suitStr);
        }
        System.out.println("Your card is: " + card);
    }

}