Advertisement
Guest User

Card.java

a guest
Sep 16th, 2014
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.58 KB | None | 0 0
  1. /**
  2.  * Card class (Card.java)
  3.  *
  4.  * This file aims to create a card class, with three attributes: its rank, suit, and point value.
  5.  * The class has a method to display these three attributes, and also has a method to compare with other cards.
  6.  * <code>Card</code> represents a playing card.
  7.  */
  8. public class Card {
  9.  
  10.     /**
  11.      * String value that holds the suit of the card
  12.      */
  13.     private String suit;
  14.  
  15.     /**
  16.      * String value that holds the rank of the card
  17.      */
  18.     private String rank;
  19.  
  20.     /**
  21.      * int value that holds the point value.
  22.      */
  23.     private int pointValue;
  24.  
  25.  
  26.    /**
  27.      * Creates a new <code>Card</code> instance.
  28.      *
  29.      * @param cardRank  a <code>String</code> value
  30.      *                  containing the rank of the card
  31.      * @param cardSuit  a <code>String</code> value
  32.      *                  containing the suit of the card
  33.      * @param cardPointValue an <code>int</code> value
  34.      *                  containing the point value of the card
  35.      */
  36.     public Card(String cardRank, String cardSuit, int cardPointValue) {
  37.         suit = cardSuit;
  38.         rank = cardRank;
  39.         pointValue = cardPointValue;
  40.     }
  41.  
  42.  
  43.     /**
  44.      * Accesses this <code>Card's</code> suit.
  45.      * @return this <code>Card's</code> suit.
  46.      */
  47.     public String suit() {
  48.         return suit;
  49.    }
  50.  
  51.     /**
  52.      * Accesses this <code>Card's</code> rank.
  53.      * @return this <code>Card's</code> rank.
  54.      */
  55.     public String rank() {
  56.         return rank;
  57.     }
  58.  
  59.    /**
  60.      * Accesses this <code>Card's</code> point value.
  61.      * @return this <code>Card's</code> point value.
  62.      */
  63.     public int pointValue() {
  64.         return pointValue;
  65.     }
  66.  
  67.     /** Compare this card with the argument.
  68.      * @param otherCard the other card to compare to this
  69.      * @return true if the rank, suit, and point value of this card
  70.      *              are equal to those of the argument;
  71.      *         false otherwise.
  72.      */
  73.     public boolean matches(Card otherCard) {
  74.         if(this.suit() == otherCard.suit()
  75.                 && this.rank() == otherCard.rank()
  76.                 && this.pointValue() == otherCard.pointValue()){
  77.             return true;
  78.            
  79.         }
  80.         else{
  81.             return false;
  82.         }
  83.     }
  84.  
  85.     /**
  86.      * Converts the rank, suit, and point value into a string in the format
  87.      *     "[Rank] of [Suit] (point value = [PointValue])".
  88.      * This provides a useful way of printing the contents
  89.      * of a <code>Deck</code> in an easily readable format or performing
  90.      * other similar functions.
  91.      *
  92.      * @return a <code>String</code> containing the rank, suit,
  93.      *         and point value of the card.
  94.      */
  95.     @Override
  96.     public String toString() {
  97.         return String.format("[%s] of [%s] (point value = %d)", this.rank, this.suit, this.pointValue);
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement