Question

In: Computer Science

Create a class named GameCharacter to define an object as follows: The class should contain class...

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:

  • Use the four-parameter constructor to create an instance of GameCharacter named char1 with values "Joe", "Elf", 8 and 420.
  • Use the two-parameter constructor to create an instance of GameCharacter named char2 with values "Mary", "Wizard".
  • Use a set method to change Mary's health rating to 5.
  • Now use a System.out.println statement(s), including calls to the appropriate char1 and char2 get methods to display the following:

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

Solutions

Expert Solution

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());

    }
}

===============================================================


Related Solutions

Create a custom Exception named IllegalTriangleSideException. Create a class named Triangle. The Triangle class should contain...
Create a custom Exception named IllegalTriangleSideException. Create a class named Triangle. The Triangle class should contain 3 double variables containing the length of each of the triangles three sides. Create a constructor with three parameters to initialize the three sides of the triangle. Add an additional method named checkSides with method header - *boolean checkSides() throws IllegalTriangleSideException *. Write code so that checkSides makes sure that the three sides of the triangle meet the proper criteria for a triangle. It...
Create a custom Exception named IllegalTriangleSideException. Create a class named Triangle. The Triangle class should contain...
Create a custom Exception named IllegalTriangleSideException. Create a class named Triangle. The Triangle class should contain 3 double variables containing the length of each of the triangles three sides. Create a constructor with three parameters to initialize the three sides of the triangle. Add an additional method named checkSides with method header - *boolean checkSides() throws IllegalTriangleSideException *. Write code so that checkSides makes sure that the three sides of the triangle meet the proper criteria for a triangle. It...
Java Create a Project named Chap4b 1. Create a Student class with instance data as follows:...
Java Create a Project named Chap4b 1. Create a Student class with instance data as follows: student id, test1, test2, and test3. 2. Create one constructor with parameter values for all instance data fields. 3. Create getters and setters for all instance data fields. 4. Provide a method called calcAverage that computes and returns the average test score for an object to the driver program. 5. Create a displayInfo method that receives the average from the driver program and displays...
Instructions 1. Create a class named after your favorite object in the whole world. For example,...
Instructions 1. Create a class named after your favorite object in the whole world. For example, if you love pizza, then create a class called Pizza.java. This class should not have a main method (1 point) 2. Create Instance Variables (attributes) (2 point) Create at least 3 private instance fields (attributes) for your class You must use at least 3 different data types for your fields 3. Create getter (accessor) and setter (mutator) methods   Create a getter (accessor) method for...
Create a file named StudentArrayList.java,within the file create a class named StudentArrayList. This class is meant...
Create a file named StudentArrayList.java,within the file create a class named StudentArrayList. This class is meant to mimic the ArrayList data structure. It will hold an ordered list of items. This list should have a variable size, meaning an arbitrary number of items may be added to the list. Most importantly this class should implement the interface SimpleArrayList provided. Feel free to add as many other functions and methods as needed to your class to accomplish this task. In other...
Design a class named Message to represent a sentence or phrase. The class will contain: •...
Design a class named Message to represent a sentence or phrase. The class will contain: • a private string data field to hold the sentence or phrase. • A no-arg constructor with an empty string message. • A constructor that create a message object with the specified string sentence or phrase. • Accessor and mutator (getter/setter) for string data field. • A method named getVowels ( ) that returns the number of vowels in a sentence or phrase. • A...
JAVASCRIPT Create an array of 5 objects named "movies" Each object in the movies array, should...
JAVASCRIPT Create an array of 5 objects named "movies" Each object in the movies array, should have the following properties: Movie Name Director Name Year Released WasSuccessful (this should be a boolean and at least 2 should be false) Genre Loop through all of the objects in Array If the movie is successful, display all the movie information on the page. These movies were a success: Title: Forrest Gump Year Realeased: 1994 Director: Robert Zemeckis Genre: Comedy
In java: -Create a class named Animal
In java: -Create a class named Animal
Create a Class to contain a customer order Create attributes of that class to store Company...
Create a Class to contain a customer order Create attributes of that class to store Company Name, Address and Sales Tax. Create a public property for each of these attributes. Create a class constructor without parameters that initializes the attributes to default values. Create a class constructor with parameters that initializes the attributes to the passed in parameter values. Create a behavior of that class to generate a welcome message that includes the company name. Create a Class to contain...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) id: String   (5 pts) hours: int   (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT