Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a subclass of the Creature class called the Troll class. A Troll has the

ID: 3702972 • Letter: C

Question

Create a subclass of the Creature class called the Troll class. A Troll has the following attribute in addition to a Creature:

•       happinessLevel a double representing how happy the Troll is.

This attribute is initialized in the Troll's constructor, which you must write.

ANSWER

//Declare the class header





//Declare and initialize variable





//Write the constructor


Write the getHappinessLevel method for returning the Troll’s level of happiness (no parameters).

ANSWER








Write the updatehappinessLevel method that takes a double parameter and returns no value. The method adds the parameter to the Troll’s happiness level if its lifeLevel is strictly greater than zero.

ANSWER













Override the Creature class’s speak method (given below) so that the method returns “Let’s play!” if its happiness level is greater than its lifeLevel, and “I must break you!” otherwise.

public String speak ) f return " ";

Explanation / Answer

//Declare the class header public class Troll extends Creature { //Declare and initialize variable private double happinessLevel; //Write the constructor public Troll() { } // Write the getHappinessLevel method for returning the Troll's level of happiness public double getHappinessLevel() { return happinessLevel; } // Write the updatehappinessLevel method that takes a double parameter and returns no value. public void updatehappinessLevel(double level) { if(getLifeLevel() > 0) { happinessLevel += level; } } // Override the Creature class’s speak method public String speak() { if(happinessLevel > getLifeLevel()) { return "Let's play!"; } else { return "I must break you!"; } } }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote