Your TennisPlayer class should currently contain fields, accessors and mutators
ID: 3714018 • 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. You do NOT have to change the name of TestTennisPlayer3.java
This is the 3rd part to a continuous series, please let me know if updated info needed.
This is Part 2 JAVA CODE (JohnTennisPlayer.java):
public class JohnTennisPlayer
{
//instance variables
private String playerName;
private String country;
private int rank;
private int age;
private int wins;
private int losses;
//default constructor
public JohnTennisPlayer()
{
playerName=null;
country=null;
rank=0;
age=0;
wins=0;
losses=0;
}
//parameterized constructor
public JohnTennisPlayer(String playerName,String country)
{
this.playerName=playerName;
this.country=country;
rank=0;
age=0;
wins=0;
losses=0;
}
//parameterized constructor
public JohnTennisPlayer(String playerName,String country,int rank, int age)
{
this.playerName=playerName;
this.country=country;
this.rank=rank;
this.age=age;
wins=0;
losses=0;
}
//parameterized constructor
public JohnTennisPlayer(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;
}
//all accesor and mutator method for all six fields.
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;
}
//method to display player details
public void displayPlayer()
{
System.out.println("Player's name: " + getPlayerName());
System.out.println("Player's country: " + getCountry());
System.out.println("Player's rank: " + getRank());
System.out.println("Player's age: " + getAge());
System.out.println("Player's wins: " + getWins());
System.out.println("Player's losses: " + getLosses());
System.out.println();
}
}
This is the test for Part 2: JAVA CODE(TestJohnTennisPlayer.java):
public class TestJohnTennisPlayer
{
public static void main(String[] args)
{
//creating JohnTennisPlayer objects
JohnTennisPlayer tp1 = new JohnTennisPlayer();
JohnTennisPlayer tp2 = new JohnTennisPlayer("Nick Kyrgios", "Australia");
JohnTennisPlayer tp3 = new JohnTennisPlayer("Andy Murray", "Great Britain", 2, 29);
JohnTennisPlayer tp4 = new JohnTennisPlayer("Novak Djokovic", "Serbia", 1, 29, 59, 7);
//displaying all the object details
tp1.displayPlayer();
tp2.displayPlayer();
tp3.displayPlayer();
tp4.displayPlayer();
}
}
This is the test for Part 3 *(TennisPlayer3 tp3 = new TennisPlayer3("Stan Wawrinka", "Switzerland") MUST COMPILE*:
public class TestTennisPlayer3
{
public static void main(String[] args)
{
TennisPlayer3 tp1 = new TennisPlayer3("Andy Murray", "Great Britain", 1, 29, 73, 9);
TennisPlayer3 tp2 = new TennisPlayer3("Novak Djokovic", "Serbia", 2, 29, 61, 8);
TennisPlayer3 tp3 = new TennisPlayer3("Stan Wawrinka", "Switzerland");
tp1.displayPlayer();
tp2.displayPlayer();
tp3.displayPlayer();
}
}
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.