JAVA INTRO COURSE You will have to write the following file for the PRSGame.java
ID: 3761520 • Letter: J
Question
JAVA INTRO COURSE
You will have to write the following file for the PRSGame.java. There will be two private fields score: int p2choice: int
Constructor
You will to create a constructor that will use the setScore() to set the score to 0 and set the p2choice to a random number between 1 and 3.
setScore() There will be a setter called setScore(), it will return nothing, but take in int s. It will check to see if the int s passed to it is equal to or less than 0 and set score to zero, else it will set the score to s.
getScore() The getter getScore() will return the field score.
checkWinner() This method will take in a variable int c and compare that to p2choice.
First it will use a for loop to count to 3, then it will check the results. Use the scanner.nextLine to take in the pressing of enter.
Then compare the two player choices(hint: may be useful to use a switch) This will return a string that will let the player know if they won or lost. If they win, score increases by one, if they lose score decreases by one, if they tie, no change.
newChoice() This will be a public method that resets the computer player to a random choice from 1-3 in p2choice.
follow this class outline using the above code as the main class
* To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools| Templates and open the template in the editor. package midterm2; import java.util.Scanner; public class Midterm2 t pub llc stat1c Vold maln(stringll args) i // TODO code application logic here int choice; Scanner inputnew Scanner (System.in); PRSGame player2 = new PRSGame(); int score 0; 0 System.out.println("Score is: "+ player2.getScorel)); menu choice = input. nextInt ( ) ; switch(choice)Explanation / Answer
Midterm2.java:
import java.util.Scanner;
public class Midterm2
{
public static void main(String[] args)
{
int choice;
Scanner input=new Scanner(System.in);
PRSGame player2 =new PRSGame();
int score=0;
do{
System.out.println("Score is:" + player2.getScore());
menu();
choice =input.nextInt();
switch(choice){
case 1:{
System.out.println("You choose Rock, nice and solid!");
System.out.println(player2.checkWinner(choice));
score =player2.getScore();
break;
}
case 2:{
System.out.println("You choose Paper, must be feeling flat!");
System.out.println(player2.checkWinner(choice));
score =player2.getScore();
break;
}
case 3:{
System.out.println("You choose Siccors, how cutting edge!");
System.out.println(player2.checkWinner(choice));
score =player2.getScore();
break;
}
case 0:{
System.out.println("Thanks for playing");
break;
}
default:{
System.out.println("That is not a choice!");
break;
}
}
player2.newChoice();
}while(choice!=0);
}
public static void menu(){
System.out.println("Please select a choice: 1-Rock 2-Paper 3-Sissors 0-End");
}
}
PRSGame.java
public class PRSGame
{
int score;
int p2choice;
PRSGame()
{
setScore(0);
p2choice = (int) (Math.random() * 3 + 1);
}
public void setScore(int s) {
if (s <= 0)
score = 0;
else
score = s;
}
public int getScore() {
return score;
}
public void print()
{
if(p2choice ==1)
System.out.println("Rock");
if(p2choice ==2)
System.out.println("Paper");
if(p2choice ==3)
System.out.println("Sissors");
}
public String checkWinner(int c) {
print();
if (p2choice == c) {
return "Draw";
}
if (p2choice < c) {
score++;
return "Win";
}
if (p2choice > c) {
score--;
return "Lost";
}
return "";
}
public void newChoice() {
p2choice = (int) (Math.random() * 3 + 1);
}
}
Sample output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.