Java Bootcamp – Workbooks and Challenges
)}

Rock paper scissors part 2

In part 2, your application will determine a winner.

Task 4: the game result


1. Function name:

/** * Function name: result <------- * @param yourChoice (String) * @param computerChoice (String) * @return result (String). */

2. Parameters:

/** * Function name: result - * @param yourChoice (String) <------- * @param computerChoice (String) <------- * @return result (String). */

3. Return value:

/** * Function name: result * @param yourChoice (String) * @param computerChoice (String) * @return result (String). <-------- */

Inside the function, set up series of if - else if statements.

/** * Function name: result * @param yourChoice (String) * @param computerChoice (String) * @return result (String) * * Inside the function: <---------- * 1. result is "You win!" if: * * You: "rock" Computer: "scissors" * You: "paper" Computer: "rock" * You: "scissors" Computer: "paper" * * 2. result is "You lose!" if: * * Computer: "rock" You: "scissors" * Computer: "paper" You: "rock" * Computer: "scissors" You: "paper" * * 3. result is "It's a tie" if: * * your choice equals computer choice. * */

There are three scenarios to win, three to lose, and one to tie. It's your task to manifest these scenarios into code.

Call the function and print the result.

- if the answer is yes:
  -- print: Great!
  -- print: rock -- paper -- scissors, shoot!
  -- pick up user's choice.
  -- get the computer choice.
  -- System.out.println("\nYou chose:        " + yourChoice);
  -- System.out.println("The computer chose: " + computerChoice);

  -- get the result              <------------
  -- System.out.println(result); <------------

  -- print everything        (can only be done after task 5).
- else:
  -- print: Darn, some other time...!

Before you move to task 5, this is a good place to test your code. The end result should print your choice, the computer's choice, and the result.

Run it a few more times. It should keep returning the correct result.

Task 5: print everything at once


Remove all your print statements from main(). Then, create the following function:

1. Function name:

/** * Name: printResult <-------- * @param yourChoice (String) * @param computerChoice (String) * @param result (String) * * Inside the function: * 1. prints everything: * -- prints: You chose: <your choice> * -- prints: The computer chose: <computer choice> * -- prints: <result> */

2. Parameters:

/** * Name: printResult * @param yourChoice (String) <------ * @param computerChoice (String) <------ * @param result (String) <------- * * Inside the function: * 1. prints everything: * -- prints: You chose: <your choice> * -- prints: The computer chose: <computer choice> * -- prints: <result> */

3. Return value: void.

4. Inside the function:

/** * Name: printResult * @param yourChoice (String) * @param computerChoice (String) * @param result (String) * * Inside the function: <------ * 1. prints everything: * -- prints: You chose: <your choice> * -- prints: The computer chose: <computer choice> * -- prints: <result> */

Back in the if statement, call printResult() and you're done!

- if the answer is yes:
   -- print: Great!
   -- print: rock -- paper -- scissors, shoot!
   -- pick up user's choice.
   -- get the computer choice.
   -- get the result.
   -- print everything <------
- else:
   -- print: Darn, some other time...!

Run your code


If it feels like you're playing a real game of rock-paper-scissors, then you're finished!

Good luck!


Feedback Summary
4.8
38 students
5

92%
4

3%
3

0%
2

0%
1

5%
Written Reviews
There are no written reviews yet.