CS 150, Fall 2017
Programming Assignment - P3
Conditionals - If and Switch
Due - September 26th, 2017 at 6:00pm
Late - September 27th, 2017 at 8:00am
Objectives of this Assignment
- To write a Java program that determines output based on conditionals,
- to practice reading input from the user via the console,
- to gain exposure to the Random class, and
- to practice writing if statements and a switch block.
Description
This assignment is inspired by the TV show The Big Bang Theory and you will be
implementing Rock, Paper, Scissors, Lizard, Spock as a Java program. As Sheldon explains on the show,
"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,
and as it always has, rock crushes scissors."
Instructions
Create a Java program called P3 with a class named P3, which after created
should contain a file named P3.java in the src folder. All of the Java code you write
should be contained in the main method. An example of how you might structure your main
method is given below. For this assignment, you must follow the directions below
exactly:
Part One
- Create a Java project in Eclipse called P3.
- Create a new class in the P3 project and let Eclipse create a main method in P3.java.
- Copy the comment block below and personalize it if you have not set up a default comment header.
All variables and code should reside in the main method!
- Declare two variables of type int, playerChoice to represent the player's choice and computerChoice to represent the computer's choice.
- Declare a variable of type int, seedVariable to represent the seed to the random number generator
- Initialize these three variables to zero.
- Create a Scanner object to read console input (i.e. input from the keyboard).
- Prompt the user for the seed, by printing:
"Please enter a seed: "
- Read the seed to be provided to the random number generator, which means assign the integer entered by the user to the seed variable.
- Print a blank line then prompt the user for their object choice, by printing:
"Which game object do you choose? "
"For rock, enter 1."
"For paper, enter 2."
"For scissors, enter 3"
"For lizard, enter 4."
"For Spock, enter 5."
- Following the options, print a blank line and then "Your choice: " on a new line.
- Read the number entered by the user into the player variable using the Scanner method nextInt().
- Next, the program will create a random number between 1 and 5. Please use the following to do so:
Random r = new Random();
r.setSeed(seedVariable);
computerChoice = r.nextInt(4) + 1;
- Print the value of the computer's choice variable to the console after the prompt "Computer's choice: ".
- Print a blank line after printing the computer's choice.
Part Two
- Now, we need to use conditionals to decide which player wins.
- We will do so by utilizing a switch block with if statements inside each case. This is by no means the only way to
determine which player wins, but by doing so, you will be able to practice writing a switch statement.
- Write a switch statement that switches on the player variable.
- Note: Remember your cases within the switch statement need to match the variable type of the player's choice variable.
- You should write your switch statement so that there are five cases (one for each choice in the game) and a default, which if reached, should print "1, 2, 3, 4, or 5 was not entered. Try playing again."
- For each case within the switch statement, you will need to write five if statements.
- One of these if statements deals with the situation where the computer generates the same integer that the player enters. If this occurs, have your program print "Computer generated same choice. Draw game."
- Two of the five if statements need to handle the situations where the player beats the computer. If this is the case, print out the appropriate statement from the options below that relates to each situation.
- The other two if statements will handle the situations where the computer beats the player. If this is the case, print out the appropriate statement from the options below that relates to each situation.
- Be sure the last line has an end of line.
- Here are your console output options:
"Computer generated same choice. Draw game."
"Rock crushes scissors. Player wins."
"Rock crushes scissors. Computer wins."
"Rock crushes lizard. Player wins."
"Rock crushes lizard. Computer wins."
"Paper covers rock. Player wins."
"Paper covers rock. Computer wins."
"Paper disproves Spock. Player wins."
"Paper disproves Spock. Computer wins."
"Scissors cuts paper. Player wins."
"Scissors cuts paper. Computer wins."
"Scissors decapitates lizard. Player wins."
"Scissors decapitates lizard. Computer wins."
"Lizard eats paper. Player wins."
"Lizard eats paper. Computer wins."
"Lizard poisons Spock. Player wins."
"Lizard poisons Spock. Computer wins."
"Spock vaporizes rock. Player wins."
"Spock vaporizes rock. Computer wins."
"Spock smashes scissors. Player wins."
"Spock smashes scissors. Computer wins."
- It is recommended that you first write down the structure of your switch statement on paper to more easily keep track of using each of the above options only once, except of course, for the situation of a draw game.
- Determining the output of this assignment may seem tedious and boring, but this assignment is also an exercise in thinking. We want you to create a system that works for you, where you will be able to ensure that each case in your switch statement handles all appropriate situations in the game. For some, working off of the visual above may be helpful. For example, you might notice that each object in the above visual has two arrows pointing away from it and two arrows pointing toward it.
- See the sample output section for four sample runs and associated output.
Program Structure
// P3 Assignment
// Author: Russ Wakefield
// Date: 9/19/2017
// Class: CS150
// Email: waker@colostate.edu
import java.util.Scanner;
public class P3 {
public static void main(String[] args) {
// Declare variables.
// Prompt user for a seed and copy and paste code from step 13.
// Game instructions, prompt user and assign player's choice variable.
// Print computer's choice.
// Utilize a switch block with if statements to determine winner.
} // end main
} // end class P3
Sample Output
Your program should print 13 lines, which includes 3 blank lines.
Please enter a seed: 10
Which game object do you choose?
For rock, enter 1.
For paper, enter 2.
For scissors, enter 3.
For lizard, enter 4.
For Spock, enter 5.
Your choice: 2
Computer's choice: 3
Scissors cut paper. Computer wins.
---------------------------------
Please enter a seed: 10
Which game object do you choose?
For rock, enter 1.
For paper, enter 2.
For scissors, enter 3.
For lizard, enter 4.
For Spock, enter 5.
Your choice: 3
Computer's choice: 3
Computer generated same choice. Draw game.
---------------------------------
Please enter a seed: 10
Which game object do you choose?
For rock, enter 1.
For paper, enter 2.
For scissors, enter 3.
For lizard, enter 4.
For Spock, enter 5.
Your choice: 5
Computer's choice: 3
Spock smashes scissors. Player wins.
---------------------------------
Please enter a seed: 10
Which game object do you choose?
For rock, enter 1.
For paper, enter 2.
For scissors, enter 3.
For lizard, enter 4.
For Spock, enter 5.
Your choice: 1
Computer's choice: 3
Rock crushes scissors. Player wins.
Specifications
- Work on your own, as always.
- The name of the source code file must be exactly
P3.java
- Name the file exactly - upper and lower case matters!
- Comments at the top as shown above.
- Assignments should be implemented using Eclipse.
- Assignments should be implemented using Java, version 1.8.
- Make sure your code runs on machines in the CSB 120 lab.
- Turn in through the Checkin tab.
- Read the syllabus for the late policy.
- We will be checking programs for plagiarism, so please don't copy from anyone else.
Grading Criteria
- 100 points for perfect submission.
- 0 points for no submission, will not compile, submitted class file, etc.
- Preliminary Tests
Preliminary tests will use a seed value of 10.
- testCompile: checks that program compiles. (0 points)
- test10: checks the menu for correctness (10 points)
- test11 - test15: checks the response for a player choice of 1-5 (10 points each)
- Final Tests
Final tests use a different seed value.
- Final grading includes the preliminary tests.
Submit P3.java to Checkin.