Question

In: Computer Science

arraylist<Card> cards; hand(Card [ ] cards) creates a hand from the cards in the given array....

arraylist<Card> cards;
hand(Card [ ] cards) creates a hand from the cards in the given array.

issorted() return true if the elements of the cards are in sorted order.


----java---

Solutions

Expert Solution

Here is the code,

Card.java

public class Card {
    private int number;

    public Card(int number){
        this.number = number;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    @Override
    public String toString() {
        return "Card{" +
                "number=" + number +
                '}';
    }
}

CardPlay.java

import java.util.*;

public class CardPlay {
    // I have assumed card with values that is : (1-10), Jack - 11, Queen - 12, Kind - 13, Ace - 14;
    private List<Card> cards;

    public CardPlay(){
        cards = new ArrayList<>();;
    }

    private void hand(Card[] mCards){
        cards.addAll(Arrays.asList(mCards));
    }

    private boolean issorted(){
        for(int i=1; i<cards.size(); i++){
            if(cards.get(i).getNumber() < cards.get(i-1).getNumber()) return false;
        }
        return true;
    }

    public static void main(String[] args) {
        Card[] mCards = new Card[14];
        mCards[0] = new Card(12);
        mCards[1] = new Card(1);
        mCards[2] = new Card(9);
        mCards[3] = new Card(8);
        mCards[4] = new Card(2);
        mCards[5] = new Card(14);
        mCards[6] = new Card(5);
        mCards[7] = new Card(7);
        mCards[8] = new Card(13);
        mCards[9] = new Card(3);
        mCards[10] = new Card(6);
        mCards[11] = new Card(4);
        mCards[12] = new Card(10);
        mCards[13] = new Card(11);

        CardPlay cardPlay = new CardPlay();
        cardPlay.hand(mCards);

        System.out.println("isSorted? : "+cardPlay.issorted());
    }
}

If you have any doubts, then put in the comments. Also do upvote the solution.


Related Solutions

A hand contains 2 red cards and 3 black cards. A card is selected from the...
A hand contains 2 red cards and 3 black cards. A card is selected from the hand and set aside. Then another card is selected and put with the first card. Let X be the number of black cards drawn from the hand (i.e. set aside) during this process. Find Pr[X = 1].
A five-card poker hand dealt from a standard 52-card deck of playing cards is called a...
A five-card poker hand dealt from a standard 52-card deck of playing cards is called a three-of-a-kind hand if it contains exactly three cards of the same rank (e.g. 3 aces and 2 other cards). How many distinct three-of-a-kind hands can be dealt with? Calculate a numeric answer.
A hand of five cards is drawn from a standard 52 card deck. (a) What is...
A hand of five cards is drawn from a standard 52 card deck. (a) What is the probability of getting only one pair? (b) What is the probability of getting all cards with the same suit?
A 5-card hand is dealt from a perfectly shuffled deck of playing cards. What is the...
A 5-card hand is dealt from a perfectly shuffled deck of playing cards. What is the probability of each of the following events: a) The hand has at least one club b) The hand has at least two cards with the same rank c) The hand has exactly one club or exactly one spade d) The hand has at least one club or at least one spade
In a particular card​ game, each player begins with a hand of 3 ​cards, and then...
In a particular card​ game, each player begins with a hand of 3 ​cards, and then draws 6 more. Calculate the probability that the hand will contain one pair​ (2 cards of one​ value, with the other cards of 7 different​ values).
. If 2 cards are selected from a standard deck of cards. The first card is...
. If 2 cards are selected from a standard deck of cards. The first card is not replaced in the deck before the second card is drawn. Find the following probabilities: a) P(2 Aces) b) P(Queen of hearts and a King) c) P(Q of Hearts and Q of hearts )
Cards: Suppose you draw one card from a single deck of cards. (a) What is the...
Cards: Suppose you draw one card from a single deck of cards. (a) What is the probability that you draw an queen? Round your answer to 3 significant digits*. (b) What is the probability that you draw a heart? Round your answer to 3 significant digits*. (c) What is the probability that you draw the queen of hearts? Round your answer to 3 significant digits*. ............................................... *Significant Digits: Here are some probabilities expressed to 3 significant digits. You start counting...
You are dealt a hand of five cards from a standard deck of 52 playing cards....
You are dealt a hand of five cards from a standard deck of 52 playing cards. Calculate the probability of the given type of hand. (None of them is a recognized poker hand.) (a) Mixed royal meeting: One of each type or royal card (king, queen, jack, ace) of different suites, and a last non-royal card (neither king, queen, jack or ace). Enter a formula. Examples: C(5,3)C(33,3)/C(14,2) C(5,3)C(33,3)C(4,1)/C(100,100) (b) Red and black double royal wedding: A red king, a red...
You are dealt a hand of five cards from a standard deck of 52 playing cards....
You are dealt a hand of five cards from a standard deck of 52 playing cards. Calculate the probability of the given type of hand. (None of them is a recognized poker hand.) 1. Double royal interracial wedding: Two kings of one color, two queens of the other color, and a last non-royal card (neither king, queen, jack or ace). Solve using a formula. Examples:  C(5,3)C(33,3)/C(14,2) or C(5,3)C(33,3)C(4,1)/C(100,100)
A hand of 13 cards is dealt from a standard deck of 52 playing cards. What...
A hand of 13 cards is dealt from a standard deck of 52 playing cards. What is the probability that it contains more spades (♠) than hearts (♡) given that the hand contains at least two spades?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT