LD06: Inheritance due Wed 21 Feb 10:00

\begin{purpose}
In this assignment you will:
\begin{itemize}
\item Study OOP...
...em Refactor code to take advantage of inheritance.
\end{itemize}
\end{purpose}

NOTE: Much of the benefit of this lab day and its preparation will come only if you think deeply about the code you are working with. Don't be in a rush to “get through” the prelab. Really study the existing code.

Preparation for Lab Day

Do these steps:
  1. In your workspace, make sure you have the latest homework files from the base repository. This will create a hw06 directory and provide several example files. If necessary, also get the latest files from your own bitbucket repository. We will be working in the hw06 directory.

  2. The ComputerRPSPlayer class defines a computer Rock-Paper-Scissors player who randomly selects a pose. The class comes endowed with quite a few capabilities, including that of fighting another ComputerRPSPlayer object. Take time to study the code for that class, paying attention to the attributes and the construction of the fight() and pose() methods.

  3. Now turn your attention to the driver. For now look at the initial print statements that explain what the various codes mean. Run the driver to see the results. NOTICE: ComputerRPSPlayer is a type/class, not an object. Why is it possible to call the translate() method in this way? Can you also call the display() method in this way? Why or why not?

  4. The fight() method exemplifies a common pattern in object-oriented programming in which an object is passed as a parameter to a method in an object of the same type. You have already been exposed to this when comparing strings: str1.equals(str2). Look at how the parameter to the fight() method is defined. Then notice the notation for working with the two objects in the method: this and other.

  5. What happens if in the driver you modify the fight statement to read b.fight(a);?

  6. Modify the driver so that instead of playing a single game the computer players engage in a best-of-five match. At the end declare a victor for the match. Some things to consider:
    • playing best-of-five ends when one of the players reaches three wins
    • matches may have fewer or more games depending on how many individual games end in ties and whether one player wins all of the games
    • DO NOT MODIFY ComputerRPSPlayer.java when doing this exercise. Only modify the driver.

Lab Day

Begin by showing your prelab work to the instructor. Then do these steps:

  1. In your workspace make sure you have the most recent code from the base repository and from your homework repository on bitbucket.

  2. Take the code you wrote for the pre-lab work and modify it so that one of the players is a human player and the other is a computer player. Notice that it doesn't compile because the fight method assumes a human is playing a human and a computer is playing a computer. You would need to write two versions of the same fight method to allow a player to play both types of opponents! We will find a more elegant way to deal with this issue in the remaining steps today.

  3. Compare the code of the HumanRPSPlayer and the ComputerRPSPlayer and make note of the ways in which they are the same.

  4. Create a new class called RPSPlayer that contains everything the two classes have in common. Add a pose() method but leave it blank. Once the new class compiles commit your work.

  5. Remove all of the duplicated code (including attributes) from ComputerRPSPlayer and have it inherit that functionality from RPSPlayer. Pay especially close attention to the constructor of ComputerRPSPlayer ... it should invoke the constructor of the base class as its first action. Test the code by having the driver play a best-of-five match between to computer players. Commit your work once the ComputerRPSPlayer method works.

  6. Now do a similar transformation of the HumanRPSPlayer class. That is, it should inherit from RPSPlayer and should be stripped of its duplicate code (including attributes).

  7. Now modify the driver so that it pits a human player against a computer player. The fight() method should now work between the two types of players. Do you understand why that is? Once everything is working as expected, commit your work.

  8. Show your work to the instructor. Be sure to push your latest commit to your bitbucket repository.

  9. If you finish early begin work on the homework assignment.