A game app involves players modeled as creatures by the Creature class: public c
ID: 647917 • Letter: A
Question
A game app involves players modeled as creatures by the Creature class:
public class Creature{
private String name;
private int lifeLevel;
public Creature(String name, int level){
this.name=name;
lifeLevel=level;
}
public String getName(){
return name;
}
public int getLifeLevel(){
return lifeLevel;
}
public void changeLifeLevel(int amt){
lifeLevel+=amt;
}
public String toString(){
return(name + " " + lifeLevel);
}
}
Write a method for the Creature class called showTheDead, which takes an array ofCreature objects as a parameter, and which does not return a value but prints to the screen the names of all creatures that are dead. A Creature is dead if its life level is less than or equal to zero.
Enter your code for the showTheDead method below.
Explanation / Answer
public void showTheDead(Creature[] creatures)
{
for(int i=0;i<creatures.length;i++)
{
if(creatures[i].lifeLevel<=0)
{
System.out.println(creatures[i].getName());
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.