Question: Task 2: create a blueprint class for a player Write a class named Player that serves as a blueprint for objects that represent a single
Task 2: create a blueprint class for a player
Write a class named Player that serves as a blueprint for objects that represent a single Blackjack player. Make sure to save it in your ps4 folder.
Import the java.util package at the start of the file.
Each Player object should have the following components:
1.three fields:
a)one called name to keep track of the players name (a single string)
b)one called hand for an array to hold the cards in the players hand
c)one called numCards to keep track of how many cards are currently in the players hand Make sure that your field definitions prevent direct access by code from outside the class.
2.a constructor that takes a single parameter for the name of the player. It should initialize all of the fields. Among other things, it should create the array that will store the cards. Make the collection big enough to store the maximum number of cards in a given hand (11). Use the class constant Blackjack.MAX_CARDS_PER_PLAYER to specify this value, rather than hard-coding the integer 11.
3.an accessor named getName that returns the players name.
4.an accessor named getNumCards that returns the current number of cards in the players hand.
5.a toString method that just returns the players name.
6.a mutator named addCard that takes a Card object as a parameter and adds the specified card to the players hand, filling the array from left to right. It should throw an IllegalArgumentException if the parameter is null, or if the player already has the maximum number of cards.
7.an accessor named getCard that takes an integer index as a parameter and returns the Card at the specified position in the players hand, without actually removing the card from the hand. For example, if p is a Player, p.getCard(0) should return the card at position 0 in ps hand i.e., the first/leftmost card. If the specified index does not correspond to one of the cards in the hand, the method should throw an IllegalArgumentException.
8.an accessor method named getHandValue that computes and returns the total value of the players current hand i.e., the sum of the values of the individual cards. Use the getValue method from the Card class to get each cards value.
One tricky aspect of this method is handling any Aces that may be present in the hand. The getValue method will return 1 for an Ace, but you may need to change its value to 11 depending on the values of the rest of the cards in the hand. Hint: If there is more than one Ace in the hand, at most one of them can have a value of 11, and you should only give it a value of 11 if doing so wont cause the hands total value to exceed 21.
9.an accessor method named printHand that prints the current contents of the players hand, followed by the value of the players hand. For example:
4H 7C QD (value = 21)
Notes:
The spacing matters. There should be two spaces between the strings for the individual cards, and two spaces between the last cards string and the left parenthesis at the start of the value.
This method should not print the player identifiers (dealer: or you: ) that come before the hands in the sample runs.
10.an accessor method named hasBlackjack that returns true if the player has Blackjack (a two-card hand with a value of 21), and false otherwise. This method will be relatively simple to write if you take advantage of one or more of the other methods that youve already written.
11.an accessor method called wantsHit that should return true if the player wants another hit, and false if the player does not want another hit. The method should take two parameters: a Scanner object that can be used to read from the console, and a Playerobject representing the players opponent (in that order). In this version of the method:
You should print the appropriate prompt asking if the user wants a hit (see the sample runs), and read a string from the console using the nextLine method of the Scanner passed in as a parameter. (Make sure that you do not use the Scanners next() method.) The method should return true if the user enters y or Y, and it should return false if the user enters any other input (even if its something other than n or N).
You should ignore the second parameter (the Player object for the opponent). The version of this method that you will write in Task 3 will use this second parameter, which is why we include it here as well.
12.a mutator method called discardCards that should get rid of all of the cards in the players hand, to prepare for a new round of the game. There are different ways to accomplish this. The key thing is to ensure that immediately after this method is called, all other methods that depend on the number of cards in the players hand should behave as if the player has no cards.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
