Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a class named TennisGame that holds data about a single tennis game. Thec

ID: 3765245 • Letter: C

Question

Create a class named TennisGame that holds data about a single tennis game. Theclass has six fields: the names of the two players, the integer final scores for theplayers, and the String values of the final scores. Include a get method for each of the six fields. Also include a set method that accepts two players ’ names, andanother set method that accepts the two integer final score values. The integer finalscore for a player is the number of points the player won during the game; this valueshould be in the range of 0 through 4. If either of the set method parameters for ascore is not in the range of 0 through 4, assign 0 to both scores and assign “ error ” tothe String scores. If both players ’ score parameters are 4, assign 0 to both scoresand “ error ” to the String scores. The String score values are set by the method thatsets the integer score values. The String final score for each player contains thetraditional names for points in tennis: love , 15 , 30 , 40 , or game , respectively, for the values 0 through 4. Create a subclass named DoublesTennisGame that includes twoadditional fields for the names of the first two players ’ partners. Include get methodsfor the names. Override the parent class setNames() method to accept the names of all four players. Write an application named DemoTennisGames that instantiatesseveral objects of each of these classes. Demonstrate that all the methods assigncorrect values. Save the files as TennisGame.java , DoublesTennisGame.java ,and DemoTennisGames.java

Explanation / Answer

TennisGame.java

public class TennisGame {
   private String player1Name;
   private String player2Name;
   private int player1Score;
   private int player2Score;
   private String player1ScoreInString;
   private String player2ScoreInString;
  
   public String getPlayer1Name() {
       return player1Name;
   }
   public String getPlayer2Name() {
       return player2Name;
   }
   public int getPlayer1Score() {
       return player1Score;
   }
   public int getPlayer2Score() {
       return player2Score;
   }
   public String getPlayer1ScoreInString() {
       return player1ScoreInString;
   }
   public String getPlayer2ScoreInString() {
       return player2ScoreInString;
   }
   public void setPlayerNames(String player1Name, String player2Name) {
       this.player1Name = player1Name;
       this.player2Name = player2Name;
   }
   public void setPlayerScores(final int player1Score, final int player2Score) {
       this.player1Score = player1Score;
       this.player2Score = player2Score;
      
       if (this.player1Score < 0 || this.player1Score > 4) {
           this.player1Score = 0;
           this.player1ScoreInString = "error";
       }
       if (this.player2Score < 0 || this.player2Score > 4) {
           this.player2Score = 0;
           this.player2ScoreInString = "error";
       }
       if (this.player1Score == this.player2Score && this.player1Score == 4) {
           this.player1Score = 0;
           this.player1ScoreInString = "error";
           this.player2Score = 0;
           this.player2ScoreInString = "error";
       }
       if (this.player1ScoreInString == null) {
           switch(this.player1Score) {
           case 0:
               this.player1ScoreInString = "love";
               break;
           case 1:
               this.player1ScoreInString = "15";
               break;
           case 2:
               this.player1ScoreInString = "30";
               break;
           case 3:
               this.player1ScoreInString = "40";
               break;
           case 4:
               this.player1ScoreInString = "game";
           }
       }
       if (this.player2ScoreInString == null) {
           switch(this.player2Score) {
           case 0:
               this.player2ScoreInString = "love";
               break;
           case 1:
               this.player2ScoreInString = "15";
               break;
           case 2:
               this.player2ScoreInString = "30";
               break;
           case 3:
               this.player2ScoreInString = "40";
               break;
           case 4:
               this.player2ScoreInString = "game";
           }
       }
   }
}

DoubleTennisGame.java

public class DoubleTennisGame extends TennisGame{
   private String player1PartnerName;
   private String player2PartnerName;
  
   public String getPlayer1PartnerName() {
       return player1PartnerName;
   }
   public String getPlayer2PartnerName() {
       return player2PartnerName;
   }
   public void setPlayerNames(String player1Name, String player2Name, String player1PartnerName, String player2PartnerName) {
       super.setPlayerNames(player1Name, player2Name);
       this.player1PartnerName = player1PartnerName;
       this.player2PartnerName = player2PartnerName;
      
   }
}

DemoTennisGame.java

package chegg;

public class DemoTennisGame {
  
   public static void main(String[] args) {
       TennisGame game1 = new TennisGame();
       game1.setPlayerNames("Roger Federer", "Rafael Nadal");
       game1.setPlayerScores(3, 4);
       System.out.println("Player 1 name = " + game1.getPlayer1Name());
       System.out.println("Player 2 name = " + game1.getPlayer2Name());
       System.out.println("Player 1 score = " + game1.getPlayer1ScoreInString());
       System.out.println("Player 2 score = " + game1.getPlayer2ScoreInString());
      
       DoubleTennisGame doubleGame1 = new DoubleTennisGame();
       doubleGame1.setPlayerNames(game1.getPlayer1Name(), game1.getPlayer2Name(), "Andy Murray", "Novak Djokovic");
       System.out.println("Player 1 partner name = " + doubleGame1.getPlayer1PartnerName());
       System.out.println("Player 2 partner name = " + doubleGame1.getPlayer2PartnerName());
   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote