Starting from:
$35

$29

Project 2 Solution

Purpose: This program will provide experience decomposing a problem, writing methods, and more practice using decision statements and loops.




You need to write a paper-scissors-rock game that supports one human player and one computer player.




To help you understand how the program should work, a sample program run is shown on the next several pages. The basic requirements are as follows:




Ask the user to choose paper, scissors or rock by typing 1, 2, or 3, respectively. If the user enters an invalid number, repeat the request for input. State the item (paper, scissors, or rock) the user has chosen.



Your program should then randomly choose paper, scissors, or rock for the computer player. State the item the computer has chosen.



Determine who won and display the appropriate error message. The rules of paper scissors rock are that paper beats rock, scissors beats paper, and rock beats scissors.



If the computer won, output “You have met your doom!”
If the human won, output “I have been vanquished!”
If both human and computer chose the same object, output “We are equally matched. You are a worthy adversary.”



Keep track of how many times the user has played and their current score. The user should earn one point for a win and lose one point for a loss. A tie does not affect the user’s score. After each round, display the number of times the user has played and their score and ask if they would like to play again. Then react accordingly.



Other requirements:




Use methods appropriately to structure your code. Your main method should consist primarily of method calls within a loop.






Generating random numbers:




Use the following formula to generate random numbers.




(int)(lowerBound + Math.random() * (upperBound – lowerBound + 1)




You may use literals in place of the upper and lower bounds. For instance, to display values between 1 and 10 (inclusive on both ends), the formula would look like this:




(int)(1 + Math.random() * 10)




Reading in a single character using a Scanner object:




When asking the user if they want to play again, you can have them enter y for yes and n for no. To read this in to a character variable, use the following statement, where “answer” is the variable used to store the user’s response to the question and myScanner is the Scanner object:




char answer = myScanner.next().charAt(0);




The grading rubric follows on the last page of this document.

Sample program run:




Choose your weapon!




Paper



Scissors



Rock



5




Choose your weapon!




Paper



Scissors



Rock



1




You chose paper!




I choose rock!




I have been vanquished!




We have matched wits 1 times, and your score is 1 Do you want to play again (y or n)? y




Choose your weapon!




Paper



Scissors



Rock



1




You chose paper!




I choose paper!




We are equally matched. You are a worthy adversary.




We have matched wits 2 times, and your score is 1 Do you want to play again (y or n)? y




Choose your weapon!




Paper



Scissors



Rock



2




You chose scissors!




I choose paper!




I have been vanquished!




We have matched wits 3 times, and your score is 2 Do you want to play again (y or n)? y




Choose your weapon!




Paper



Scissors



Rock



3
You chose rock!




I choose rock!




We are equally matched. You are a worthy adversary.




We have matched wits 4 times, and your score is 2 Do you want to play again (y or n)? y




Choose your weapon!




Paper



Scissors



Rock



3




You chose rock!




I choose rock!




We are equally matched. You are a worthy adversary.




We have matched wits 5 times, and your score is 2 Do you want to play again (y or n)? y




Choose your weapon!




Paper



Scissors



Rock



3




You chose rock!




I choose paper!




You have met your doom!




We have matched wits 6 times, and your score is 1 Do you want to play again (y or n)? n




Grading (100 points total):




(20) The program consists of methods that each do a single task.




Allows a user to pick paper, scissors, or rock. Invalid choices have no effect.



Generates a random choice of item for the computer.



Correctly determines who won the round.



Correctly keeps track of the number of times the user has played.



Correctly keeps track of the user’s score.



Output messages match the format provided in the requirements and shown in the sample program



run.




The user can play repeatedly during a single execution of the program by entering ‘y’ when asked if they want to play again and stop playing by entering ‘n’ when asked if they want to play again.



(10) The code is well commented and formatted in accordance with the style guidelines

More products