Question

In: Computer Science

assum have a Class enum Suit{ CLUBS , DIAMONDS,SPADES,HEARTS; } Class enum Raml { TWO,THREE,FOUR,FIVE,SIX,SEVEN,EIGHT,NINE,TEN, JACK,QUEEN,KING,...

assum have a
Class enum Suit{
CLUBS , DIAMONDS,SPADES,HEARTS;
}

Class enum Raml {
TWO,THREE,FOUR,FIVE,SIX,SEVEN,EIGHT,NINE,TEN, JACK,QUEEN,KING, ACE;
}

public class Card {
  
   private Suit suit;
   private Rank rank;

   public Card (Rank rank, Suit suit){
  
       this.suit = suit;
       this.rank = rank;

   }

   public Suit getSuit() {
       // TODO Auto-generated method stub
       return suit;
   }

   public Rank getRank() {
       return rank;
   }



Class Hand{
given ArrayList<Card> cards;
public Hand(Card[] cards) creates a hand from the cards in the given array.

public Card playCard(int i): Removes and returns the card with the given index in cards.

   public boolean isSorted () return true if the elements of the cards are in sorted order.

---------------------Java------------------

Solutions

Expert Solution

Program Code Screenshot (Rank.java)

Program Code Screenshot (Suit.java)

Program Code Screenshot (Card.java)

Program Code Screenshot (Hand.java)

Program Code Screenshot (Main.java)

Program Sample Input/Output Screenshot

Program Code to Copy (Rank.java)

public enum Rank {
    TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE
}

Program Code to Copy (Suit.java)

public enum Suit {
    CLUBS, DIAMONDS, SPADES, HEARTS
}

Program Code to Copy (Card.java)

public class Card {

    private Suit suit;
    private Rank rank;

    public Card(Rank rank, Suit suit) {

        this.suit = suit;
        this.rank = rank;

    }

    public Suit getSuit() {
        return suit;
    }

    public Rank getRank() {
        return rank;
    }
}

Program Code to Copy (Hand.java)

import java.util.ArrayList;

public class Hand {
    ArrayList<Card> cards;

    public Hand(Card[] cards) {
        this.cards = new ArrayList<Card>();
        // creates a hand from the cards in the given array.
        for (int i = 0; i < cards.length; i++) {
            this.cards.add(cards[i]);
        }

    }

    public Card play(int i) {
        // Removes and returns the card with the given index in cards.
        Card x = cards.get(i);
        cards.remove(x);
        return x;
    }

    public boolean isSorted() {
        // return true if the elements of the cards are in sorted order.
        if (cards.size() == 0)
            return true;

        // check if elements are sorted by their rank
        Rank last = cards.get(0).getRank();
        for (int i = 1; i < cards.size(); i++) {
            if (cards.get(i).getRank().ordinal() < last.ordinal()) {
                return false;
            }
        }
        return true;
    }
}

Program Code to Copy (Main.java)


class Main {
    public static void main(String[] args) throws Exception {
        Card[] cards = new Card[] { new Card(Rank.ACE, Suit.CLUBS), new Card(Rank.KING, Suit.CLUBS),
                new Card(Rank.JACK, Suit.CLUBS), new Card(Rank.QUEEN, Suit.CLUBS), new Card(Rank.SEVEN, Suit.CLUBS) };

        Hand h1 = new Hand(cards);

        System.out.println("Created a hand with following cards");
        System.out.println("Ace of CLubs");
        System.out.println("King of CLubs");
        System.out.println("Jack of CLubs");
        System.out.println("Queen of CLubs");
        System.out.println("Seven of CLubs");
        System.out.println("Playing card at position 3 ");
        Card x = h1.play(3);
        System.out.println("Card played is : " + x.getRank() + " of " + x.getSuit());

        System.out.println();
        System.out.println("isSorted() -> " + h1.isSorted());
    }
}

Related Solutions

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT