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...
-
Luis Fantano is a realtor. He buys and sells properties on his own and also earns commission as an agent for buyers and sellers. Fantano organized his business as a corporation on July 10, 2014. The...
-
Is the following an example of social marketing - why or why not? 'An ad which talks about the massive amounts of disposable plastic water bottles thrown out each year and promotes a new brand of...
-
Zeta Safety, Inc., began the year with $15,000 of common stock and $34,000 of retained earnings. On August 5, investors bought $19,000 of additional stock in the business. On October 22, the business...
-
One of the operations in the U.S. Post Office is a mechanical mail sorting operation. In this operation, letter mail is sorted at a rate of one letter per second. The letter is mechanically sorted...
-
Montclair Company is considering a project that will require a $670,000 loan. It presently has total liabilities of $135,000 and total assets of $705,000. 1. Compute Montclair's (a) current...
-
A2 percent increase in the price of milk causes a 4 percent reduction in the quantity demanded of chocolate syrup. What is the cross-price elasticity of demand for chocolate syrup with respect to the...
-
The given vector functions are solutions to the system x'(t) = Ax(t). 8. X1 = 3t 9t X2 = - 8 Determine whether the vector functions form a fundamental solution set. Select the correct choice below...
-
1. Select an actual service company related to or in tourism or hospitality (name of the hotel) and briefly describe what the organization does. Make sure to select an actual company . 2. Review the...
-
For the group of stocks assigned to you, form the minimum variance frontier. What is the minimum attainable annualised standard deviation of the portfolio (using the 2014-2018 data pool): Q2: at an...
-
On 9 th November 2 0 2 1 , Ford and other five car manufacturers agree to sign COP 2 6 commitment, as part of efforts to cut carbon emissions and reduce global warming. You want to investigate...
-
Develop at least three planning metrics that represent different aspects of quality, are appropriate to the tools you chose, and are appropriate to your selected project. The metrics should reflect...
-
4 Part A The electric field at the point x = 5.00 cm and y = 0 points in the positive direction with a magnitude of 8.00 N/C. At the point a = 10.0 cm and y = 0 the electric field points in the...
-
ng acid salts Name these ionic compounds: compound NaHPO3 Mg(HCO3) MgHPO3 Explanation Check X name 0 0 ?
-
The first national bank pays a 4% interest rate compound continuously. The effective annual rate paid by the bank is __________. a. 4.16% b. 4.20% c. 4.08% d. 4.12%
-
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...
-
A reversible process is a process (a) Which proceeds with no driving force (b) Which takes place spontaneously (c) Which is quasi-static (d) Which is frictional process.
-
At constant temperature and pressure, the free energy for a chemically reacting system at equilibrium is (a) Minimum (b) Maximum (c) Can not be predicted (d) None of these.
-
The operation of a throttling device follows the (a) Zeroth law of thermodynamics (b) First law of thermodynamics (c) Second law of thermodynamics (d) Third law of thermodynamics.
Study smarter with the SolutionInn App