Download the file TennisPlayer.java and rename it <yourLastName>TennisPlayer.jav
ID: 3734087 • Letter: D
Question
Download the file TennisPlayer.java and rename it <yourLastName>TennisPlayer.java. You will have to change the name of the class.
This file 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 TestTennisPlayer.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. You do NOT have to change the name of TestTennisPlayer.java.
This will be the output:
<run TestTennisPlayer
Player 1 name: John Doe
Player 1 country: America
Player 1 rank: 1
Player 1 age: 40
Player 1 wins: 19
Player 1 losses: 2
Player 2 name: Jane Doe
Player 2 country: Japan
Player 2 rank: 3
Player 2 age: 19
Player 2 wins: 17
Player 2 losses: 4
Explanation / Answer
TestTennisPlayer.java
public class TestTennisPlayer
{
public static void main(String[] args)
{
TennisPlayer tp1 = new TennisPlayer();
TennisPlayer tp2 = new TennisPlayer();
tp1.setPlayerName("Roger Federer");
tp1.setCountry("Switzerland");
tp1.setRank(1);
tp1.setAge(36);
tp1.setWins(12);
tp1.setLosses(0);
tp2.setPlayerName("Juan Martin Del Potro");
tp2.setCountry("Argentina");
tp2.setRank(8);
tp2.setAge(29);
tp2.setWins(11);
tp2.setLosses(3);
System.out.println("Player 1 name: " + tp1.getPlayerName());
System.out.println("Player 1 country: " + tp1.getCountry());
System.out.println("Player 1 rank: " + tp1.getRank());
System.out.println("Player 1 age: " + tp1.getAge());
System.out.println("Player 1 wins: " + tp1.getWins());
System.out.println("Player 1 losses: " + tp1.getLosses());
System.out.println();
System.out.println("Player 2 name: " + tp2.getPlayerName());
System.out.println("Player 2 country: " + tp2.getCountry());
System.out.println("Player 2 rank: " + tp2.getRank());
System.out.println("Player 2 age: " + tp2.getAge());
System.out.println("Player 2 wins: " + tp2.getWins());
System.out.println("Player 2 losses: " + tp2.getLosses());
}
}
TennisPlayer.java
public class TennisPlayer
{
private String playerName;
private String country;
private int rank;
private int age;
private int wins;
private int 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;
}
}
Output:
Player 1 name: Roger Federer
Player 1 country: Switzerland
Player 1 rank: 1
Player 1 age: 36
Player 1 wins: 12
Player 1 losses: 0
Player 2 name: Juan Martin Del Potro
Player 2 country: Argentina
Player 2 rank: 8
Player 2 age: 29
Player 2 wins: 11
Player 2 losses: 3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.