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...
Question: In a package named "oop" create a Scala class named "Team" and a Scala object...
Question: In a package named "oop" create a Scala class named "Team" and a Scala object named "Referee". Team will have: • State values of type Int representing the strength of the team's offense and defense with a constructor to set these values. The parameters for the constructor should be offense then defense • A third state variable named "score" of type Int that is not in the constructor, is declared as a var, and is initialized to 0 Referee...
Java Q1: Create a class named Triangle, the class must contain: Private data fields base and...
Java Q1: Create a class named Triangle, the class must contain: Private data fields base and height with setter and getter methods. A constructor that sets the values of base and height. A method named toString() that prints the values of base and height. A method named area() that calculates and prints the area of a triangle. Draw the UML diagram for the class. Implement the class. Q2: Write a Java program that creates a two-dimensional array of type integer...
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...
Define a class to represent a Temperature. Your class should contain Instance variable int fahrenheit A...
Define a class to represent a Temperature. Your class should contain Instance variable int fahrenheit A parameterized constructor; Member method to calculate the equivalent temperature in Celsius unit toCelsius() with decimal value. double toCelsius() Conversion formula: celsius= (fahrenheit - 32) * 5/9 Method definition to override the equals() method boolean equals(Object obj)
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...
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...
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...
Define and implement class Course. This class should contain the following fields: course name, course description,...
Define and implement class Course. This class should contain the following fields: course name, course description, department, time the course starts, weekday the course is held on (for simplicity, let us assume the course only meets once a week). This class should contain getters and setters for all its attributes. This class also needs at least one constructor. Save this class and its definition into a file named Course.java. Define and implement class Student. This class should contain the following...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT