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].
Given that you have two cards of the same rank in a five card hand, what...
Given that you have two cards of the same rank in a five card hand, what is the probability that you have, a) a three of a kind b) four of a kind
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 )
Using Java, design a program that creates an array of Card objects, shuffles them, then recursively...
Using Java, design a program that creates an array of Card objects, shuffles them, then recursively sorts them by value and then recursively sorts them by value and suit You will need to create a Card class with the following two fields: • value (a String) • suit (a String) You may include any other fields, constructors, or methods as needed. Your single array will need to contain 52 card objects, one for each value of each suit. The values...
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...
A standard deck of cards contains 52 cards. One card is selected from the deck. (a)...
A standard deck of cards contains 52 cards. One card is selected from the deck. (a) Compute the probability of randomly selecting a jack or ace. ​(b) Compute the probability of randomly selecting a jack or ace or nine. ​(c) Compute the probability of randomly selecting a king or diamond.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT