Homework help! Consider the Creature class: public class Creature { private Stri
ID: 3702979 • Letter: H
Question
Homework help!
Consider the Creature class:
public class Creature {
private String name;
private String weapon;
private double lifeLevel;
public Creature(String name, String weapon) {
this.name = name;
this.weapon = weapon;
lifeLevel=100;
}
public String getName(){return name;}
public String getWeapon(){return weapon;}
public double getLifeLevel(){return lifeLevel;}
public void die(){lifeLevel = 0;}
public boolean isAlive(){return (lifeLevel>0);}
/* Adds the argument: incAmt to the lifeLevel.
* Note: if the lifeLevel is currently 0 it should not change
*/
public void updateLifeLevel(double incAmt){
if(lifeLevel > 0)
lifeLevel += incAmt;
}
public String speak(){
return " ";
}
public String toString() {
return name+", weapon: "+weapon+", life level: "+lifeLevel;
}
}
Question 4.
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
Question 5.
Write the getHappinessLevel method for returning the Troll’s level of happiness (no parameters).
ANSWER
Question 6.
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
Question 7.
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.
ANSWER
Question 8.
Override the Creature class's toString method (given below) so that it returns this String:
"Ragnarita, weapon: Hammer, life level: 300.50, happinessLevel: 375.00"
for a Troll named Ragnarita with a Hammer as a weapon and has life level 300.50 and happiness level 375.00. Your Troll class's toString method MUST use a call to its super class's toString method.
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(String name, String weapon) { super(name, weapon); } // 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!"; } } // Override the Creature class's toString method @Override public String toString() { return super.toString() + ", happinessLevel: " + happinessLevel; } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.