Question

In java please Create an immutable class named Card implementing the Comparable interface that will represent...

In java please Create an immutable class named Card implementing the Comparable interface that will represent a card, have a suit: diamonds - lowest, clubs, hearts, spades - highest) and a rank ( 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A). The class must have a constructor provided with a suit and rank, and the following methods: toString(), getSuit(), getRank(), print(). Create a static method that will randomly generate a card. A card is less than another either if the suite is lower or if they have the suite, then the rank is lower. Please add comments and a run code output pic please to ensure the code ran please. Thank you

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class Card implements Comparable<Card> {

    // Symbolic constants
    public static final int DIAMOND = 0;
    public static final int CLUB = 1;
    public static final int HEART = 2;
    public static final int SPADE = 3;
    private int rank;
    private int suit;

    /**
     * Construct a card of the given rank, suit and whether it is faceup or
     * facedown. The rank is an integer from 2 to 14. Numbered cards (2 to 10)
     * have a rank equal to their number. Jack, Queen, King and Ace have the
     * ranks 11, 12, 13, and 14 respectively. The suit is an integer from 0 to 3
     * for Clubs, Diamonds, Hearts and Spades respectively.
     * 
     * @param rank
     * @param suit
     */
    public Card(int rank, int suit) {
        this.rank = rank;
        this.suit = suit;
    }

    /**
     * @return the rank
     */
    public int getRank() {
        return rank;
    }

    /**
     * @return the suit
     */
    public int getSuit() {
        return suit;
    }

    @Override
    public boolean equals(Object ob) {
        Card c = (Card) ob;
        if (c.rank == rank && c.suit == suit) {
            return true;
        }

        return false;
    }

    @Override
    public int hashCode() {// DO NOT MODIFY
        int hash = 7;
        hash = 31 * hash + this.getSuit();
        return hash;
    }


    public int compareTo(Card c) {
        int a = 0;

        if (c.suit > suit) {
            a = -1;
        } else if (c.suit < suit) {
            a = 1;
        } else {
            if (c.rank > rank) {
                a = -1;
            } else if (c.rank < rank) {
                a = 1;
            } else {
                a = 0;
            }
        }       

        return a;
    }

    /**
     * Return the rank as a String. For example, the 3 of Hearts produces the
     * String "3". The King of Diamonds produces the String "King".
     * 
     * @return the rank String
     */
    public String getRankString() {
        if(rank == 14) {
            return "Ace";
        } else if(rank == 13) {
            return "King";
        } else if(rank == 12) {
            return "Queen";
        } else if(rank == 11) {
            return "Jack";
        }
        return "" + rank;
    }

    /**
     * Return the suit as a String: "Clubs", "Diamonds", "Hearts" or "Spades".
     * 
     * @return the suit String
     */
    public String getSuitString() {
        if (suit == CLUB) {
            return "Clubs";
        }
        if (suit == DIAMOND) {
            return "Diamonds";
        }
        if (suit == HEART) {
            return "Hearts";
        }
        return "Spades";

    }

    /**
     * Return "?" if the card is facedown; otherwise, the rank and suit of the
     * card.
     * 
     * @return the String representation
     */
    @Override
    public String toString() {
        String s = "" + getRankString() + " of " + getSuitString();
        return s;
    }

    public static void main(String[] args) {
        // Create 5 of clubs
        Card club5 = new Card(5, CLUB);
        System.out.println("club5: " + club5);
        Card spadeAce = new Card(14, SPADE);
        Card diamondAce = new Card(14, DIAMOND);
        System.out.println("spadeAce: " + spadeAce);
        System.out.println("club5 compareTo spadeAce: "
                + club5.compareTo(spadeAce));
        System.out.println("diamondAce compareTo spadeAce: "
                + diamondAce.compareTo(spadeAce));
        System.out.println("club5 compareTo club5: " + club5.compareTo(club5));
        System.out.println("club5 equals spadeAce: " + club5.equals(spadeAce));
        System.out.println("club5 equals club5: " + club5.equals(club5));
    }
}


Please upvote, as i have given the exact answer as asked in question. Still in case of any concerns in code, let me know in comments. Thanks!

