*****IN JAVA**** Objective: Write one interface and three classes which will mak
ID: 3699415 • Letter: #
Question
*****IN JAVA****
Objective:
Write one interface and three classes which will make a rock paper scissors game. First download the driver.
The interface named RPSPlayerInterface will have the following methods
public int getPoints()
public String getGesture()
public void setPoints(int aPoints)
public void setGesture(String aGesture)
public void chooseGesture()
The next class RPSPlayer implements this interface
It has an instance variable gesture which is a string that corresponds to “rock”, “paper”, or “scissors”
It has another instance variable points that keeps track of how many times the player has won
Create only a default constructor that sets the gesture to “none” and points to 0
Create the accessors and mutators for the instance varaibles
The mutator for setGesture should make sure it is either “rock”, “paper”, or “scissors”. If it is not then tell the user that was a wrong choice and set gesture to “none”
Write the chooseGesture method
In this case just set the instance variable gesture to “none”
Next write the class RPSHumanPlayer which inherits from RPSPlayer
It has an instance variable keyboard which is of type Scanner
Make sure to import java.util.Scanner
Create a default constructor
Calls the parent’s default constructor
Sets up the keyboard to System.in
Override the method chooseGesture
Call the parent’s setGesture method and pass the string gotten from the Scanner keyboard.
You do not have to prompt the user.
Next write the class RPSComputerPlayer which also inherits from RPSPlayer
It has an instance variable rand which is of type Random
Make sure to import java.util.Random
Create a default constructor
Calls the parent’s default constructor
Constructs the rand variable
Override the method chooseGesture
Using the random variable it should randomly select a number
Correspond the number to a string which will either rock, paper, or scissors
For instance if rand equals 0 then it is a rock, else if 1 then paper, else if 2 then scissors
Use the parent’s setGesture to then assign the randomly picked gesture
Explanation / Answer
RPSPlayerInterface.java
public interface RPSPlayerInterface {
public int getPoints();
public String getGesture();
public void setPoints(int aPoints);
public void setGesture(String aGesture);
public void chooseGesture();
}
RPSPlayer.java
public class RPSPlayer implements RPSPlayerInterface {
String gesture;
private int points;
public RPSPlayer()
{
this.gesture="none";
this.points=0;
}
public int getPoints() {
return this.points;
}
public String getGesture() {
return this.gesture;
}
public void setPoints(int points) {
this.points = points;
}
public void setGesture(String gesture) {
if(gesture.equals("rock") || gesture.equals("paper") || gesture.equals("scissors")){
this.gesture = gesture;
}else{
System.out.println("wrong choice");
this.gesture = "none";
}
}
public void chooseGesture() {
this.gesture="none";
}
}
RPSHumanPlayer.java
import java.util.Scanner;
public class RPSHumanPlayer extends RPSPlayer {
private Scanner keyboard;
public RPSHumanPlayer() {
super();
keyboard = new Scanner(System.in);
}
public void chooseGesture()
{
String input=keyboard.nextLine();
if(input.equals("rock") || input.equals("paper") || input.equals("scissors"))this.setGesture(input);
}
}
RPSComputerPlayer.java
import java.util.Random;
public class RPSComputerPlayer extends RPSPlayer {
private Random rand;
public RPSComputerPlayer() {
super();
}
public void chooseGesture() {
rand=new Random();
int number = rand.nextInt(3);
System.out.println(number);
if(number==0)this.setGesture("rock");
else if(number==1)this.setGesture("paper");
else if(number==2)this.setGesture("scissors");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.