Your TennisPlayer class should currently contain fields, accessors and mutators
ID: 3918896 • 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.
The java program TestTennisPlayer3.java can be used to test your class. 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.
--------------------------------------------
Testing Java
--------------------------------------------
----------------
code
--------------
public class TennisPlayer {
//data fields
private String playerName;
private String country;
private int rank;
private int age;
private int wins;
private int losses;
//constructor methods
public TennisPlayer() {
this.playerName = null;
this.country = null;
this.rank = 0;
this.age = 0;
this.wins = 0;
this.losses = 0;
}
public TennisPlayer(String playerName, String country) {
this.playerName = playerName;
this.country = country;
this.rank = 0;
this.age = 0;
this.wins = 0;
this.losses = 0;
}
public TennisPlayer(String playerName, String country, int rank, int age) {
this.playerName = playerName;
this.country = country;
this.rank = rank;
this.age = age;
this.wins = 0;
this.losses = 0;
}
public TennisPlayer(String playerName, String country, int rank, int age, int wins, int losses) {
this.playerName = playerName;
this.country = country;
this.rank = rank;
this.age = age;
this.wins = wins;
this.losses = losses;
}
public void displayPlayer() {
System.out.println("---------------------------------------");
System.out.println("Tennis Player: "+ this.playerName);
System.out.println("Country: "+ this.country);
System.out.println();
System.out.println("Current Rank: "+ this.rank);
System.out.println();
System.out.println("Age: "+ this.age);
System.out.println();
System.out.println("Wins: "+ this.wins);
System.out.println("Losses: "+ this.losses);
System.out.println("---------------------------------------");
System.out.println();
}
//accessors and mutators
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;
}
}
Explanation / Answer
//Method for calculation of Win Percentage
public double calcWinPercentage() {
int totalGames = wins + losses;
if (totalGames == 0) {
return -1;
}
return wins / totalGames;
}
//Edited Display()
public void displayPlayer() {
System.out.println("---------------------------------------");
System.out.println("Tennis Player: " + this.playerName);
System.out.println("Country: " + this.country);
System.out.println();
System.out.println("Current Rank: " + this.rank);
System.out.println();
System.out.println("Age: " + this.age);
System.out.println();
System.out.println("Wins: " + this.wins);
System.out.println("Losses: " + this.losses);
double winPercentage = calcWinPercentage() * 100;
if (winPercentage < 0) {
System.out.println("No Games Played");
} else {
System.out.printf("Win Percentage: %.1f", calcWinPercentage());
}
System.out.println("---------------------------------------");
System.out.println();
}
//All the best. I hope i was able to answer it
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.