Expand|Select|Wrap|Line Numbers
- import java.util.*;
- public class Card
- {
- // instance variables
- //suits
- private int suit;
- private int spades;
- private int hearts;
- private int diamonds;
- private int clubs;
- //values
- private int value;
- private int ace;
- private int jack;
- private int queen;
- private int king;
- // constants representing the suits and values
- public static final int SPADES = 3;
- public static final int HEARTS = 2;
- public static final int DIAMONDS = 1;
- public static final int CLUBS = 0;
- public static final int ACE = 1;
- public static final int JACK = 11;
- public static final int QUEEN = 12;
- public static final int KING = 13;
- //constructors
- //constructor that creates a random, valid card
- public Card()
- {
- Random gen = new Random();
- suit = gen.nextInt(4);
- spades = 3;
- hearts = 2;
- diamonds = 1;
- clubs = 0;
- value = gen.nextInt(13);
- ace =1;
- jack = 11;
- queen = 12;
- king = 13;
- }
- //constructor that accepts two parameters.
- //First parameter is the suit
- //Second parameter is the card value
- public Card(int suit, int value)
- {
- this.suit = suit;
- this.spades = spades;
- this.diamonds = diamonds;
- this.clubs = clubs;
- this.ace = ace;
- this.jack = jack;
- this.queen = queen;
- this.king = king;
- }
- //override equals() method inherited from object
- public boolean equals(Object obj)
- {
- //check to see if object is a reference to the cards
- //and if so, check if its value and suit equals the currecnt value and suit
- if(obj instanceof Card)
- {
- Card crd = (Card) obj;
- if ((value == crd.value) && (suit == crd.suit)) return true;
- else return false;
- }
- else return false;
- }
- // override close () method from the object class
- public Object clone()
- {
- //create card that has the same instant values as the current card
- Card newCard = new Card();
- newCard.value = value;
- newCard.suit = suit;
- return newCard;
- }
- //a compareValue() method that takes another card object and
- //returns true if the current object has a greater value and
- //false otherwise.
- public boolean compareValue(Card other)
- {
- if(value >= value)
- {
- return true;
- }
- else return false;
- }
- //Getter method
- public String getSuitAsString()
- {
- //Return a String representing the card's suit
- switch ( suit )
- {
- case SPADES: return "Spades";
- case HEARTS: return "Hearts";
- case DIAMONDS: return "Diamonds";
- case CLUBS: return "Clubs";
- default: return "??";
- }
- }
- public String getValueAsString()
- {
- //Return a String representing the card's value
- switch ( value )
- {
- case 1: return "Ace";
- case 2: return "2";
- case 3: return "3";
- case 4: return "4";
- case 5: return "5";
- case 6: return "6";
- case 7: return "7";
- case 8: return "8";
- case 9: return "9";
- case 10: return "10";
- case 11: return "Jack";
- case 12: return "Queen";
- case 13: return "King";
- default: return "??";
- }
- }
- public String toString()
- {
- //Return a String representation of this card
- return getValueAsString() + " of " + getSuitAsString();
- }
- //Getter method for the value
- public String getValue()
- {
- if (value == ace)
- return "Ace";
- else if (value == jack)
- return "Jack";
- else if (value == queen)
- return "Queen";
- else if (value == king)
- return "King";
- }
- //Getter method for the suit
- public String getSuit()
- {
- if (suit == spades) return "Spades";
- else if (suit == hearts) return "Hearts";
- else if (suit == diamonds) return "Diamonds";
- else if (suit == clubs) return "Clubs";
- }
- }