Question

In JAVA Chapter 5Assignment(Rock, Paper, Scissors)–20pointsYour goal is towrite a program that lets the user play...

In JAVA

Chapter 5Assignment(Rock, Paper, Scissors)–20pointsYour goal is towrite a program that lets the user play the game of Rock, Paper, Scissors against the computer.Your program should have the following: •Make the name of the project RockPaperScissors•Write a method that generates a random number in the range of 1 through 3. The randomly generated number will determine if the computer chooses rock, paper, or scissors. 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.Return the computer’s choice.(5points)•Write another method where the user enters his or her choice of “rock”, “paper”, or “scissors”. You can usecommand line or message box. You can choose to have the user type the wordor use a menu choice. If the user does not enter a valid choose,prompt them for the choice again(You can do this inside of this method or create another method to do the validation).Return the user’s choice.(6points)•Write another method to determine the winnerand display the results. Write conditionals to determine the winner. •Ifone player chooses rock andthe other player choose scissors, then rock wins. (Rock smashes scissors).•If one player chooses scissors and the other player chooses paper, then scissors wins. (Scissors cut paper).•If one player chooses paper and the other player chooses rock, then paper wins. (Paper wraps rock.)•If both players make the same choice, the game must be played again to determine the winner.Display the computer’s choice, the user’s choice, and whether or not the computer or the user won/tied. (5points)•In the main method, call the method that generates the computer’s choice and save this choice in a variable. Call the method to get the user’s choice and save in a variable. Callthemethod to determine the winnerand displayresults method(You will need to usea loop to repeat this method if the computer and the user choices were tied).(4points)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

package rockpaperscissors;

import java.util.Random;
import java.util.Scanner;

public class RockPaperScissors {
public static void main(String[] args) {
//c_choice - stores computer's choice
//u_choice - stores user's choice
int c_choice, u_choice;
//result - stores result (-1 computer wins), (1 user wins), (0 ties)
int result = 0;
//while result is 0(tie), loop runs
while(result == 0){
//getComputerChoice() - returns (1 or 2 or 3) and assigned to c_choice
c_choice = getComputerChoice();
//getUserChoice() - returns (1 or 2 or 3) and assigned to u_choice
u_choice = getUserChoice();
//checkResult() - prints result and return (-1 or 0 or 1)
result = checkResult(c_choice,u_choice);
}
  
}
  
//getComputerChoice() - returns 1 or 2 or 3 (for rock, paper, scissors respectively)
public static int getComputerChoice(){
return new Random().nextInt(3) + 1;
}
  
public static int getUserChoice(){
Scanner scan = new Scanner(System.in);
// if flag = 0 -> input is invalid
// if flag = 1 -> valid input and it is returned
int flag = 0, choice = 0;
// while input is invalid, loop runs
while(flag == 0){
System.out.print("Choose\n1.Rock\n2.Paper\n3.Scissors\nEnter your choice :");
choice = scan.nextInt();
if(choice > 0 & choice < 4)
// flag is set to 1, if input is valid
flag = 1;
else
System.out.println("Invalid Input!");
}
return choice;
}
  
// checkResult() - calculate result based on conditions
// based on result, prints the result
// returns result
public static int checkResult(int c_choice, int u_choice){
int result = 0;
// (-1 computer wins), (1 user wins), (0 ties)
//computer == user - ties
if(c_choice == u_choice)
result = 0;
//computer - rock, user - scissors
else if(c_choice == 1 && u_choice == 3)
result = -1;
//computer - rock, user - paper
else if(c_choice == 1 && u_choice == 2)
result = 1;
//computer - paper, user - rock
else if(c_choice == 2 && u_choice == 1)
result = -1;
//computer - paper, user - scissors
else if(c_choice == 2 && u_choice == 3)
result = 1;
//computer - scissors, user - rock
else if(c_choice == 3 && u_choice == 1)
result = 1;
//computer - scissors, user - paper
else if(c_choice == 3 && u_choice == 2)
result = -1;
  
//prints corresponding object name - getObject()
System.out.println("Computer : "+getObject(c_choice));
System.out.println("User : "+getObject(u_choice));
if(result == -1)
System.out.println("Computer Wins!");
else if(result == 1)
System.out.println("User Wins!");
else
System.out.println("Tied!");
return result;   
}
  
//Helper function//
// getObject() - reads a int and String based on int value
// if int value is 1 returns "Rock"
// if 2 returns "Paper"
// if 3 returns "Scissors"
public static String getObject(int choice){
String object = null;
switch(choice){
case 1:
object = "Rock";
break;
case 2:
object = "Paper";
break;
case 3:
object = "Scissors";
}
return object;
}
}

