CSU Banner

CS 150, Spring 2018

Programming Assignment - P3

Conditionals - If and Switch

Due - February 27th, 2018 at 6:00pm

Late - February 28th, 2018 at 8:00am


Objectives of this Assignment

  1. To write a Java program that determines output based on conditionals,
  2. to practice reading input from the user via the console,
  3. to gain exposure to the Random class, and
  4. 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."

Game visual

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
  1. Create a Java project in Eclipse called P3.
  2. Create a new class in the P3 project and let Eclipse create a main method in P3.java.
  3. Copy the comment block below and personalize it if you have not set up a default comment header.
  4. All variables and code should reside in the main method!
  5. Declare two variables of type int, playerChoice to represent the player's choice and computerChoice to represent the computer's choice.
  6. Declare a variable of type int, seedVariable to represent the seed to the random number generator
  7. Initialize these three variables to zero.
  8. Create a Scanner object to read console input (i.e. input from the keyboard).
  9. Prompt the user for the seed, by printing:
    "Please enter a seed: "
  10. Read the seed to be provided to the random number generator, which means assign the integer entered by the user to the seed variable.
  11. 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."
  12. Following the options, print a blank line and then "Your choice: " on a new line.
  13. Read the number entered by the user into the player variable using the Scanner method nextInt().
  14. 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;
  15. In order to use Java's Random class you will also need to import the Random library at the top of your project. The syntax for this is the same as when we import the Scanner library except replace the word Scanner with Random.
  16. Print the value of the computer's choice variable to the console after the prompt "Computer's choice: ".
  17. Print a blank line after printing the computer's choice.

  18. Part Two
  19. Now, we need to use conditionals to decide which player wins.
  20. 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.
  21. Write a switch statement that switches on the player variable.
  22. Note: Remember your cases within the switch statement need to match the variable type of the player's choice variable.
  23. 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."
  24. For each case within the switch statement, you will need to write five if statements.
  25. 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."
  26. 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.
  27. 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.
  28. Be sure to include a new line when printing any of the program options below.

Program Structure

// Assignment: P3  
// Author: Jess Cobb
// Date: 12 February 2018
// Class: CS150
// Email: jesscobb@rams.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

Grading Criteria


Submit P3.java to Checkin.


© 2018 CS150 Colorado State University. All Rights Reserved.
CS Banner
CS Building