Among the weaknesses of the Java class TennisGameState, which was the subject of the previous programming assignment, is in how it reports the current score of a game, as implemented by its getScore() and toString() methods.
For one thing, it makes no provisions for associating a name (e.g., "Elena Rybakina", 2026 U.S. Women's Open champion) to either of the players involved in the game, instead referring to them by the generic "SERVER" and "RECEIVER".
For another, in indicating how many points each player has scored, it shows those numbers literally (e.g., "3-2"), which is not the convention that is followed in the game of tennis. For reasons of tradition (the origin of these practices not being known for sure[1]), in tennis we use LOVE for zero points, 15 for one point, 30 for two points, and 40 for three points. Thus, for example, if the server has won three points and the receiver has won one, the score would be reported as 40 - 15. If the server has won none and the receiver two, the score would be reported as LOVE - 30.
In the case that the game is tied at fewer than three points, we use the term ALL, as in 30-ALL. The score of a game tied at three or more points is DEUCE.
If both players have won at least three points but one player is ahead by one point, we say that that player "has the advantage" and the score is reported as ADVANTAGE X, where X can be either "server" or "receiver" but is more likely to be the name of the player, as in ADVANTAGE Clijsters.[2]
If the game is over, the score is reported as GAME X, where X identifies the winning player (e.g., "server", "Clijsters").
The table below shows several examples. The left column shows "raw" scores indicating the actual number of points won by server and receiver, respectively. The right column shows the preferred way to report those scores, as described above. (The server and receiver are Connors and Borg, respectively.)
| Raw Score | Preferred Form |
|---|---|
| 3-1 | 40 - 15 |
| 0-2 | LOVE - 30 |
| 2-2 | 30-ALL |
| 3-3 | DEUCE |
| 5-5 | DEUCE |
| 7-6 | ADVANTAGE Connors |
| 1-4 | GAME Borg |
For this assignment you are to complete the development of the Java class TennisGameState2, which is a child of the class that was the subject of the previous programming assignment, TennisGameState.
The goal is for TennisGameState2 to rectify the weaknesses of its parent class, as described above. To address the issue of associating names to the players, TennisGameState2 introduces instance variables for holding that data; the values of these variables are provided by the client program (via parameters) when it invokes the constructor.
As for expressing game scores in the traditional fashion, both the getScore() and toString() methods are overridden. The body of the former is left to the student to supply. The (also stubbed) private method pointsInTennisLingo() is intended to be where the translations from zero to "LOVE", one to "15", etc., are carried out. You should include a method called Hello() that simply returns the string "Hello".
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, TennisGameState2Tester. To illustrate what it does, observe below a sample dialog between it and a user (with the user's input shown in boldface). The dialog is with respect to a hypothetical game played between Elena Rybakina and CoCo Gauff 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 the prompt.
Welcome to the TennisGameState2Tester program. Enter name of server: Rybakina Enter name of receiver: Gauff Rybakina(0) serving Gauff(0): LOVE-ALL Enter s or r (or a sequence thereof): srssrr Rybakina(1) serving Gauff(0): 15 - LOVE Rybakina(1) serving Gauff(1): 15-ALL Rybakina(2) serving Gauff(1): 30 - 15 Rybakina(3) serving Gauff(1): 40 - 15 Rybakina(3) serving Gauff(2): 40 - 30 Rybakina(3) serving Gauff(3): DEUCE Enter s or r (or a sequence thereof): w Ignoring meaningless character 'w' Enter s or r (or a sequence thereof): r Rybakina(3) serving Gauff(4): ADVANTAGE Gauff Enter s or r (or a sequence thereof): sss Rybakina(4) serving Gauff(4): DEUCE Rybakina(5) serving Gauff(4): ADVANTAGE Rybakina Rybakina(6) serving Gauff(4): GAME Rybakina 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 TennisGameState2.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. #2, acknowledging any persons or AI's 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.
1 See the Wikipedia entry for Tennis_scoring_system for theories regarding the possible origins of this practice.
2 Kim Clijsters of Belgium was among the top women tennis players in the world during the decade preceding her retirement in 2012.