//Screenshot of Code:

//refer this to know the proper indentations.

package rockpaperscissors; import java.util.Random; import java.util.Scanner; public class RockPaperScissors { public staticelse System.out.println(Invalid Input!); return choice; } // checkResult -calculate result based on conditions // based onSystem.out.println(Computer : +getUbject (c_choice)); System.out.println(User : +getObject (u_choice)); if(result == -1)//Sample I/O:

run: Choose 1. Rock 2.Paper 3. Scissors Enter your choice :1 Computer : Rock User : Rock Tied! Choose 1. Rock 2. Paper 3. Sci

run: Choose 1. Rock 2. Paper 3. Scissors Enter your choice :3 Computer : Scissors User : Scissors Tied! Choose 1. Rock 2. Pap

run: Choose 1. Rock 2. Paper 3. Scissors Enter your choice : 2 Computer : Rock User : Paper User Wins! BUILD SUCCESSFUL (tota

//Hope this helps.

//Please give thumbs up. Thank you!

//Comment for doubts.

Add a comment
Know the answer?
Add Answer to:
In JAVA Chapter 5Assignment(Rock, Paper, Scissors)–20pointsYour goal is towrite a program that lets the user play...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 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...

  • C++ Part 2: Rock Paper Scissors Game Write a program that lets the user play this...

    C++ Part 2: Rock Paper Scissors Game Write a program that lets the user play this game against the computer. The program should work as follows: When the program begins, a random number between 1 and 3 is generated. If the number is 1, the computer has chosen rock. If the number is 2, the computer has chosen paper. If the number is 3, the computer has chosen scissors. Don't display the computer's choice yet. Use a menu to display...

  • java pls Rock Paper Scissors Lizard Spock Rock Paper Scissors Lizard Spock is a variation of...

    java pls Rock Paper Scissors Lizard Spock Rock Paper Scissors Lizard Spock is a variation of the common game Rock Paper Scissors that is often used to pass time (or sometimes to make decisions.) The rules of the game are outlined below: • • Scissors cuts Paper Paper covers Rock Rock crushes Lizard Lizard poisons Spock Spock smashes Scissors Scissors decapitates Lizard Lizard eats Paper Paper disproves Spock Spock vaporizes Rock Rock crushes Scissors Write a program that simulates the...

  • C++ You are asked to implement scissors-rock-paper game where the players are a computer and a...

    C++ You are asked to implement scissors-rock-paper game where the players are a computer and a user. The player that wins 3 hands successively is the winner of the game. Your program should prompt the user with a message at the beginning of each hand to enter one of scissors, rock or paper. If the user’s entry is not valid, i.e. entry is neither scissors, rock, nor paper, your program throws a message to the user to enter a valid...

  • In python language Write a program that lets the user play the game of Rock, Paper,...

    In python language Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows: 1. When the program begins, a random number in the range of 1 and 3 is generated. 1 = Computer has chosen Rock 2 = Computer has chosen Paper 3 = Computer has chosen Scissors (Dont display the computer's choice yet) 2. The user enters his or her choice of “Rock”, “Paper", “Scissors" at...

  • Write a Python program (using python 3.7.2) that lets the user play the game of Rock,...

    Write a Python program (using python 3.7.2) that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. 1. 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...

  • For this week’s assignment, we will be recreating the Rock, Paper, Scissors program using Object-...

    For this week’s assignment, we will be recreating the Rock, Paper, Scissors program using Object-Oriented Programming. You will be working with me on a team to build the program. I have already written my part of the program, and the Game.java file is attached. Your task will be to write a RockPaperScissors class that contains the following methods: getUserChoice: Has the user choose Rock, Paper, or Scissors. After validating the input, the method returns a String containing the user choice....

  • 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....

  • Using python 3.7.3 Challenge: Rock, Paper, Scissors GamePDF Description: Create a menu-driven rock, paper, scissors game...

    Using python 3.7.3 Challenge: Rock, Paper, Scissors GamePDF Description: Create a menu-driven rock, paper, scissors game in Python 3 that a user plays against the computer with the ability to save and load a game and its associated play statistics. Purpose: The purpose of this challenge is to assess the developer’s ability to create an interactive application with data persistence in Python 3. Requirements: Create a Rock, Paper, Scissors game in Python named rps.py according to the requirements specified in...

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