A game of tennis involves two "players" and consists of a sequence of points, each scored by one player or the other. (In "doubles" play, each player is actually two people.) One player is referred to as the server and the other is referred to as the receiver. The server initiates each point by hitting the ball over the net to the receiver, but this detail is not of concern to us. What is relevant is that, unlike in volleyball or ping pong, one player stays in the role of server for an entire game.
A game of tennis ends as soon as one player has scored at least four points while also outscoring the opponent by at least two. (Thus, for a player to win a game having scored four points requires the other side to have scored at most two points. To win with five points requires the opponent to have scored exactly three. And so on.)
In reporting a score, we list the number of points scored by the server followed by the number scored by the receiver. Thus, for example, a score of 3−1 means that the server has scored three points and the receiver only one. Certain game scores have terminology associated to them:
For this assignment you are to complete the development of the Java class TennisGameState. As its name suggests, an instance of this class is intended to represent the state of a game of tennis.
The heading of every public method that is to appear in the class has been provided, along with comments specifying its intended behavior. The bodies of most of these methods have been left either empty or containing only a single statement that returns a "dummy" value to its caller. (Such methods are referred to as stubs.) Your task, of course, is to supply the code missing from these method bodies that will result in the methods behaving as specified. You are not to change any public method's heading, nor are you to add any new public methods!
In developing the bodies of the public methods in TennisGameState, you may recognize the utility of introducing methods that are not already there. You are free to incorporate such methods (which should be declared to be private) into the class. Possible examples of such methods are ones having purposes suggested by the names hasWon() and hasAdvantage(). The former could be used by both of the public methods serverHasWon() and receiverHasWon(). The latter could be used by both of the public methods serverHasAdvantage() and receiverHasAdvantage().
You are also free to introduce new instance variables, although the ones whose declarations were provided are quite adequate.
You will notice that TennisGameState includes the declaration and use of an enum ("enumeration") type called Player. Also, its toString() method makes use of an instance of the class StringBuilder. Objects of that class are just like those of the more familiar String class, except that they are "mutable". We will discuss these in lecture.
As with all programming assignments, your code is expected to be properly formatted and well-commented. In particular, any new method you introduce should have comments describing its purpose. Also, refrain from including any line of text (whether it be executable code or comment) that extends beyond the 100th column. Violations of these rules may result in significant point deductions.
For the purpose of testing your class, you are provided with a client application program, TennisGameStateTester. To illustrate what it does, observe below the sample dialog between it and a user (with the user's input shown in boldface). An input of s signals that the server has scored a point; r signals that the receiver has done so. (You can escape from the program by entering q.) In response to each input, the program displays the game's current score. Upon the game ending, so does the program.
Welcome to the TennisGameStateTester program. 0 - 0 Enter s or r (or a sequence thereof): s 1 - 0 Enter s or r (or a sequence thereof): s 2 - 0 Enter s or r (or a sequence thereof): r 2 - 1 Enter s or r (or a sequence thereof): r 2 - 2 Enter s or r (or a sequence thereof): s 3 - 2 Enter s or r (or a sequence thereof): r 3 - 3 (Deuce) Enter s or r (or a sequence thereof): r 3 - 4 (Advantage RECEIVER) Enter s or r (or a sequence thereof): s 4 - 4 (Deuce) Enter s or r (or a sequence thereof): s 5 - 4 (Advantage SERVER) Enter s or r (or a sequence thereof): s 6 - 4 (Game SERVER) Goodbye. |
In completing this assignment, you should expect to make extensive use of boolean expressions and their frequent companion, the if-else statement. In particular, expect your code to include several comparisons (using relational operators such as ==, <=, etc.) between integer values and to also possibly include boolean operators such as conjunction (&&) and/or disjunction (||).
Submit your source code file (which must be named TennisGameState.java) to the appropriate Brightspace drop box. Make sure to include comments in your source code identifying yourself, indicating that it is a solution to CMPS 144 Prog. Assg. #1, acknowledging any agents (persons or otherwise) who aided you in developing your solution, and pointing out any flaws or deficiencies of which you are aware.
You may submit a program more than one time. Hence, if, after submitting, you improve your program (e.g., by fixing logic errors), you should submit the newer version.