Lab 10 In-Class: More Work with Classes

The goal of today's lab is to write a version of Rock, Paper, Scissors that uses classes. In the process, you will gain more experience in writing classes and in working with the interaction of different classes. We will model the game using two classes -- one to represent a play (the player's choice of rock, paper, or scissors) and one to represent a player. (NOTE: The game itself could be modeled using a class though we won't do that.) The main program will be written to let the user play over and over again and keep the score (number of times each player wins).

The Play Class

The Pre-Lab described part of the class Play that will model a player's play in the Rock, Paper, Scissors game. The complete class will have the following components: (NOTE: This is an overview of the class. Read it to see what the class will contain but DON'T WRITE CODE YET!!!)

Implement and test this class as follows:

  1. First define the class including NO MORE than the following methods (you may start with fewer): the two constructors, the method that returns the play (as an R, P, or S), and the toString method.
  2. The file TestPlay.java contains part of a program to test the class. Fill in the instantiations and the method calls as instructed in the comments. Compile and run the program to make sure everything works so far. (NOTE: You only need to compile the TestPlay.java file. The compiler will recognize it uses Play.java and will automatically compile it.)
  3. Now add the equals method to the Play class and add a statement to TestPlay.java to test the method (an if that prints out whether or not the two Play objects play1 and play2 are equal). Run the program to make sure equals is correct.
  4. Finally add the beats method to the Play class. Note that this method should also print a message (see description above) in addition to returning a boolean. It would be helpful in writing this method to declare some local variables -- one for the boolean you will return and one for the value of play2 (otherwise you will need to call a method several times to get the value of play2 -- call it once and store the value in a local variable then use that variable everywhere you need to compare)
  5. Add two if statements to the test program - one to see if player1 beats player2 and another to see if player2 beats player1.
  6. Print the final version of TestPlay.java.

The Player Class

The player in a game can also be viewed as an object. (A game has two players who each choose plays.) For our program, a player will be described by the following instance variables: The class must provide the following methods:

Implement the Player class and fill in code in the file TestPlayer.java to test it. Note that the purpose of TestPlayer.java is just to call each of the methods to make sure they work -- it doesn't actually play the game. Print the final version of TestPlayer.java.

Rock, Paper, Scissors Program

Now we're ready to put all of this together in a program that plays the game. The file RPS.java contains a start for the program. Fill it in (you can use much of your code from the TestPlayer.java program) and test it as follows:
  1. Get the info about the two players and instantiate the player objects.
  2. Use a do...while loop to play the game as many times as the user wishes.
  3. Test the program thoroughly. Run it with two people playing, with a person and a computer, and with two computers. Make sure it works in all cases.
  4. Print your final version of RPS.java, Play.java, and Player.java. Hand In: Hard copies of RPS.java, Play.java, Player.java, TestPlay.java, and TestPlayer.java. Email a tar file of your work to your instructor at cs.roanoke.edu.