Add a comment
Know the answer?
Add Answer to:
In java please Create an immutable class named Card implementing the Comparable interface that will represent...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • bblem deals with playing cards. The Card API is given below: public class Card ( suit...

    bblem deals with playing cards. The Card API is given below: public class Card ( suit is "Clubs", "Diamonds", "Bearts", or "Spades" Gene=ination s 2", , "10" יי", ,"פ" ,"8" ,"ר" , "6" ,"5י ,-4" ,"ני- * or "A * value is the value of the card number if the card denominat, *is between 2 and 10; 11 for J, 12 for Q, 13 for K, 14 for A public Card (String suit, string denomination){} 1/returns the suit (Clubs, Diamonds,...

  • I've created a Card class and I'm asked to implement a class called DeckOfCards that stores...

    I've created a Card class and I'm asked to implement a class called DeckOfCards that stores 52 objects of the Card class. It says to include methods to shuffle the deck, deal a card, and report the number of cards left in the deck, and a toString to show the contents of the deck. The shuffle methods should assume a full deck. I also need to create a separate driver class that first outputs the populated deck to prove it...

  • IN JAVA - COMMENT CODE WELL Write a class named Card which will represent a card...

    IN JAVA - COMMENT CODE WELL Write a class named Card which will represent a card from a deck of cards. A card has a suit and a face value. Suits are in order from low to high: Clubs, Diamonds, Hearts and Spades. The card values from low to high: 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, and Ace. Write a Deck class that contains 52 cards. The class needs a method named shuffle that...

  • C++ Your solution should for this assignment should consist of five (5) files: Card.h (class specification...

    C++ Your solution should for this assignment should consist of five (5) files: Card.h (class specification file) Card.cpp (class implementation file) DeckOfCards.h (class specification file) DeckOfCards.cpp (class implementation file) 200_assign6.cpp (application program) NU eelLS Seven UT Diamonds Nine of Hearts Six of Diamonds For your sixth programming assignment you will be writing a program to shuffle and deal a deck of cards. The program should consist of class Card, class DeckOfCards and an application program. Class Card should provide: a....

  • Write a class named Card which will represent a card from a deck of cards. A...

    Write a class named Card which will represent a card from a deck of cards. A card has a suit and a face value. Suits are in order from low to high: Clubs, Diamonds, Hearts and Spades. The card values from low to high: 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, and Ace. Write a Deck class that contains 52 cards. The class needs a method named shuffle that randomly shuffles the cards in the...

  • CS102 : JAVA Object-Oriented Programming. 1 Write a class Card whose instances represent a single playing...

    CS102 : JAVA Object-Oriented Programming. 1 Write a class Card whose instances represent a single playing card from a deck of cards. Playing cards have two distinguishing properties an integer for the rank (1 (corresponding to Ace) ,2,3, 13 (correspond ing to King) and suit (Spades, Hearts, Diamonds, or Clubs). Make the suit an enumerated data type. Include getters and setters and a method that tests if a card is valid. Write a class named Deck whose instances are full...

  • Now, create a Deck class that consists of 52 cards (each card is an instance of...

    Now, create a Deck class that consists of 52 cards (each card is an instance of class Card) by filling in the template below. Represent the suit of cards as a string: "Spades", "Diamonds", "Hearts", "clubs" and the rank of cards as a single character or integer: 2, 3, 4, 5, 6, 7, 8, 9, 10, "J", class Deck: def init (self): pass # Your code here. def draw (self): Returns the card at the top of the deck, and...

  • Write in Java! Do NOT write two different programs for Deck and Card, it should be...

    Write in Java! Do NOT write two different programs for Deck and Card, it should be only one program not 2 separate ones!!!!!! !!!!!!!!!!!!!!!Use at least one array defined in your code and two array lists defined by the operation of your code!!!!!!!!!!!!!!!!!!!!! The array should be 52 elements and contain a representation of a standard deck of cards, in new deck order. (This is the order of a deck of cards new from the box.) The 2 Array lists...

  • I'm currently writing a program based on stub poker and trying to deal cards to the...

    I'm currently writing a program based on stub poker and trying to deal cards to the players, show their hands, and determine the winner, and lastly ask them to if they want to play another hand. I'm probably going to have to write a method to compare hands but i really need to just deal the cards and store in their hands Any help or suggestions? Here are my current classes. public class PlayingCard {    private final Suit suit;...

  • Create a data class named Automobile that implements the Comparable interface. Give the class data fields...

    Create a data class named Automobile that implements the Comparable interface. Give the class data fields for make, model, year, and price. Then add a constructor, all getters, a toString method that shows all attribute values, and implement Comparable by using the year as the criterion for comparing instances. Write a program named TestAutos that creates an ArrayList of five or six Automobiles. Use a for loop to display the elements in the ArrayList. Sort the Arraylist of autos by...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT