Your TennisPlayer class should currently contain fields, accessors and mutators
ID: 3713097 • Letter: Y
Question
Your TennisPlayer class should currently contain fields, accessors and mutators for each field, a few constructors and a display method. In this assignment, you will be adding a method and modifying the display method. Create a method called calcWinPercentage() that returns a double representing the winning percentage of the player. (Winning perentage = wins/games.) The value returned will be between 0 (lost all games) and 1 (won all games). Since we are dividing here, we have to make sure we do not divide by 0 and make sure we are not performing integer division. If there are no games, return -1. Modify displayPlayer() to also display winning percentage. Since winning percentage is a calculated value, it is not stored in the object (and there is no data field). Instead of displaying a field, the method calcWinPercentage() will be called. The value returned by the method will be multiplied by 100 for whole number form. Display this value to one digit after the decimal point. To display the percent sign with printf, use %%. If no games have been played, display “No games played” for winning percentage.
Explanation / Answer
package CorrectErrors;
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);
if(losses+wins == 0)
System.out.println("No games played");
else
System.out.println("Winning percentage: "+calcWinPercentage()+"%%");
}
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;
}
public double calcWinPercentage() {
if(losses+wins == 0)
return -1;
else
return wins/(double)(wins+losses);
}
}
==============================================================
Please let me know if there is any concern.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.