In: Computer Science
Implement the constructor, accessor (getter) and mutator (setter) methods indicated in the comments below for the following class Player.
public class PlayerTester
{
public static void main(String[] args)
{
Player p = new Player("John Adam");
p.addScore(70);
p.addScore(80);
System.out.println("Average score is " + p.averageScore());
}
}
public class Player
{
private String name;
private double totalScore;
private int gamesPlayed;
//constructor with one parameter for the player name - 2 points
//Three accessor methods: getName(), getTotalScore(), getGamesPlayed() - 2 points each
//One mutator method: addScore(double) - 2 points
public double averageScore()
{
double avg = totalScore / gamesPlayed;
return avg;
}
}
public class PlayerTester
{
public static void main(String[] args)
{
Player p = new Player("John Adam");
p.addScore(70);
p.addScore(80);
System.out.println("Average score is " + p.averageScore());
}
}
class Player
{
private String name;
private double totalScore;
private int gamesPlayed;
//constructor with one parameter for the player name - 2
points
Player(String name)
{
this.name = name;
}
//Three accessor methods: getName(), getTotalScore(),
getGamesPlayed() - 2 points each
String getName()
{
return name;
}
double getTotalScore()
{
return totalScore;
}
int getGamesPlayed()
{
return gamesPlayed;
}
//One mutator method: addScore(double) - 2 points
void addScore(double totalScore)
{
this.totalScore = totalScore;
gamesPlayed++;
}
public double averageScore()
{
double avg = totalScore / gamesPlayed;
return avg;
}
}
OUTPUT: Average score is
40.0