Your TennisPlayer class should currently contain fields, and accessors and mutat
ID: 3737604 • Letter: Y
Question
Your TennisPlayer class should currently contain fields, and accessors and mutators for each field.
In this assignment, 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.
Explanation / Answer
TennisPlayer.java
public class TennisPlayer
{
private String playerName;
private String country;
private int wins;
private int losses;
private int rank;
private int age;
public TennisPlayer() {
playerName=null;
country=null;
rank=0;
age=0;
wins=0;
losses=0;
}
public TennisPlayer(String name, String country, int rank, int age) {
this.country=country;
this.playerName=name;
this.age=age;
this.rank=rank;
wins=0;
losses=0;
}
public TennisPlayer(String name, String country, int rank, int age, int wins, int losses) {
this.country=country;
this.playerName=name;
this.age=age;
this.rank=rank;
this.wins=wins;
this.losses=losses;
}
public void displayPlayer() {
System.out.println("Player Name: "+playerName+" Country: "+country+" Age: "+age+" Rank: "+rank+" Wins: "+wins+" Losses: "+losses);
}
public String getPlayerName() {
return playerName;
}
public void setPlayerName(String playerName) {
this.playerName = playerName;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getWins() {
return wins;
}
public void setWins(int wins) {
this.wins = wins;
}
public int getLosses() {
return losses;
}
public void setLosses(int losses) {
this.losses = losses;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.