In: Computer Science
Create a class named GameCharacter to define an object as follows:
The class should contain class variables for charName (a string to store the character's name), charType (a string to store the character's type), charHealth (an int to store the character's health rating), and charScore (an int to store the character's current score).
Provide a constructor with parameters for the name, type, health and score and include code to assign the received values to the four class variables.
Provide a constructor with parameters for the name and type only. Include code to assign these to the charName and charType class variables and assign 0 to the charHealth and charScore class variables.
Provide set and get methods for each class variable.
Now create an application named GameScene as follows:
Joe the Elf is standing at a crossroads. Suddenly there is a flash of light and Mary the Wizard appears!
Joe's health rating is 8
Joe's current score is 420
Mary's health rating is 5
Mary's current score is 0
Thanks for the question. Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks! =========================================================================== public class GameCharacter { private String charName; private String charType; private int charHealth; private int charScore; //Provide a constructor with parameters for the name, type, health and score public GameCharacter(String charName, String charType, int charHealth, int charScore) { this.charName = charName; this.charType = charType; this.charHealth = charHealth; this.charScore = charScore; } //Provide a constructor with parameters for the name and type only. public GameCharacter(String charName, String charType) { this.charName = charName; this.charType = charType; charHealth = 0; charScore = 0; } //Provide set and get methods for each class variable. public String getCharName() { return charName; } public void setCharName(String charName) { this.charName = charName; } //Provide set and get methods for each class variable. public String getCharType() { return charType; } public void setCharType(String charType) { this.charType = charType; } //Provide set and get methods for each class variable. public int getCharHealth() { return charHealth; } public void setCharHealth(int charHealth) { this.charHealth = charHealth; } //Provide set and get methods for each class variable. public int getCharScore() { return charScore; } public void setCharScore(int charScore) { this.charScore = charScore; } }
===============================================================
public class GameScene { public static void main(String[] args) { GameCharacter char1 = new GameCharacter("Joe", "Elf", 8, 420); GameCharacter char2 = new GameCharacter("Mary", "Wizard"); //Use a set method to change Mary's health rating to 5. char2.setCharHealth(5); System.out.println(char1.getCharName() + " the " + char1.getCharType() + " is standing at a crossroads.\n" + "Suddenly there is a flash of light and " + char2.getCharName() + " the " + char2.getCharType() + " appears!"); System.out.println(char1.getCharName() + "\'s health rating is " + char1.getCharHealth()); System.out.println(char1.getCharName() + "\'s health rating is " + char1.getCharScore()); System.out.println(char2.getCharName() + "\'s health rating is " + char2.getCharHealth()); System.out.println(char2.getCharName() + "\'s health rating is " + char2.getCharScore()); } }
===============================================================