The to String() method for the Card class works differently from how we normally define this...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
The to String() method for the Card class works differently from how we normally define this method. Simply return a short representation of the card, for example: . "Ah" for the ace of hearts "3d" for the three of diamonds "10c" for the 10 of clubs "Qs" for the queen of spades As you can see, the String value returned from to String() is always 2 or 3 characters in length. Hint: This toString() method becomes super easy to write if you include two arrays in your Card class: • private static final String [] SUITLETTER = {"c","d", "s", "h"); Plus a similar array called FACELETTER with 13 short strings. Do you remember what the word 'static' means in that context? It means that each individual Card object does not require its own copy of SUITLETTER. It's enough to have one copy of that array that is shared between any Card objects you happen to create. 837 Also write a Deck class. The Deck class has two instance variables: An array of 52 Card objects currentCard - An int value. When this number is in the range 0-51, it represents the position of the next card to be dealt from the deck. Once all 52 cards have been dealt, this number becomes 52, thereafter indicating that there are no more cards to deal (at least until the deck is shuffled again). ● ● The Deck constructor has no parameter. It loops to create all 52 Cards in a standard deck and inserts them into the array in the order of Ace-to-King, of clubs, diamonds, spades, hearts, in that order. Thus the order is Ac, 2c, 3c... all the way to Qh, Kh. In terms of the (face, suit) values for the Cards, that's the same as (0,0), (1,0), (2,0) ... all the way to (11,3), (12,3). It's those face and suit int values you need to generate to create the Card objects, since that is what the Card constructor accepts as parameters. The Deck class includes the following methods: shuffle() - reorders the cards within the Deck and resets currentCard to zero. Use the following algorithm for shuffling your deck: for each Card in the deck, swap this card with a card in a randomly selected position. This means you only have to loop through the cards once to shuffle the deck. dealCard - If currentCard is less than the number of cards, return the Card object in the position indicated by currentCard, and increase currentCard by one. Otherwise return null. Note that "dealing" a card doesn't remove it from the Deck the only change is to update currentCard to a new position. - toString() - Concatenates together the toString() results for every card in the deck. Insert two spaces between each card. Insert a newline character after the 13th, 26th, an 39th card, so that printing the result shows up as four lines of thirteen cards. You can see a few examples in the sample output shown on the next page. Also write a DeckTest class to try out your Card and Deck classes. This test class should: 1. Create a Deck and display it (using toString) in its original, unshuffled state. 2. Shuffle the deck and display it again. 3. Show the cards returned by dealing the first five cards from the deck. (You can visually compare with the shuffled deck display to ensure your dealCard() method is working properly.) 4. Loop to deal the rest of the cards in the deck, except for the very last card, but with no output. 5. Deal the last card and print it so you can confirm you are getting the last card in the shuffled deck. 6. Use an if-else statement to test one more call to dealCard(). Confirm whether or not you get back the appropriate result, and display an appropriate message either way 7. Shuffle the deck and display it again. 8. Deal 5 more cards and display them, which allows you to confirm that the shuffle reset the value of currentCard appropriately. See the next page for sample output from my version of Deck Test. The order of the cards in your output should differ, of course, since each shuffle is random. Part B: A Deck of Cards A standard deck of playing cards includes 52 cards - 13 each in clubs, diamonds, spades, and hearts. The 13 cards in each suit are the Ace, 2 through 10, Jack, Queen, and King. Write a Card class that represents a single playing card. This class has two instance variables: ● face - An int value 0-12 (This works better with arrays than 1-13) O represents the Ace 1 represents 2 2 represents 3 10 represents the Jack 11 represents the Queen 12 represents the King suit - An int value 0-3 - clubs, diamonds, spades, hearts in that order 1030 Include a standard constructor, as well as getFace() and getSuit() methods that return the int values held in the instance variables. Your output from Decktest should resemble the following: *** Unshuffled Deck *** Ac Jc Ad 2c 3c 4c 5c 6c 7c 8c 9c 10c JC Qc Kc 2d 3d 4d 5d 6d 7d 8d 9d 10d Jd Qd Kd As 2s 3s 3s 4s 5s 6s 7s 8s 9s 10s Js Qs Ks Ah 2h 3h 4h 5h 6h 7h 8h 9h 10h Jh Qh Kh **** Shuffled Deck **** 9s Jd 2d Ac Jc 2h 10s Kd 3s 5s 2c Qs 4d Ah 5h 10h Qd 6c 5c 7s 8d 5d sa 3c 9h 3d 4c Jh Ks 10d 7c 4s Ad 9c Js 2s 4h Qc Kc 10c 3h 6h 8s 9d 7d 8h As 7h Qh 6s 8c Kh 6d **** Dealing the first five cards **** 2d Ac Jc 2h Jd **** Dealing the last card from the deck **** 6d **** Attempting to deal one more **** Correct dealCard () returned null Ah 2d **** Re-shuffled Deck **** 5h 3s Ac Ac 6s 6h Kc 7d 6d 4d Qc Jd 2h 8h Ks Js Jh As 5d 7c 10d 5s Qh 10c 9d 4h 6c Qs Kh 8d Ad 4s 8s 3d 3c Jc 7h Qd 2s 7s 5c 9c 8c 2c 9s Kd 4c 10s 9h 3h 10h **** Dealing the first five cards **** Qc Jd 2h 8h 5h Bonia Cux keb BE362 sritis tuo The to String() method for the Card class works differently from how we normally define this method. Simply return a short representation of the card, for example: . "Ah" for the ace of hearts "3d" for the three of diamonds "10c" for the 10 of clubs "Qs" for the queen of spades As you can see, the String value returned from to String() is always 2 or 3 characters in length. Hint: This toString() method becomes super easy to write if you include two arrays in your Card class: • private static final String [] SUITLETTER = {"c","d", "s", "h"); Plus a similar array called FACELETTER with 13 short strings. Do you remember what the word 'static' means in that context? It means that each individual Card object does not require its own copy of SUITLETTER. It's enough to have one copy of that array that is shared between any Card objects you happen to create. 837 Also write a Deck class. The Deck class has two instance variables: An array of 52 Card objects currentCard - An int value. When this number is in the range 0-51, it represents the position of the next card to be dealt from the deck. Once all 52 cards have been dealt, this number becomes 52, thereafter indicating that there are no more cards to deal (at least until the deck is shuffled again). ● ● The Deck constructor has no parameter. It loops to create all 52 Cards in a standard deck and inserts them into the array in the order of Ace-to-King, of clubs, diamonds, spades, hearts, in that order. Thus the order is Ac, 2c, 3c... all the way to Qh, Kh. In terms of the (face, suit) values for the Cards, that's the same as (0,0), (1,0), (2,0) ... all the way to (11,3), (12,3). It's those face and suit int values you need to generate to create the Card objects, since that is what the Card constructor accepts as parameters. The Deck class includes the following methods: shuffle() - reorders the cards within the Deck and resets currentCard to zero. Use the following algorithm for shuffling your deck: for each Card in the deck, swap this card with a card in a randomly selected position. This means you only have to loop through the cards once to shuffle the deck. dealCard - If currentCard is less than the number of cards, return the Card object in the position indicated by currentCard, and increase currentCard by one. Otherwise return null. Note that "dealing" a card doesn't remove it from the Deck the only change is to update currentCard to a new position. - toString() - Concatenates together the toString() results for every card in the deck. Insert two spaces between each card. Insert a newline character after the 13th, 26th, an 39th card, so that printing the result shows up as four lines of thirteen cards. You can see a few examples in the sample output shown on the next page. Also write a DeckTest class to try out your Card and Deck classes. This test class should: 1. Create a Deck and display it (using toString) in its original, unshuffled state. 2. Shuffle the deck and display it again. 3. Show the cards returned by dealing the first five cards from the deck. (You can visually compare with the shuffled deck display to ensure your dealCard() method is working properly.) 4. Loop to deal the rest of the cards in the deck, except for the very last card, but with no output. 5. Deal the last card and print it so you can confirm you are getting the last card in the shuffled deck. 6. Use an if-else statement to test one more call to dealCard(). Confirm whether or not you get back the appropriate result, and display an appropriate message either way 7. Shuffle the deck and display it again. 8. Deal 5 more cards and display them, which allows you to confirm that the shuffle reset the value of currentCard appropriately. See the next page for sample output from my version of Deck Test. The order of the cards in your output should differ, of course, since each shuffle is random. Part B: A Deck of Cards A standard deck of playing cards includes 52 cards - 13 each in clubs, diamonds, spades, and hearts. The 13 cards in each suit are the Ace, 2 through 10, Jack, Queen, and King. Write a Card class that represents a single playing card. This class has two instance variables: ● face - An int value 0-12 (This works better with arrays than 1-13) O represents the Ace 1 represents 2 2 represents 3 10 represents the Jack 11 represents the Queen 12 represents the King suit - An int value 0-3 - clubs, diamonds, spades, hearts in that order 1030 Include a standard constructor, as well as getFace() and getSuit() methods that return the int values held in the instance variables. Your output from Decktest should resemble the following: *** Unshuffled Deck *** Ac Jc Ad 2c 3c 4c 5c 6c 7c 8c 9c 10c JC Qc Kc 2d 3d 4d 5d 6d 7d 8d 9d 10d Jd Qd Kd As 2s 3s 3s 4s 5s 6s 7s 8s 9s 10s Js Qs Ks Ah 2h 3h 4h 5h 6h 7h 8h 9h 10h Jh Qh Kh **** Shuffled Deck **** 9s Jd 2d Ac Jc 2h 10s Kd 3s 5s 2c Qs 4d Ah 5h 10h Qd 6c 5c 7s 8d 5d sa 3c 9h 3d 4c Jh Ks 10d 7c 4s Ad 9c Js 2s 4h Qc Kc 10c 3h 6h 8s 9d 7d 8h As 7h Qh 6s 8c Kh 6d **** Dealing the first five cards **** 2d Ac Jc 2h Jd **** Dealing the last card from the deck **** 6d **** Attempting to deal one more **** Correct dealCard () returned null Ah 2d **** Re-shuffled Deck **** 5h 3s Ac Ac 6s 6h Kc 7d 6d 4d Qc Jd 2h 8h Ks Js Jh As 5d 7c 10d 5s Qh 10c 9d 4h 6c Qs Kh 8d Ad 4s 8s 3d 3c Jc 7h Qd 2s 7s 5c 9c 8c 2c 9s Kd 4c 10s 9h 3h 10h **** Dealing the first five cards **** Qc Jd 2h 8h 5h Bonia Cux keb BE362 sritis tuo
Expert Answer:
Answer rating: 100% (QA)
The word static in the context of the Card class means that the arrays SUITLETTER and FACELETTER are ... View the full answer
Related Book For
Microeconomics An Intuitive Approach with Calculus
ISBN: 978-0538453257
1st edition
Authors: Thomas Nechyba
Posted Date:
Students also viewed these programming questions
-
Planning is one of the most important management functions in any business. A front office managers first step in planning should involve determine the departments goals. Planning also includes...
-
Based on the scenario I need to finish four tasks: -Add a moving bullet actor -Shoot a bullet when the player presses the spacebar key -Destroy a rock when the bullet hits the rock -Increase the...
-
Carbon has the electron configuration 1s 2s2p. The two unpaired electrons in the n = 2 level suggests that carbon will form two bonds. We know, however, that carbon forms four bonds in most compounds...
-
On September 30, 2014, its fiscal year end, NS Services recorded an adjusting entry for $2,000 of interest it owes at year end and will include as part of its payment on October 31, 2014. On October...
-
Describe how a wavefunction summarizes the dynamical properties of a system and how those properties may be predicted.
-
Zappos.com is a popular website known mainly for its discounted shoe sales. In 2012, a hacker hacked into the Zappos website in an effort to obtain the personal account information of Zappos...
-
Relevant Costing, Cost-Based Pricing, Cost Behavior, and Net Present Value Analysis for NoFat The purpose of this integrated exercise is to demonstrate how a special sales-relevant decision analysis...
-
1. There are many market disruptions in the news today. (If you get stuck, on the last slide of the PowerPoint, I've given you 2 examples.) Choose any disruptor in the news and, (a) summarize the...
-
1. What should BoB have done to avoid this situation? 2. Should the sales support staff ask the IT department to rewrite the user manual as a maintenance project, or should they request more training...
-
Watch the video, A Childs Mind: How Kids Learn Right from Wrong). Answer each question using complete sentences. For your description of each of the moral development theories, write at least 45...
-
The Wilson Company is considering the purchase of a new warehouse. Wilson can borrow $78,000 from the bank to make the purchase. He will be required to make monthly payments of $2,234. How long will...
-
Consider the quadratic function y = -x+6x-5. What do we know about the graph of this quadratic equation, based on its formula? The vertex is (3,4) and it opens upward. The vertex is (-3,-32) and it...
-
Supervisor: "We have 81% of our team here today and we have 16 team members." Employee: "So, __________ members are missing today.
-
Consider that you have decided to quantify many of the aspects required for financial planning on a personal basis. Accordingly, using your age as of the first day of next month, make all the...
-
As we reflect on the topic of leadership we have wrestled with a few theories these past few weeks. Our readings seem to place some of these theories into categories or boxes to help us better...
-
Consider base class X and derived class Y, where Y is derived from X. The order of destructor execution when an object of type Y is deallocated or goes out of scope is (A) X then Y (B) (C) (D) (E) Y...
-
Express mass density in kg/m3 and weight density in lb/ft3. 1. Find the mass density of a chunk of rock of mass 215 g that displaces a volume of 75.0 cm3 of water. 2. A block of wood is 55.9 in. x...
-
Parents, Children and the Degree of Substitutability across Time: Consider again exactly the same scenario as in exercise 16.6. A: This time, however, suppose that parent and child tastes treat...
-
Suppose that everything in the grade insurance market is as described in exercise 22.1. But instead of taking the asymmetric information as fixed, we will now ask what can happen if students can...
-
In several of the above exercises, we have indicated that an infinite series 1/(1+r )+1/(1+r )2 +1/(1+r )3 +... sums to 1/r . This can (and has, in some of the B-parts of exercises above) been used...
-
Working individually, in pairs, or in small groups, as your instructor directs, a. Look at five of the example student rsums on VisualCV. com. What features do you like? Why? What features would you...
-
All of the following are first paragraphs in prospecting letters written by new college graduates. Evaluate the paragraphs on these criteria: Is the paragraph likely to interest readers and motivate...
-
Working individually, in pairs, or in small groups, as your instructor directs, look at six profiles on LinkedIn. You could use those of your classmates, family members, or local businesspeople. ...
Study smarter with the SolutionInn App