Question
Write Java program using the code I will attach below as a start file that has the game running as a single method in main(not
Write Java program using the code I will attach below as a start file that has the game running as a single method in main(not good!). You need to take that as a start file and convert it into method (that is good!). Change the name to RPSWithMethods. Here is my UML.
Here is my main method: ============ publicstaticvoid main(String[] args) { // TODO Auto-generated method stub String playerChoice = userChoice(); String compChoice = computerChoice(); determineWinner(playerChoice,compChoice); }//End main ============
Explanation of methods: RPSWithMethods a no-arg constructor. Every class has to have one or it will not run! But if you do not write any constructors, Java writes this for you so you do not need to write anything main() a method that runs (you know this!) computerChoice() generate a number between 1 and 3 and return a String value (1=rock 2=paper 3=scissors) userChoice() The method has a boolean variable set to false. While this is false, the program asks the user to enter either rock, paper, or scissors. Pass their answer into the isValidChoice() method which returns true if it is one of the valid choices and false it not. The loop continues until they enter a valid word. determineWinner() asses in the two strings from the above methods and prints out the winner =======================
public class RPSGame {
public static void main(String[] args) { Scanner scan = new Scanner(System.in);
// set counters int uWins = 0; int cWins = 0; String user = ""; // begin loop for (int i = 0; i
while (! user.equals("rock") && ! user.equals("scissors") && ! user.equals("paper")) { // get user choice System.out .println("Please enter rock, paper, or scissors using only lower case letters"); user = scan.nextLine(); }
// find the winner System.out.println("The computer's choice was " + comp); System.out.println("The user's choice was " + user); if (user.equals("rock") && comp.equals("paper")) { cWins++; System.out.println("Paper covers rock. " + "The computer wins!"); } else if (user.equals("paper") && comp.equals("scissors")) { cWins++; System.out.println("Scissors cuts paper. " + "The computer wins!"); } else if (user.equals("scissors") && comp.equals("rock")) { cWins++; System.out.println("Rock crushes scissors. " + "The computer wins!"); } else if (user.equals("rock") && comp.equals("scissors")) { uWins++; System.out.println("Rock crushes scissors. " + "The user wins!"); } else if (user.equals("scissors") && comp.equals("paper")) { uWins++; System.out.println("Scissors cuts paper. " + "The user wins!"); } else if (user.equals("paper") && comp.equals("rock")) { uWins++; System.out.println("Paper covers rock. " + "The user wins!"); } else System.out.println("It is a tie. No one wins!!"); user = ""; } // end of for loop System.out.println("The user won " + uWins + " times and the computer won " + cWins + " times"); } // end main
}
=======================
> RPSWithMethods itp 120mod4q1_ANS RPSWithMethods() main(String[]):void computerChoice():String userChoice():String isValidChoice(String):boolean determine Winner(String, String):void THE - CODE - TO - START - WITH
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started