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

Download the file TennisPlayer.java and rename it <yourLastName>TennisPlayer.jav

ID: 3916656 • Letter: D

Question

Download the file TennisPlayer.java and rename it <yourLastName>TennisPlayer.java. You will have to change the name of the class.

Download the files TestTennisPlayer1.java and TestTennisPlayer2.java. These files will be used to test your class. You do not have to change the name of these files.

Part 1 – Define accessors and mutators

TennisPlayer.java contains a class with six data fields, which are data that would be stored for a tennis player. Define an accessor and mutator method for all six fields.

The java program TestTennisPlayer1.java can be used to test your methods. You will need to put this file in the same folder as your TennisPlayer.java file. Since your tennis player class name includes your last name, you will have to modify this file to reflect your class name. A successful run of this program ensures your methods work correctly.

This will be the output:

Part 2 – Add constructors and display method

You will be adding constructors and a display method to your class. Remember, after you add any constructor method, Java’s default constructor will no longer be available. So if you add only a constructor that defines some parameters, you will not be able to call a constructor with no arguments.

Add a no-argument constructor to your TennisPlayer class. This constructor should initialize all the fields. Set the String fields to null and the numeric fields to 0.

Create a constructor method that defines two parameters: one for the player’s name and one for the country. This constructor will set these two fields using the parameters. Set the other values to default values (0 for numeric values).

Create a constructor method that defines four parameters: one for the player’s name, one for the player’s country, one for the rank and one for age. This constructor will set these fields using the parameters. Set the other values to default values (0 for numeric values).

Create a constructor method that defines six parameters: one for the player’s name, one for the player’s country, one for rank, one for age, one for wins, and one for losses. This constructor will set these fields using the parameters.

Create a method called displayPlayer() to your class. This method will nicely display all the tennis player’s information. It will print the player’s name, country, rank, age, wins and losses. See my output below for a formatting guide. Yours does not have to be identical, but it should be nicely formatted and readable.   This method does not define any paramters and does not return a value.

In your TennisPlayer.java file, the data fields should come first, then the constructor methods, then the accessors and mutators for each field, then other methods like the display method.

The java program TestTennisPlayer2.java can be used to test your constructor methods and display method.   You will need to put this file in the same folder as your TennisPlayer.java file. Since your tennis player class name includes your last name, you will have to modify this file to reflect your class name. A successful run of this program ensures your methods work correctly.  

run TestTennisPLaver1 Player 1 name: Roger Federer Player 1 country: Switzerland Player 1 rank: 1 Player 1 age: 36 Player 1 wins: 17 Player 1 losses: 2 Player 2 name: Rafael Nadal Player 2 country: Spain Player 2 rank: 2 Player 2 age: 31 Player 2 wins: 18 Player 2 losses: 2

Explanation / Answer

/*Part 1*/

class TennisPlayer{

   private String name;
   private String country;
   private int rank;
   private int age;
   private int wins;
   private int losses;

   public void setName(String a){
       name = a;
   }
   public String getName(){
       return name;
   }
   public void setCountry(String a){
       country = a;
   }
   public String getCountry(){
       return country;
   }
   public void setRank(int a){
       rank = a;
   }
   public int getRank(){
       return rank;
   }
   public void setAge(int a){
       age = a;
   }
   public int getAge(){
       return age;
   }
   public void setWins(int a){
       wins = a;
   }
   public int getWins(){
       return wins;
   }
   public void setLosses(int a){
       losses = a;
   }
   public int getLosses(){
       return losses;
   }
}

/*Part 2 */


class TennisPlayer{

   private String name;
   private String country;
   private int rank;
   private int age;
   private int wins;
   private int losses;


   public TennisPlayer(){
       name = null;
       country = null;
       rank = 0;
       age = 0;
       wins = 0;
       losses = 0;
   }
   public TennisPlayer(String n, String c){
       name = n;
       country = c;
       rank = 0;
       age = 0;
       wins = 0;
       losses = 0;
   }
   public TennisPlayer(String n, String c, int r, int a){
       name = n;
       country = c;
       rank = r;
       age = a;
       wins = 0;
       losses = 0;
   }
   public TennisPlayer(String n, String c, int r, int a, int w, int l){
       name = n;
       country = c;
       rank = r;
       age = a;
       wins = w;
       losses = l;
   }
   public void display(){
      if (name == null)
         System.out.println("Tennis Player: null");
      else
         System.out.println("Tennis Player:" + name);
      if (country == null)
         System.out.println("Country: null");
      else
         System.out.println("Country:" + country);
      System.out.println("Current Rank:" + rank);
      System.out.println("Age:" + age);
      System.out.println("Wins:" + wins);
      System.out.println("Losses:" + losses);
   }
   public void setName(String a){
       name = a;
   }
   public String getName(){
       return name;
   }
   public void setCountry(String a){
       country = a;
   }
   public String getCountry(){
       return country;
   }
   public void setRank(int a){
       rank = a;
   }
   public int getRank(){
       return rank;
   }
   public void setAge(int a){
       age = a;
   }
   public int getAge(){
       return age;
   }
   public void setWins(int a){
       wins = a;
   }
   public int getWins(){
       return wins;
   }
   public void setLosses(int a){
       losses = a;
   }
   public int getLosses(){
       return losses;
   }
}

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