Java program assignment through 3 files
COMP 1130
Computer Programming I
Due Date: Thursday, March 14th 2019, by 11:59 PM
Assignment 2: Logic, Loops, and Classes
Requirements – Summary:
Produce one (1) program that allows to the User to play the Rock, Paper, Scissors Game against the computer via three (3) java class
files.
1. When the program starts it must ask the User for his/her Name, give its own name, and welcome the person to the game.
2. It will then present instructions to the user on how to play the game.
a. Pressing R will play Rock
b. Pressing P will play Paper
c. Pressing S will play Scissors
d. Pressing 0 will end the game
3. The program will then start the game and ask the user for his/her first pick of R, P, or S (and of course 0).
Example:
Output: “What is your name?:”
Input: “Eric”
Output: “Hi Eric, it’s very nice to meet you.”
Output: “My name is Bob, your computer opponent.”
Output: “Let’s play Rock, Paper, Scissors!”
Output: “(R)ock, (P)aper, or (S)cissors?...0 to end game:”
4. Game play continues in a loop until the User presses 0 to end the game.
5. At the end of the game, the program must provide statistics for the winning percentage for each the User and the
Computer
6. If the User’s winning percentage is greater than or equal to the Computer’s winning percentage, then the User wins (the
User wins if the final percentage is a tie).
Requirements – Detailed:
1. You program must include three (3) .java class files:
a. Java class file #1 - RockPaperScissors.java
i. This class will include the main method and must include the main loop for running the game
ii. This class will create one User object from the User class you will be creating
iii. The reference identifier for this User object must be the word human
iv. This class will create one Computer object from the Computer class you will be creating
v. The reference identifier for this Computer object must be the word robot
vi. This class will check to see if the User has typed in 0 before starting each round of play and will skip the
round of play if 0 has indeed been entered
vii. Before the program ends, this class will display the win percentage for each the User and the Computer in
a formatted output to 3 decimal places, and will announce which one is the overall winner of the game.
b. Java class file #2 - User.java
i. This class will be the blueprint of the user
ii. This class must include encapsulated (private) instance variables for storing the following information
about the user:
1. The User’s Name
2. The User’s current or most recently played choice (R,P,S, or 0)
3. A running count of the number of wins for the User during the course of the game
4. A running count of the number of ties for the User during the course of the game
5. A running count of the number of loses for the User during the course of the game
6. A win percentage that is calculated after each result
iii. This class must include the following publically available methods:
1. A method to retrieve the User’s Name
2. A method to retrieve the User’s current or most recently played choice (R,P,S, or 0)
3. A method to retrieve the current count of User wins
4. A method to retrieve the current count of User ties
5. A method to retrieve the current count of User losses
6. A method called .play() that will ask the User for their choice (R,P,S, or 0) and save it into the
current or most recently played choice variable
a. This method must continue to ask the User for (R,P,S, or 0) if they do not enter in one of
these values. Put another way, this method must ensure that the User data is one of
these 4 allowable values
b. This method must also echo back, as output, the play from the User as confirmation.
7. A method (or more than one method) to store the result of each play of the game.
a. If the User wins then increment the win variable
b. If the User ties then increment the tie variable
c. If the User loses then increment the loss variable
8. A method that calculates the User’s win percentage after each play
a. Win percentage = count of wins / total number of games played
c. Computer.java
i. This class will be the blueprint of the computer, however, the computer’s choices will be random rather
than User input from the keyboard.
ii. This class must include encapsulated (private) instance variables for storing the following information
about the Computer:
1. A string constant to hold the Computer’s name
2. The Computer’s current or most recently played choice (R,P,S, or 0)
3. A running count of the number of wins for the Computer during the course of the game
4. A running count of the number of ties for the Computer during the course of the game
5. A running count of the number of loses for the Computer during the course of the game
6. A win percentage that is calculated after each result
iii. This class must include the following publically available methods:
1. A method to retrieve the Computer’s Name
2. A method to retrieve the Computer’s current or most recently played choice (R,P,S, or 0)
3. A method to retrieve the current count of Computer wins
4. A method to retrieve the current count of Computer ties
5. A method to retrieve the current count of Computer losses
6. A method called .play() that will randomly assign an R,P,or S to the current or most recently
played variable for the Computer. This method will also output the Computer’s play to the
screen so the User knows what the random selection was.
7. A method (or more than one method) to store the result of each play of the game.
a. If the Computer wins then increment the win variable
b. If the Computer ties then increment the tie variable
c. If the Computer loses then increment the loss variable
8. A method that calculates the Computer’s win percentage after each play
a. Win percentage = count of wins / total number of games played
The Flow of Control:
The following is a high level look at what the flow of control should be for your Rock, Paper, Scissors game:
1. The program starts in the main method
2. Create a new User object called human (the creation involves asking the User for their Name and welcoming them to the
game)
3. Create a new Computer objected called robot
4. Output to the screen to say “Let’s play Rock, Paper, Scissors!”
5. While the user still wants to play (while the User has typed in R,P, or S… but not 0)
a. human.play();
b. robot.play();
c. Conditional logic to see if the User won the round or if the Computer won, or if it’s a tie
d. If the User won, increment the User’s win count and increment the Computer’s loss count
e. If the Computer won, increment the Computer’s win count and increment the User’s loss count
f. If it’s a tie, increment the User’s tie count and increment the Computer’s tie count
g. Recalculate the win percentage for the User
h. Recalculate the win percentage for the Computer
6. When the User eventually types in 0, show the calculated win percentages
a. Win percentage of the User
b. Win percentage of the Computer
7. Output who has won the overall game
8. End of program
Submission:
Submit only the three (3) java class files via Moodle on or before the posted deadline of March 14th, 2019:
1. RockPaperScissors.java (includes your main method)
2. User.java (blueprint for your User object)
3. Computer.java (blueprint for your Computer object)
Reminder, this Assignment is worth 5% of your overall course grade.
Late assignments will not be accepted so make sure you do not miss the deadline!