Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.26 KB | None | 0 0
  1. package PJ4;
  2.  
  3. import java.util.*;
  4.  
  5.  
  6. //=================================================================================
  7. /** class PlayingCardException: It is used for errors related to Card and
  8. * Decks objects.
  9. * Do not modify this class!
  10. */
  11. class PlayingCardException extends Exception {
  12.  
  13. /* Constructor to create a PlayingCardException object */
  14. PlayingCardException (){
  15. super ();
  16. }
  17.  
  18. PlayingCardException ( String reason ){
  19. super ( reason );
  20. }
  21. }
  22.  
  23.  
  24.  
  25. //=================================================================================
  26. /** class Card : for creating playing card objects
  27. * it is an immutable class.
  28. * Rank - valid values are 1 to 13
  29. * Suit - valid values are 1 to 4
  30. * Do not modify this class!
  31. */
  32. class Card {
  33.  
  34. /* constant suits and ranks */
  35. static final String[] Suit = {"","Clubs", "Diamonds", "Hearts", "Spades" };
  36. static final String[] Rank = {"","A","2","3","4","5","6","7","8","9","10","J","Q","K"};
  37.  
  38. /* Data field of a card: rank and suit */
  39. private int cardRank; /* values: 1-13 (see Rank[] above) */
  40. private int cardSuit; /* values: 1-4 (see Suit[] above) */
  41.  
  42. /* Constructor to create a card */
  43. /* throw PlayingCardException if rank or suit is invalid */
  44. public Card(int suit, int rank) throws PlayingCardException {
  45. if ((rank < 1) || (rank > 13))
  46. throw new PlayingCardException("Invalid rank:"+rank);
  47. else
  48. cardRank = rank;
  49. if ((suit < 1) || (suit > 4))
  50. throw new PlayingCardException("Invalid suit:"+suit);
  51. else
  52. cardSuit = suit;
  53. }
  54.  
  55. /* Accessor and toString */
  56. /* You may impelemnt equals(), but it will not be used */
  57. public int getRank() { return cardRank; }
  58. public int getSuit() { return cardSuit; }
  59. public String toString() { return Rank[cardRank] + " " + Suit[cardSuit]; }
  60.  
  61.  
  62. /* Few quick tests here */
  63. public static void main(String args[])
  64. {
  65. try {
  66. Card c1 = new Card(4,1); // A Spades
  67. System.out.println(c1);
  68. c1 = new Card(1,10); // 10 Clubs
  69. System.out.println(c1);
  70. c1 = new Card(5,10); // generate exception here
  71. }
  72. catch (PlayingCardException e)
  73. {
  74. System.out.println("PlayingCardException: "+e.getMessage());
  75. }
  76. }
  77. }
  78.  
  79.  
  80.  
  81. //=================================================================================
  82. /** class Decks represents : n deck of 52 playing cards
  83. *
  84. * Do not add new data fields!
  85. * Do not modify any methods
  86. * You may add private methods
  87. */
  88.  
  89. public class Decks {
  90.  
  91. /* constructedDeck is used to keep track of original n*52 cards */
  92. private List<Card> constructedDeck;
  93.  
  94. /* gameDeck starts with copying cards from constructedDeck */
  95. /* it is used to play the card game */
  96. /* see reset(): resets gameDeck to constructedDeck */
  97. private List<Card> gameDeck;
  98.  
  99. /**
  100. * Task : Creates n decks of 52 playing cards in constructedDeck
  101. * and copy them to gameDeck.
  102. * @param n number of deck, i.e. n * 52 cards
  103. * @throw PlayingCardException if n < 1
  104. *
  105. * Note: PlayingCardException is a checked exception.
  106. */
  107.  
  108. public Decks(int n) throws PlayingCardException
  109. {
  110. ArrayList<Card> Cards = new ArrayList<>();
  111. constructedDeck=new ArrayList<>(52*n);
  112. gameDeck=new ArrayList<>(constructedDeck.size());
  113.  
  114. while (n>0){
  115. for(int i=0;i<=3;i++){
  116. for(int j=0;j<=12;j++){
  117. Cards.add(new Card(i+1,j+1));
  118. }
  119. }
  120. n--;
  121. }
  122. constructedDeck=(List)Cards.clone();
  123. gameDeck=(List)((ArrayList<Card>) constructedDeck).clone();
  124. // implement this method!
  125. // Use ArrayList for both constructedDeck & gameDeck
  126. }
  127.  
  128.  
  129. /**
  130. * Task: Shuffles cards in gameDeck.
  131. * Hint: Look at java.util.Collections
  132. */
  133. public void shuffle()
  134. {
  135. Collections.shuffle(gameDeck);
  136. // implement this method!
  137. }
  138.  
  139. /**
  140. * Task: Deals cards from the gameDeck.
  141. *
  142. * @param numberCards number of cards to deal
  143. * @return a list containing cards that were dealt
  144. * @throw PlayingCardException if numberCard > number of remaining cards
  145. *
  146. * Note: You need to create ArrayList to stored dealt cards
  147. * and should removed dealt cards from gameDeck
  148. *
  149. */
  150. public List<Card> deal(int numberCards) throws PlayingCardException
  151. {
  152. ArrayList<Card> dealt = new ArrayList<Card>(gameDeck.subList(0,numberCards-1));
  153. if(gameDeck.size()>=numberCards){
  154. while (numberCards>0){
  155. gameDeck.remove(0);
  156. numberCards--;
  157. }
  158. }
  159. else{
  160. throw new PlayingCardException();
  161. }
  162.  
  163. // implement this method!
  164. return dealt;
  165. }
  166.  
  167. /**
  168. * Task: Resets gameDeck by getting all cards from the constructedDeck.
  169. */
  170. public void reset()
  171. {
  172. // implement this method!
  173. gameDeck.clear();
  174. gameDeck = (List)((ArrayList<Card>) constructedDeck).clone();
  175. }
  176.  
  177. /**
  178. * Task: Return number of remaining cards in gameDeck.
  179. */
  180. public int remainSize()
  181. {
  182. return gameDeck.size();
  183. }
  184.  
  185. /**
  186. * Task: Returns a string representing cards in the gameDeck
  187. */
  188. public String toString()
  189. {
  190. return ""+gameDeck;
  191. }
  192.  
  193.  
  194. /* Quick test */
  195. /* */
  196. /* Do not modify these tests */
  197. /* Generate 2 decks of cards */
  198. /* Loop 2 times: */
  199. /* Deal 35 cards for 3 times */
  200. /* Expect exception last time */
  201. /* reset() */
  202.  
  203. public static void main(String args[]) {
  204.  
  205. System.out.println("******* Create two deck of cards *********\n\n");
  206.  
  207. Decks decks=null;
  208. try {
  209. decks = new Decks(2);
  210. } catch (Exception e) {
  211. System.out.println("Exception!");
  212. }
  213.  
  214. for (int j=0; j < 2; j++)
  215. {
  216. System.out.println("\n************************************************\n");
  217. System.out.println("Loop # " + j + "\n");
  218. System.out.println("Before shuffle:"+decks.remainSize()+" cards");
  219. System.out.println("\n\t"+decks);
  220. System.out.println("\n==============================================\n");
  221.  
  222. int numHands = 3;
  223. int cardsPerHand = 35;
  224.  
  225. for (int i=0; i < numHands; i++)
  226. {
  227. decks.shuffle();
  228. System.out.println("After shuffle:"+decks.remainSize()+" cards");
  229. System.out.println("\n\t"+decks);
  230. try {
  231. System.out.println("\n\nHand "+i+":"+cardsPerHand+" cards");
  232. System.out.println("\n\t"+decks.deal(cardsPerHand));
  233. System.out.println("\n\nRemain:"+decks.remainSize()+" cards");
  234. System.out.println("\n\t"+decks);
  235. System.out.println("\n==============================================\n");
  236. }
  237. catch (PlayingCardException e)
  238. {
  239. System.out.println("*** In catch block:PlayingCardException:Error Msg: "+e.getMessage());
  240. }
  241. }
  242.  
  243.  
  244. decks.reset();
  245. }
  246. }
  247.  
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement