Question

In: Computer Science

The answer should be in JAVA. You will design and implement two classes to support a...

The answer should be in JAVA.

You will design and implement two classes to support a client program, RockPaperScissorsGame.java, to simulate Rock-Paper-Scissors game.

Read and understand the client program to find out the requirements for the HandShape and Player classes.

The rules of the Rock-Paper-Scissors game are:

    Scissors✌️ beats Paper✋ that beats Rock✊ that beats Scissors✌️

Additionally, to simplify the game logic (and complexify a little bit the HandSahpe class) , two players cannot show the same hand shape.

The followings are some sample runs:

$java RockPaperScissorsGame
Alice shows Paper
Bob shows Scissors
Bob wins!


$java RockPaperScissorsGame
Alice shows Paper
Bob shows Rock
Alice wins!


$java RockPaperScissorsGame
Alice shows Scissors
Bob shows Rock
Bob wins!

RockPaperScissorsGame.java

public class RockPaperScissorsGame {

        public static void main(String[] args) {
                
                String[] handShapeName = {"Rock", "Paper", "Scissors"};
                
                HandShape handShape = new HandShape();
                
                Player player1 = new Player("Alice");
                Player player2 = new Player("Bob");

                System.out.println(player1.getName() + " shows " + handShapeName[player1.showHand(handShape)]);
                System.out.println(player2.getName() + " shows " + handShapeName[player2.showHand(handShape)]);         
                
                System.out.println(player1.findWinner(player2) + "  wins!");
        }
}

Solutions

Expert Solution

Program:

File name: RockPaperScissorsGame.java

//Include necessary header files

import java.util.ArrayList;

import java.util.Random;

//Define the HandShape class

class HandShape

{

   /*Declare the arratlist to store the handshapes*/

   ArrayList<String> Shapes=new ArrayList<String>();

   //Define the constructor

   HandShape()

   {

       //Call the method "add()"

       Shapes.add("Rock");

       Shapes.add("Paper");

       Shapes.add("Scissors");

   }

   //Define the function "getShape()"

   public String getShape()

   {

       //Object creation

       Random r = new Random();

       /*Randomly select a hand shape from list*/

       String shape= Shapes.get(r.nextInt(Shapes.size()));

        //Call the method "remove()"

        Shapes.remove(shape);

        //Return the shape

        return shape;

   }

}

//Define the player class

class Player

{

   //Variable declaration

   String name;

   String str;

   //Define the constructor

   Player(String name)

   {

       this.name=name;

   }

   //Define the method "setName()"

   public void setName(String name)

   {

       this.name=name;

   }

   //Define the method "getName()"

   public String getName()

   {

       //Return the player name

       return this.name;

   }

   //Define the method "showHand()"

   public int show_Hand(HandShape hs1)

   {

     

       //Call the method "getShape()"

       str=hs1.getShape();

       //If the shape is equal to Rock

       if(str=="Rock")

       {

            //Then, return the index of the handshape

           return 0;

       }

       //If the shape is equal to Paper

       else if(str=="Paper")

            //Then, return the index of the handshape

           return 1;

     

      //Otherwise

       else

           //Return the index of the handshape

           return 2;

   }

   //Define the function "find_Winner()"

   public String find_Winner(Player pla2)

   {

      //Check the condition

      if((this.str=="Scissors" &&       pla2.str=="Rock")||(this.str=="Paper" && pla2.str=="Scissors")||(this.str=="Rock" && pla2.str=="Paper"))

           //Return the player2 name

           return pla2.name;

      //Otherwise

       else

           //Return the other plyer name

           return this.name;

    

   }

}

//Define the class RockPaperScissorsGame

public class RockPaperScissorsGame

{

   //Define the "main()" method

   public static void main(String[] args)

   {

     //Print the statement

     System.out.println("$java RockPaperScissorsGame");

     //Declare the variables

     String[] handShapeName= {"Rock","Paper","Scissors"};

     //Create object for "HandShape" class

     HandShape hs=new HandShape();

     //Create object for "player" class

     Player p1=new Player("Alice");

     Player p2=new Player("Bob");

     

     //Print the result

System.out.println(p1.getName()+" shows "+handShapeName[p1.show_Hand(hs)]);

      //Print the result

System.out.println(p2.getName()+" shows "+handShapeName[p2.show_Hand(hs)]);

     

      //Print the winner

      System.out.println(p1.find_Winner(p2)+" wins!");

   }

}

Output:


Related Solutions

The answer should be in JAVA. You will design and implement two classes to support a...
The answer should be in JAVA. You will design and implement two classes to support a client program, RockPaperScissorsGame.java, to simulate Rock-Paper-Scissors game. Read and understand the client program to find out the requirements for the HandShape and Player classes. The rules of the Rock-Paper-Scissors game are:     Scissors✌️ beats Paper✋ that beats Rock✊ that beats Scissors✌️ Additionally, to simplify the game logic (and complexify a little bit the HandSahpe class) , two players cannot show the same hand shape....
JAVA PROGRAM Question : Design and implement two classes called InfixToPostfix and PostFixCalculator. The InfixToPrefix class...
JAVA PROGRAM Question : Design and implement two classes called InfixToPostfix and PostFixCalculator. The InfixToPrefix class converts an infix expression to a postfix expression. The PostFixCalculator class evaluates a postfix expression. This means that the expressions will have already been converted into correct postfix form. Write a main method that prompts the user to enter an expression in the infix form, converts it into postfix, displays the postfix expression as well as it's evaluation. For simplicity, use only these operators,...
JAVA - Design and implement a class called Flight that represents an airline flight. It should...
JAVA - Design and implement a class called Flight that represents an airline flight. It should contain instance data that represent the airline name, the flight number, and the flight’s origin and destination cities. Define the Flight constructor to accept and initialize all instance data. Include getter and setter methods for all instance data. Include a toString method that returns a one-line description of the flight. Create a driver class called FlightTest, whose main method instantiates and updates several Flight...
This program focuses on programming with Java Collections classes. You will implement a module that finds...
This program focuses on programming with Java Collections classes. You will implement a module that finds a simplified Levenshtein distance between two words represented by strings. Your program will need support files LevenDistanceFinder.Java, dictionary.txt, while you will be implementing your code in the LevenDistanceFinder.java file. INSTRUCTIONS The Levenshtein distance, named for it's creator Vladimir Levenshtein, is a measure of the distance between two words. The edit distance between two strings is the minimum number of operations that are needed to...
Design and implement a class Rectangle to represent a rectangle. You should provide two Constructors for...
Design and implement a class Rectangle to represent a rectangle. You should provide two Constructors for the class, the first being the default constructor and the second which takes the basic dimensions and sets up the private member variables with the appropriate initial values. Methods should be provided that allow a user of the class to find out the length, width, area and perimeter of the shape plus a toString()method to print the values of the basic dimensions. Now implement...
Question : Design and implement two classes called InfixToPostfix and PostFixCalculator. The InfixToPrefix class converts an...
Question : Design and implement two classes called InfixToPostfix and PostFixCalculator. The InfixToPrefix class converts an infix expression to a postfix expression. The PostFixCalculator class evaluates a postfix expression. This means that the expressions will have already been converted into correct postfix form. Write a main method that prompts the user to enter an expression in the infix form, converts it into postfix, displays the postfix expression as well as it's evaluation. For simplicity, use only these operators, + ,...
JAVA Specify, design, and implement a class called PayCalculator. The class should have at least the...
JAVA Specify, design, and implement a class called PayCalculator. The class should have at least the following instance variables: employee’s name reportID: this should be unique. The first reportID must have a value of 1000 and for each new reportID you should increment by 10. hourly wage Include a suitable collection of constructors, mutator methods, accessor methods, and toString method. Also, add methods to perform the following tasks: Compute yearly salary - both the gross pay and net pay Increase...
(Code in Java please) You need to implement the GarageDoor, GarageDoorUpCommand and GarageDoorDownCommand classes (similar to...
(Code in Java please) You need to implement the GarageDoor, GarageDoorUpCommand and GarageDoorDownCommand classes (similar to Light, LightOnCommand and LightOffCommand), AND (edited) Implement the CeilingFan class (with state, see p. 220), AND CeilingFanHighCommand (p. 221), CeilingFanMediumCommand, CeilingFanLowCommand, CeilingFanOffCommand (WITH Undo), AND TEST your CeilingFan classes (see pp. 222 and 223), AND Finally, implement and Test the MacroCommand class on pp. 224-227 EXAMPLE output for homework assignment…………… ======= The COMMAND PATTERN ============= guest house garage door is UP guest house garage...
Java - Design and implement an application that creates a histogram that allows you to visually...
Java - Design and implement an application that creates a histogram that allows you to visually inspect the frequency distribution of a set of values. The program should read in an arbitrary number of integers that are in the range 1 to 100 inclusive; then produce a chart similar to the one below that indicates how many input values fell in the range 1 to 10, 11 to 20, and so on. Print one asterisk for each value entered. Sample...
JAVA: You're given two classes List.java and Node.java. In your List class you're supposed to implement...
JAVA: You're given two classes List.java and Node.java. In your List class you're supposed to implement the methods toFront, print, and toBack. The Node class is used as a reference no edit necessary. LIST: public class List { protected Node head, tail; /** * Initialize the list to empty. Both head and tail * are null references in this case. */ public List() { // TODO Auto-generated constructor stub head = tail = null; } /** * Add nodeToAdd to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT