Question: Create a new Java file called RPSLSYourLastName . Copy your Rock Paper Scissor Lizard Spock game from last unit into it. Modify the method
Create a new Java file called "RPSLSYourLastName". Copy your Rock Paper Scissor Lizard Spock game from last unit into it. Modify the method to use a switch statement instead of nested if statements. If you haven't, implement Lizard & Spock. Everything else should still work for your game. Again, prompt the both player 1 and player 2 for a String (as their selection). Pass that value into your method. Set a default value for the winner (either player 1 or player 2) and switch the value if the other person wins or there is a tie. This image might help you figure out who win.
Report back whether player 1, player 2 or it's a tie. Again, do not print from your decision structure.
Add testing to this program. If you ONLY change the .java file name and the method implementation, you should be able to just modify your testing file from the last unit quickly and everything will still work [you may have to add in testing for Lizard & Spock if you didn't already have it]. When you are finished upload, the RPSLSYourLastName.java & your testing file to Blackboard. This is worth 15 points.
code that is needing modification:
import java.util.Scanner; public class RockPaperScissorsYourLastName { public static int findWinner(String s1,String s2) { int winner = 0; if(s1.equals("rock")) { // if player 1 chooses rock if(s2.equals("rock")) winner = 0; else if(s2.equals("scissors")) winner = 1; else winner = 2; } else if(s1.equals("scissors")) { // if player 1 chooses scissors if(s2.equals("scissors")) winner = 0; else if(s2.equals("paper")) winner = 1; else winner = 2; } else { // if player 1 chooses paper if(s2.equals("paper")) winner = 0; else if(s2.equals("rock")) winner = 1; else winner = 2; } return winner; } public static void main(String[] args){ Scanner pick = new Scanner(System.in); String s1 = ""; //s1 is player 1's choice string String s2 = ""; //s2 is player 2's choice string System.out.println("Player 1: Choose rock, scissors, or paper:"); s1 = pick.next(); s1 = s1.toLowerCase(); System.out.println("Player 2: Choose rock, scissors, or paper:"); s2 = pick.next(); s2 = s2.toLowerCase(); int winner = findWinner(s1,s2); if(winner == 0) { System.out.println("It's a draw! You both chose the same object" + " which is: " + s1 +"."); } else { System.out.println("Player " + winner + " Wins."); } } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
