Java Program Fix this Rock, Paper and scissor program so I can upload it to Hypergrade and it can pass all the test cases.  Here is the program: import java.util.Random; import java.util.Scanner; public class RockPaperScissors {     public static void main(String[] args) {         if (args.length != 1) {             System.out.println("Please provide a seed as a command line argument.");             return;         }         long seed = Long.parseLong(args[0]);         Random random = new Random(seed);         Scanner scanner = new Scanner(System.in);         System.out.println("Enter 1 for rock, 2 for paper, and 3 for scissors.");         do {             int computerChoice = random.nextInt(3);             int userChoice = getUserChoice(scanner);             if (userChoice == -1) {                 continue;             }             System.out.println("Your choice: " + choiceToString(userChoice) + ". Computer choice: " + choiceToString(computerChoice) + ".");             int result = determineWinner(userChoice, computerChoice);             if (result == 0) {                 System.out.println("It's a draw.");             } else if (result == 1) {                 System.out.println("Computer wins.");             } else {                 System.out.println("You win.");             }             System.out.println("Would you like to play more? (yes/no)");         } while (scanner.next().equalsIgnoreCase("yes"));         System.out.println("Thanks for playing!");         scanner.close();     }     public static int getUserChoice(Scanner scanner) {         int choice;         while (true) {             System.out.print("Enter your choice: ");             if (scanner.hasNextInt()) {                 choice = scanner.nextInt();                 if (choice >= 1 && choice <= 3) {                     break;                 } else {                     System.out.println("Please respond 1, 2, or 3.");                 }             } else {                 scanner.next(); // Consume invalid input                 System.out.println("Please respond 1, 2, or 3.");             }         }         return choice - 1; // Map to 0, 1, 2 internally     }     public static String choiceToString(int choice) {         switch (choice) {             case 0:                 return "rock";             case 1:                 return "paper";             case 2:                 return "scissors";             default:                 return "invalid";         }     }     public static int determineWinner(int userChoice, int computerChoice) {         if (userChoice == computerChoice) {             return 0; // Draw         } else if ((userChoice + 1) % 3 == computerChoice) {             return 1; // Computer wins         } else {             return -1; // User wins         }     } } Test Case 1       Command Line arguments:   123456789   Enter 1 for rock, 2 for paper, and 3 for scissors.\n 1ENTER Your choice: rock. Computer choice: paper.\n Computer wins.\n Would you like to play more?\n nENTER   Test Case 2       Command Line arguments:   123456789   Enter 1 for rock, 2 for paper, and 3 for scissors.\n 2ENTER Your choice: paper. Computer choice: paper.\n It's a draw.\n Would you like to play more?\n nENTER   Test Case 3       Command Line arguments:   123456789   Enter 1 for rock, 2 for paper, and 3 for scissors.\n 3ENTER Your choice: scissors. Computer choice: paper.\n You win.\n Would you like to play more?\n nENTER   Test Case 4       Command Line arguments:   123456789   Enter 1 for rock, 2 for paper, and 3 for scissors.\n 0ENTER Please respond 1, 2, or 3.\n Enter 1 for rock, 2 for paper, and 3 for scissors.\n 1ENTER Your choice: rock. Computer choice: paper.\n Computer wins.\n Would you like to play more?\n nENTER

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Java Program

Fix this Rock, Paper and scissor program so I can upload it to Hypergrade and it can pass all the test cases. 

Here is the program:

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

public class RockPaperScissors {
    public static void main(String[] args) {
        if (args.length != 1) {
            System.out.println("Please provide a seed as a command line argument.");
            return;
        }

        long seed = Long.parseLong(args[0]);
        Random random = new Random(seed);
        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter 1 for rock, 2 for paper, and 3 for scissors.");

        do {
            int computerChoice = random.nextInt(3);
            int userChoice = getUserChoice(scanner);

            if (userChoice == -1) {
                continue;
            }

            System.out.println("Your choice: " + choiceToString(userChoice) + ". Computer choice: " + choiceToString(computerChoice) + ".");

            int result = determineWinner(userChoice, computerChoice);
            if (result == 0) {
                System.out.println("It's a draw.");
            } else if (result == 1) {
                System.out.println("Computer wins.");
            } else {
                System.out.println("You win.");
            }

            System.out.println("Would you like to play more? (yes/no)");
        } while (scanner.next().equalsIgnoreCase("yes"));

        System.out.println("Thanks for playing!");
        scanner.close();
    }

    public static int getUserChoice(Scanner scanner) {
        int choice;
        while (true) {
            System.out.print("Enter your choice: ");
            if (scanner.hasNextInt()) {
                choice = scanner.nextInt();
                if (choice >= 1 && choice <= 3) {
                    break;
                } else {
                    System.out.println("Please respond 1, 2, or 3.");
                }
            } else {
                scanner.next(); // Consume invalid input
                System.out.println("Please respond 1, 2, or 3.");
            }
        }
        return choice - 1; // Map to 0, 1, 2 internally
    }

    public static String choiceToString(int choice) {
        switch (choice) {
            case 0:
                return "rock";
            case 1:
                return "paper";
            case 2:
                return "scissors";
            default:
                return "invalid";
        }
    }

    public static int determineWinner(int userChoice, int computerChoice) {
        if (userChoice == computerChoice) {
            return 0; // Draw
        } else if ((userChoice + 1) % 3 == computerChoice) {
            return 1; // Computer wins
        } else {
            return -1; // User wins
        }
    }
}

Test Case 1

 
 
 
Command Line arguments:
 
123456789
 
Enter 1 for rock, 2 for paper, and 3 for scissors.\n
1ENTER
Your choice: rock. Computer choice: paper.\n
Computer wins.\n
Would you like to play more?\n
nENTER
 

Test Case 2

 
 
 
Command Line arguments:
 
123456789
 
Enter 1 for rock, 2 for paper, and 3 for scissors.\n
2ENTER
Your choice: paper. Computer choice: paper.\n
It's a draw.\n
Would you like to play more?\n
nENTER
 

Test Case 3

 
 
 
Command Line arguments:
 
123456789
 
Enter 1 for rock, 2 for paper, and 3 for scissors.\n
3ENTER
Your choice: scissors. Computer choice: paper.\n
You win.\n
Would you like to play more?\n
nENTER
 

Test Case 4

 
 
 
Command Line arguments:
 
123456789
 
Enter 1 for rock, 2 for paper, and 3 for scissors.\n
0ENTER
Please respond 1, 2, or 3.\n
Enter 1 for rock, 2 for paper, and 3 for scissors.\n
1ENTER
Your choice: rock. Computer choice: paper.\n
Computer wins.\n
Would you like to play more?\n
nENTER

 

Expert Solution
Step 1: Algorithm :

Algorithm: Rock, Paper, Scissors Game

1. Start the program.

2. Check if a seed is provided as a command line argument.
   - If no seed is provided, print "Please provide a seed as a command line argument." and exit the program.

3. Parse the provided seed as a long integer.

4. Initialize a random number generator with the parsed seed.

5. Create a Scanner object to read user input.

6. Print "Enter 1 for rock, 2 for paper, and 3 for scissors."

7. Enter a loop to play the game until the user chooses to quit:
   a. Generate a random choice for the computer (an integer between 1 and 3).
   b. Get the user's choice by calling getUserChoice(scanner).
      - Repeat until a valid choice (1, 2, or 3) is entered.
   c. Print the user's choice and the computer's choice.
   d. Determine the winner by calling determineWinner(userChoice, computerChoice):
      - If userChoice == computerChoice, set result to 0 (draw).
      - If (userChoice + 1) % 3 == computerChoice, set result to 1 (computer wins).
      - Otherwise, set result to 2 (user wins).
   e. Print the result:
      - If result == 0, print "It's a draw."
      - If result == 1, print "Computer wins."
      - If result == 2, print "You win."
   f. Ask the user if they want to play more (yes/no).
   g. If the user enters "no" (case-insensitive), exit the loop.

8. Print "Thanks for playing!" to indicate the end of the game.

9. Close the Scanner object.

10. End the program.

Function getUserChoice(scanner):
1. Initialize choice to -1.
2. Enter a loop:
   a. Print "Enter your choice: ".
   b. Read an integer from the scanner.
   c. If the input is an integer:
      - If the input is between 1 and 3 (inclusive), break the loop.
      - Otherwise, print "Please respond 1, 2, or 3."
   d. If the input is not an integer, consume the invalid input and print "Please respond 1, 2, or 3."
3. Return the user's choice.

Function determineWinner(userChoice, computerChoice):
1. If userChoice equals computerChoice, return 0 (draw).
2. If (userChoice + 1) % 3 equals computerChoice, return 1 (computer wins).
3. Otherwise, return 2 (user wins).


steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Knowledge Booster
Arrays
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education