Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package PJ4;
- import java.util.*;
- //=================================================================================
- /** class PlayingCardException: It is used for errors related to Card and
- * Decks objects.
- * Do not modify this class!
- */
- class PlayingCardException extends Exception {
- /* Constructor to create a PlayingCardException object */
- PlayingCardException (){
- super ();
- }
- PlayingCardException ( String reason ){
- super ( reason );
- }
- }
- //=================================================================================
- /** class Card : for creating playing card objects
- * it is an immutable class.
- * Rank - valid values are 1 to 13
- * Suit - valid values are 1 to 4
- * Do not modify this class!
- */
- class Card {
- /* constant suits and ranks */
- static final String[] Suit = {"","Clubs", "Diamonds", "Hearts", "Spades" };
- static final String[] Rank = {"","A","2","3","4","5","6","7","8","9","10","J","Q","K"};
- /* Data field of a card: rank and suit */
- private int cardRank; /* values: 1-13 (see Rank[] above) */
- private int cardSuit; /* values: 1-4 (see Suit[] above) */
- /* Constructor to create a card */
- /* throw PlayingCardException if rank or suit is invalid */
- public Card(int suit, int rank) throws PlayingCardException {
- if ((rank < 1) || (rank > 13))
- throw new PlayingCardException("Invalid rank:"+rank);
- else
- cardRank = rank;
- if ((suit < 1) || (suit > 4))
- throw new PlayingCardException("Invalid suit:"+suit);
- else
- cardSuit = suit;
- }
- /* Accessor and toString */
- /* You may impelemnt equals(), but it will not be used */
- public int getRank() { return cardRank; }
- public int getSuit() { return cardSuit; }
- public String toString() { return Rank[cardRank] + " " + Suit[cardSuit]; }
- /* Few quick tests here */
- public static void main(String args[])
- {
- try {
- Card c1 = new Card(4,1); // A Spades
- System.out.println(c1);
- c1 = new Card(1,10); // 10 Clubs
- System.out.println(c1);
- c1 = new Card(5,10); // generate exception here
- }
- catch (PlayingCardException e)
- {
- System.out.println("PlayingCardException: "+e.getMessage());
- }
- }
- }
- //=================================================================================
- /** class Decks represents : n deck of 52 playing cards
- *
- * Do not add new data fields!
- * Do not modify any methods
- * You may add private methods
- */
- public class Decks {
- /* constructedDeck is used to keep track of original n*52 cards */
- private List<Card> constructedDeck;
- /* gameDeck starts with copying cards from constructedDeck */
- /* it is used to play the card game */
- /* see reset(): resets gameDeck to constructedDeck */
- private List<Card> gameDeck;
- /**
- * Task : Creates n decks of 52 playing cards in constructedDeck
- * and copy them to gameDeck.
- * @param n number of deck, i.e. n * 52 cards
- * @throw PlayingCardException if n < 1
- *
- * Note: PlayingCardException is a checked exception.
- */
- public Decks(int n) throws PlayingCardException
- {
- ArrayList<Card> Cards = new ArrayList<>();
- constructedDeck=new ArrayList<>(52*n);
- gameDeck=new ArrayList<>(constructedDeck.size());
- while (n>0){
- for(int i=0;i<=3;i++){
- for(int j=0;j<=12;j++){
- Cards.add(new Card(i+1,j+1));
- }
- }
- n--;
- }
- constructedDeck=(List)Cards.clone();
- gameDeck=(List)((ArrayList<Card>) constructedDeck).clone();
- // implement this method!
- // Use ArrayList for both constructedDeck & gameDeck
- }
- /**
- * Task: Shuffles cards in gameDeck.
- * Hint: Look at java.util.Collections
- */
- public void shuffle()
- {
- Collections.shuffle(gameDeck);
- // implement this method!
- }
- /**
- * Task: Deals cards from the gameDeck.
- *
- * @param numberCards number of cards to deal
- * @return a list containing cards that were dealt
- * @throw PlayingCardException if numberCard > number of remaining cards
- *
- * Note: You need to create ArrayList to stored dealt cards
- * and should removed dealt cards from gameDeck
- *
- */
- public List<Card> deal(int numberCards) throws PlayingCardException
- {
- ArrayList<Card> dealt = new ArrayList<Card>(gameDeck.subList(0,numberCards-1));
- if(gameDeck.size()>=numberCards){
- while (numberCards>0){
- gameDeck.remove(0);
- numberCards--;
- }
- }
- else{
- throw new PlayingCardException();
- }
- // implement this method!
- return dealt;
- }
- /**
- * Task: Resets gameDeck by getting all cards from the constructedDeck.
- */
- public void reset()
- {
- // implement this method!
- gameDeck.clear();
- gameDeck = (List)((ArrayList<Card>) constructedDeck).clone();
- }
- /**
- * Task: Return number of remaining cards in gameDeck.
- */
- public int remainSize()
- {
- return gameDeck.size();
- }
- /**
- * Task: Returns a string representing cards in the gameDeck
- */
- public String toString()
- {
- return ""+gameDeck;
- }
- /* Quick test */
- /* */
- /* Do not modify these tests */
- /* Generate 2 decks of cards */
- /* Loop 2 times: */
- /* Deal 35 cards for 3 times */
- /* Expect exception last time */
- /* reset() */
- public static void main(String args[]) {
- System.out.println("******* Create two deck of cards *********\n\n");
- Decks decks=null;
- try {
- decks = new Decks(2);
- } catch (Exception e) {
- System.out.println("Exception!");
- }
- for (int j=0; j < 2; j++)
- {
- System.out.println("\n************************************************\n");
- System.out.println("Loop # " + j + "\n");
- System.out.println("Before shuffle:"+decks.remainSize()+" cards");
- System.out.println("\n\t"+decks);
- System.out.println("\n==============================================\n");
- int numHands = 3;
- int cardsPerHand = 35;
- for (int i=0; i < numHands; i++)
- {
- decks.shuffle();
- System.out.println("After shuffle:"+decks.remainSize()+" cards");
- System.out.println("\n\t"+decks);
- try {
- System.out.println("\n\nHand "+i+":"+cardsPerHand+" cards");
- System.out.println("\n\t"+decks.deal(cardsPerHand));
- System.out.println("\n\nRemain:"+decks.remainSize()+" cards");
- System.out.println("\n\t"+decks);
- System.out.println("\n==============================================\n");
- }
- catch (PlayingCardException e)
- {
- System.out.println("*** In catch block:PlayingCardException:Error Msg: "+e.getMessage());
- }
- }
- decks.reset();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement