Question

In: Computer Science

Implement the constructor, accessor (getter) and mutator (setter) methods indicated in the comments below for the...

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;
}
}

Solutions

Expert Solution

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


Related Solutions

Convert the following UML diagram into the Java code. Write constructor, mutator and accessor methods for...
Convert the following UML diagram into the Java code. Write constructor, mutator and accessor methods for the given class. Create an instance of the class in a main method using a Practice class. Note: The structure of the class can be compiled and tested without having bodies for the methods. Just be sure to put in dummy return values for methods that have a return type other than void.      
class DoubleLinkedList { public: //Implement ALL following methods. //Constructor. DoubleLinkedList(); //Copy constructor. DoubleLinkedList(const DoubleLinkedList & rhs);...
class DoubleLinkedList { public: //Implement ALL following methods. //Constructor. DoubleLinkedList(); //Copy constructor. DoubleLinkedList(const DoubleLinkedList & rhs); //Destructor. Clear all nodes. ~DoubleLinkedList(); // Insert function. Returns true if item is inserted, // false if the item it a duplicate value bool insert(int x); // Removes the first occurrence of x from the list, // If x is not found, the list remains unchanged. void remove(int x); //Assignment operator. const DoubleLinkedList& operator=(const DoubleLinkedList & rhs); private: struct node{ int data; node* next;...
In the following class public class Single { private float unique; ... } Create constructor, setter...
In the following class public class Single { private float unique; ... } Create constructor, setter and getter methods, and toString method. Write a program that request a time interval in seconds and display it in hours, minutes, second format. NEED THESE ASAP
In the following class: public class Truth { private boolean yes_no; ... } Create constructor, setter...
In the following class: public class Truth { private boolean yes_no; ... } Create constructor, setter and getter methods, and toString method.
Implement the Nickel class. Include Javadoc comments for the class, public fields, constructors, and methods of...
Implement the Nickel class. Include Javadoc comments for the class, public fields, constructors, and methods of the class. I have added the Javadoc comments but please feel free to format them if they are done incorrectly. public class Nickel implements Comparable { private int year; /** * The monetary value of a nickel in cents. */ public final int CENTS = 5; /** * Initializes this nickel to have the specified issue year. * * @param year * * @pre....
1.) According to the UML Class Diagram, these are the mutator and accessor methods that you...
1.) According to the UML Class Diagram, these are the mutator and accessor methods that you need to define: 1a.) +setName(value:String):void 1b.) +setGPA(value:double):void 1c.) +setID(value: String):void 1d.) +getName(): String 1e.) +getLastName():String 2.) Working with constructors 2a.) +Student() : This is the default constuctor. For the Strings properties, initialize them to null. The order is String ID, String name, String lastName, and double GPA. 2b.) +Student(value:String) - This constructor receives just the ID. 2c.) +Student(value:String, var: String) - This constructor receives...
(In C++) Task 1: Implement a code example of Constructor Chaining / Constructor Overloading. Make sure...
(In C++) Task 1: Implement a code example of Constructor Chaining / Constructor Overloading. Make sure to label class and methods with clear descriptions describing what is taking place with the source code. Attach a snipping photo of source code and output
Please write python code for the following. Implement the functions defined below. Include a triple-quoted comments...
Please write python code for the following. Implement the functions defined below. Include a triple-quoted comments string at the bottom displaying your output. Using sets (described in Deitel chapter 6) will simplify your work. Below is the starter template for the code: def overlap(user1, user2, interests): """ Return the number of interests that user1 and user2 have in common """ return 0    def most_similar(user, interests): """ Determine the name of user who is most similar to the input user...
Please create a python module named homework.py and implement the classes and methods outlined below. Below...
Please create a python module named homework.py and implement the classes and methods outlined below. Below you will find an explanation for each class and method you need to implement. When you are done please upload the file homework.py to Grader Than. Please get started as soon as possible on this assignment. This assignment has many problems, it may take you longer than normal to complete this assignment. This assignment is supposed to represent a group of students in a...
Create an application that uses a constructor and two different methods. JAVA
Create an application that uses a constructor and two different methods. JAVA
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT