Player classThe Player class will specify the attributes and behaviours of a player. An object of the Player class will have at least the following fields:Name – the name of the playerScore – the highest game score The data type of each field must be chosen carefully and you must be able to justify the choice of the data type of the fields. You may want to include comments in the class to state any assumptions made. The class must also have a default constructor and a non-default constructor that accepts a value for the name of the player.The Player class should also have appropriate accessor and mutator methods for its fields. Validation of values for fields should also be implemented. You should not allow an object of class Player to be set to an invalid state. There should be no input from the terminal or output to the screen via methods from the Player class. A Player object should also be able to return its state in the form of a String.
Question
Player classThe Player class will specify the attributes and behaviours of a player. An object of the Player class will have at least the following fields:Name – the name of the playerScore – the highest game score The data type of each field must be chosen carefully and you must be able to justify the choice of the data type of the fields. You may want to include comments in the class to state any assumptions made. The class must also have a default constructor and a non-default constructor that accepts a value for the name of the player.The Player class should also have appropriate accessor and mutator methods for its fields. Validation of values for fields should also be implemented. You should not allow an object of class Player to be set to an invalid state. There should be no input from the terminal or output to the screen via methods from the Player class. A Player object should also be able to return its state in the form of a String.
Solution
Here is a simple implementation of the Player class in Java:
public class Player {
// Fields
private String name; // Name of the player
private int score; // Highest game score
// Default constructor
public Player() {
this.name = "";
this.score = 0;
}
// Non-default constructor
public Player(String name) {
this.name = name;
this.score = 0;
}
// Accessor methods
public String getName() {
return this.name;
}
public int getScore() {
return this.score;
}
// Mutator methods
public void setName(String name) {
if (name != null && !name.isEmpty()) {
this.name = name;
} else {
throw new IllegalArgumentException("Invalid name");
}
}
public void setScore(int score) {
if (score >= 0) {
this.score = score;
} else {
throw new IllegalArgumentException("Invalid score");
}
}
// Method to return the state of the object
public String toString() {
return "Player: " + this.name + ", Score: " + this.score;
}
}
In this class, the fields are name and score. The name is a String because names are typically represented as text. The score is an int because scores are typically whole numbers.
The class has a default constructor that initializes the name to an empty string and the score to 0, and a non-default constructor that accepts a name and initializes the score to 0.
The accessor methods getName and getScore return the values of the name and score fields, respectively.
The mutator methods setName and setScore set the values of the name and score fields, respectively. They also validate the input values: the name cannot be null or empty, and the score cannot be negative.
The toString method returns the state of the object as a String.
Similar Questions
Program 5 Identify the type of inheritance stated. Write C++ program to implement the scenario given using the identified inheritance. Address the ambiguity in this scenario. Class Base Data members: team_name, N -total_match, int score[N] - an array to store scores for N matches. Member function: void readscore() -to read the scores. Constructor- to initialize the data members Class Derived 1 void display (int score[size])- to print N scores. Class Derived 2 Data member : sum void display (int score[size])- to find the sum N scores. Class C-Derived 3 Member function:void average() -to find average of N scores. Create an object for class D and access the methods. Sample Input 4 90 45 78 89 Scores: 90 45 78 89 Sum:302.00 Average75.50
A person wants to play online games. Get the number of times he needs to play the game. Permit him to play the game till the count is exceeded. Two types of games can be played namely ‘Game 1’ and ‘Game 2’. Create a base class ‘Game’. From class, ‘Game’, two classes ‘Game 1’ and ‘Game 2’ can be derived. Each time the player plays the game a unique integer id is created for each match. Get the details like name, phone number, score obtained for both games. Find the credit obtained and display it along with the report.
Sarah got confused to creating the constructor. Write a Java application to help Sarah to do this.Type(Class)AttributesMethodsResponsibilitiesStudentint studentIdString studentNameString studentAddressString collegeNameInclude the getters and setters method for all the attributes. Student Include a public parametrized constructor of four arguments in the following order - studentId, studentName, studentAddress, and collegeName to initialize the values for the Student objectIf student belongs to other college, give input as 'no/NO' and get college name from the user and create student object with 4-argument constructor to initialize all the values. Student Include a public parametrized constructor of three arguments in the following order - studentId, studentName, studentAddress, and collegeName should be "NIT" to initialize the values for the Student objectIf student belongs to NIT, give input as 'yes/YES' and skip input for the attribute collegeName and create student object with 3-argument constructor to initilze the values for studentId, studentName and studentAddress and collegeName as "NIT". Note: The class and methods should be declared as public and all the attributes should be declared as private. In the UserInterface class, write the main method to test the application.Assume most of the students are from "NIT" college. So user has to give input whether the student is from NIT or not.Instead of Yes / No, if user enters different input then display 'Wrong Input' and get the input again. Note:In the Sample Input / Output provided, the highlighted text in bold corresponds to the input given by the user, and the rest of the text represents the output.Ensure to follow the object-oriented specifications provided in the question description.Ensure to provide the names for classes, attributes, and methods as specified in the question description.Adhere to the code template, if provided.Please do not use System.exit(0) to terminate the program. Sample Input 1:Enter Student's Id:12Enter Student's Name:JohnEnter Student's address:ChennaiWhether the student is from NIT(Yes/No):NOEnter the college name:SVS Sample Output 1:Student id:12Student name:JohnAddress:ChennaiCollege name:SVS Sample Input 2:Enter Student's Id:43Enter Student's Name:TomEnter Student's address:CoimbatoreWhether the student is from NIT(Yes/No):yWrong InputWhether the student is from NIT(Yes/No):yes Sample Output 2:Student id:43Student name:TomAddress:CoimbatoreCollege name:NIT
A person wants to play online games. Get the number of times he needs to play the game. Permit him to play the game till the count is exceeded. Two types of games can be played namely ‘Game 1’ and ‘Game 2’. Create a base class ‘Game’. From class, ‘Game’, two classes ‘Game 1’ and ‘Game 2’ can be derived. Each time the player plays the game a unique integer id is created for each match. Get the details like name, phone number, score obtained for both games. Find the credit obtained and display it along with the report. For each match, a score is get as input from the player and credits are given based on the following criteria.ScoreCredits<1001100 to 1502150 to 2003200 to 2504Only for the ‘Game 2’ get the mail id too. If the mail id is having any ‘( ‘ symbol remove it and format the mail id and print the formatted mail id.
. Design a class for a student who has details, such as id, name and email. The class has method Show() which prints the details of the student. Create one object of the class and initialize the object with details at the time of initialization. Create another object and give the default detail to the object. Perform constructor overloading and method overloading in the class.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.