Question

Java Constructor and method

Player Class Specification The Player class represents a player that will play a simple game. In this game, a player can play the number 0, 1, or 2. The Player class contains the following four fields: private String name; private int play; private int [] tally; private StringBuffer history; The Player class contains the following 2 constructors and 5 methods that you must implement: 1. Constructor with String parameter – If the argument is null, throw the IllegalArgumentException with the message "Invalid parameter", otherwise set the name field. Set play to 0, assign an int array to tally (you can decide the size of the array), and an empty StringBuffer to history. This constructor will be referred to as “the constructor” in the remainder of the text. 2. Copy Constructor – Make sure the copy is completely independent of the original with no shared fields. 3. getPlay – get method for the play field. 4. setPlay - If the argument is anything other than 0, 1, or 2 , throw the IllegalArgumentException with the message "Invalid parameter", otherwise set the play field. You are free to update other fields as you see fit in this method. 5. getHistory – returns the history of the player. The history will be each play followed by one space after the play(there will even be a space after the last play). For example, if the player plays a 2, followed by a 0, followed by a 1, the content of the history will be 2 0 1 Note: The history never contains the original 0 assigned as the default value of play using the constructor. Hint: Beware of a privacy leak. (Please do not post to piazza asking what is meant by a privacy leak. You should know this from the lectures) 6. reset – After this method is invoked on a player object, the player object should be in the same state as it would be if it was just constructed using the constructor. 7. getTally – Will return a string that represents the frequency of each play of the player. For example, the following: 0: ******** 1: ****** 2: ****** means the player has played 8 zeros (because there are 8 *), 6 ones, and 6 twos. See the sample driver for another example. PlayGame Class Specification The PlayGame class contains one static method, play, that allows 2 players to play the simple game. The rules are as follow: There are only 3 values that can be played: 0, 1, 2 In each round, each of the 2 player picks one of the three values. If both players, pick the same value, the round is a tie. A 2 beats a 1, a 1 beats a 0, and a 0 beats a 2 (this last rule is a little counter intuitive, so just to repeat, a player that picks a 0 will win over the player who picks a 2) The PlayGame class contains the following static method that you must implement: 1. play -This method will simulate 10 rounds of the game and return a string that shows the result of each round as well as the overall winner of the game (or report a tie). For each round, first set the play of player 1 then set the play of player 2. To get a value that is either 0,1,or 2, you must use the following call: random.nextInt(3) For each round, figure out who the winner is (it might be a tie) and concatenate the string you form to the string that the method will return. For example, the text for round 1 might be: Round 1 - P1: 0, P2: 1, result: P2 Wins See the Sample Driver output for more examples. Feel free to add private helper static methods to help you implement the necessary logic of who wins a round. After the 10 rounds, you need to have one additional line that reports the winner (whoever won the most game, or a tie if both have the same number of wins). For example, the text might be: P1 total: 2, P2 total: 3, Winner: P2 See the Sample Driver output for more examples.

0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
Java Constructor and method
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • Java CSC252   Programming II    Static Methods, Enums, Constants, Random Rock Paper Scissors Write a program that...

    Java CSC252   Programming II    Static Methods, Enums, Constants, Random Rock Paper Scissors Write a program that allows the user to play "Rock, Paper, Scissors". Write the RPS class. All code should be in one file. main() will be part of the RPS class, fairly small, and contain only a loop that asks the user if they want to play "Rock, Paper, Scissors". If they say yes, it calls the static method play() of the RPS class. If not, the program...

  • In this problem, you’ll play a simple rock, paper, scissors game. First, you’ll ask the user...

    In this problem, you’ll play a simple rock, paper, scissors game. First, you’ll ask the user to pick rock, paper, or scissors. Then, you’ll have the computer randomly choose one of the options. After that, print out the winner! You should keep playing the game until the user hits enter. Note: You’ll need to implement a method called String getWinner(String user, String computer). Luckily, you just wrote that in an earlier program! Here is a sample run of the program....

  • Rock, Paper, Scissors (also known by several other names, see http://en.wikipedia.org/wiki/Rock p...

    Help needed in Perl language program! Note : Perl Language Rock, Paper, Scissors (also known by several other names, see http://en.wikipedia.org/wiki/Rock paper scissors) is an extremely popular hand game most often played by children. Often, it is used as a method of selection similar to flipping a coin or throwing dice to randomly select a person for some purpose. Of course, this game is not truly random since a skilled player can often recognize and exploit the non-random behavior of...

  • JAVA Only Help on the sections that say Student provide code. The student Provide code has...

    JAVA Only Help on the sections that say Student provide code. The student Provide code has comments that i put to state what i need help with. import java.util.Scanner; public class TicTacToe {     private final int BOARDSIZE = 3; // size of the board     private enum Status { WIN, DRAW, CONTINUE }; // game states     private char[][] board; // board representation     private boolean firstPlayer; // whether it's player 1's move     private boolean gameOver; // whether...

  • Write a Python program (rock_paper_scissors.py) that allows two players play the game rock paper scissors. Remember...

    Write a Python program (rock_paper_scissors.py) that allows two players play the game rock paper scissors. Remember the rules: 1. Rock beats scissors 2. Scissors beats paper 3. Paper beats rock The program should ask the users for their names, then ask them for their picks (rock, paper or scissors). After that, the program should print the winner's name. Note, the players may pick the same thing, in this case the program should say it's tie. when the user input an...

  • ITEMS SUMMARY 16, < Previous Nex The following program obtains three integers, x, y, and z,...

    ITEMS SUMMARY 16, < Previous Nex The following program obtains three integers, x, y, and z, as input form the user. Complete the program so that it determines whether there is a subset of two or three of these numbers that sum to zero. If so, the program should print zero sum found. Otherwise, it should print no zero sum found. Check syntax X = int(input) 2 y int(input) int(input) In the word game Boggle, a word is scored according...

  • Use Java language to create this program Write a program that allows two players to play...

    Use Java language to create this program Write a program that allows two players to play a game of tic-tac-toe. Using a two-dimensional array with three rows and three columns as the game board. Each element of the array should be initialized with a number from 1 - 9 (like below): 1 2 3 4 5 6 7 8 9 The program should run a loop that Displays the contents of the board array allows player 1 to select the...

  • IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors...

    IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. Don’t display the computer’s choice yet....

  • (Java) Write a program that lets the user play the game of Rock, Paper, Scissors against...

    (Java) Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. Don’t display the computer’s choice yet. The...

  • In java language here is my code so far! i only need help with the extra...

    In java language here is my code so far! i only need help with the extra credit part For this project, you will create a Rock, Paper, Scissors game. Write a GUI program that allows a user to play Rock, Paper, Scissors against the computer. If you’re not familiar with Rock, Paper, Scissors, check out the Wikipedia page: http://en.wikipedia.org/wiki/Rock-paper-scissors This is how the program works: The user clicks a button to make their move (rock, paper, or scissors). The program...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT