A game of tennis consists of a sequence of points, each of which is won by one "side" or the other. (A side is most often composed of one person or a team of two.) One side is referred to as the server and the other side 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 side plays the role of server for an entire game.
A side wins a game of tennis as soon as it has scored at least four points while also outscoring the other side by at least two. (Thus, for a side to win a game having scored four points requires the other side to have scored at most two points. To win with five or more points requires the other side to have scored exactly two fewer.)
In most circumstances, to report the score of a tennis game we state the number of points won by the server followed by the number of points won by the receiver. However, for reasons of tradition, we don't use 0, 1, 2, 3, etc., when indicating the number of points won. Rather, we use LOVE for zero points, 15 for one point, 30 for two points, and 40 for three points.[1] 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 sides have won at least three points but one side is ahead by one point, we say that that side "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 side, as in ADVANTAGE Clijsters.[2]
If the game is over, the score is reported as GAME X, where X identifies the winning side (e.g., "server", "Clijsters").
The table below shows several examples. In the leftmost column appear "raw" scores indicating the actual number of points won by server and receiver, respectively. The middle column shows the preferred way to report those scores, as described above. The rightmost column shows acceptable, but non-preferred, ways of expressing those scores. (The server and receiver are Connors and Borg, respectively.)
Raw Score | Preferred Form | Acceptable Form |
---|---|---|
3-1 | 40 - 15 | 40 - 15 |
0-2 | LOVE - 30 | 0 - 30 |
2-2 | 30 ALL | 30 - 30 |
3-3 | DEUCE | 40 - 40 |
5-5 | DEUCE | 40 - 40 |
7-6 | ADVANTAGE Connors | ADVANTAGE - 40 |
1-4 | GAME Borg | 15 - GAME |
For this assignment you are to complete the development of the Java class TennisGameScore. As its name suggests, an instance of this class is intended to represent the score during 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 TennisGameScore, you may recognize the need for certain "utility" 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 these names: serverHasWon(), receiverHasAdvantage(), isDeuce(), and pointsInTennisLingo().
Consider the hypothetical serverHasWon() method. It could be quite useful to both the isOver() and getScore() methods. For that matter, the getScore() method, which needs to distinguish between at least five ways of expressing a score, could make use of all four hypothetical methods mentioned above. The last of them (which is rather awkwardly named, admittedly) is envisioned to translate points into tennis lingo (e.g., 0 maps to "LOVE", 1 maps to "15", etc.).
You are also free to introduce new instance variables, although the ones whose declarations were provided are quite adequate.
The getScore() method is responsible for producing a String (e.g., "15 - 40", "ADVANTAGE Borg") describing the game's score. If you are able, make it produce strings in the "preferred" form, as shown in the table above. If not, make it produce strings in the "acceptable" form, which may be a bit easier. (Of course, the closer you come to the preferred form, the more you will be rewarded.)
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.
For the purpose of testing your class, you are provided with a client application program, TennisGameScoreTester. To give you an idea of what it does, below you will find sample dialogues between it and a user (with the user's input shown in boldface). The two dialogues are with respect to a hypothetical game played between Maria Sharapova and Kim Clijsters. They differ only in that one shows the scores in the preferred form while the other shows them in a non-preferred, but acceptable, form.
Preferred | Acceptable |
---|---|
Enter name of server: Sharapova Enter name of receiver: Clijsters Sharapova(0) serving Clijsters(0): LOVE ALL Enter s or r (or a sequence thereof): s Sharapova(1) serving Clijsters(0): 15 - LOVE Enter s or r (or a sequence thereof): srr Sharapova(2) serving Clijsters(0): 30 - LOVE Sharapova(2) serving Clijsters(1): 30 - 15 Sharapova(2) serving Clijsters(2): 30 ALL Enter s or r (or a sequence thereof): sr Sharapova(3) serving Clijsters(2): 40 - 30 Sharapova(3) serving Clijsters(3): DEUCE Enter s or r (or a sequence thereof): rssrr Sharapova(3) serving Clijsters(4): ADVANTAGE Clijsters Sharapova(4) serving Clijsters(4): DEUCE Sharapova(5) serving Clijsters(4): ADVANTAGE Sharapova Sharapova(5) serving Clijsters(5): DEUCE Sharapova(5) serving Clijsters(6): ADVANTAGE Clijsters Enter s or r (or a sequence thereof): r Sharapova(5) serving Clijsters(7): GAME Clijsters Points won by Sharapova: 5 Points won by Clijsters: 7 Goodbye. |
Enter name of server: Sharapova Enter name of receiver: Clijsters Sharapova(0) serving Clijsters(0): 0 - 0 Enter s or r (or a sequence thereof): s Sharapova(1) serving Clijsters(0): 15 - 0 Enter s or r (or a sequence thereof): srr Sharapova(2) serving Clijsters(0): 30 - 0 Sharapova(2) serving Clijsters(1): 30 - 15 Sharapova(2) serving Clijsters(2): 30 - 30 Enter s or r (or a sequence thereof): sr Sharapova(3) serving Clijsters(2): 40 - 30 Sharapova(3) serving Clijsters(3): 40 - 40 Enter s or r (or a sequence thereof): rssrr Sharapova(3) serving Clijsters(4): 40 - ADVANTAGE Sharapova(4) serving Clijsters(4): 40 - 40 Sharapova(5) serving Clijsters(4): ADVANTAGE - 40 Sharapova(5) serving Clijsters(5): 40 - 40 Sharapova(5) serving Clijsters(6): 40 - ADVANTAGE Enter s or r (or a sequence thereof): r Sharapova(5) serving Clijsters(7): 40 - GAME Points won by Sharapova: 5 Points won by Clijsters: 7 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 TennisGameScore.java) to the relevant Brightspace dropbox. (Again, submit the .java file, not the .class file, nor any other file.) Make sure to include comments in your source code identifying yourself, indicating that it is a solution to CMPS 134 Prog. Assg. #6, acknowledging any persons 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.