114k views
5 votes
Write a class called Dragon. A Dragon should have a name, a level, and a boolean variable, canBreatheFire, indicating whether or not the dragon can breathe fire. The class should have getter methods for all of these variables- getName, getLevel, and isFireBreather, respectively. Dragon will alsomeed a constructor, a method to gain experience, and a method to attack. The constructor should take the name as the first argument and the level as the second argument. The constructor should initialize canBreatheFire based on the dragon's level. If the dragon is level 70 or higher, the dragon can breathe fire (meaning the third member variable should be set to true). You should create three getter (accessor) methods called getNameO getLevelO,and isFireBreatherO You should also create a method called attackO. This method does not return anything. If the dragon can breathe fire, it should print >>>999 1 public class Dragon private String name; private int level; private boolean canBreatheFire; 4 7 // Write the constructor here! public String getName(name) 9 10 return name; 12 13 14 - 15 public int getLevel(level) return level; 16 17 /Put getters here 18 19 20 21 221/ String representation of the object 23 24 - 25 26 27 3 28 // Put other methods here public String toStringO return "Dragon+ name +" is at level "+ level; 1 public class DragonTester extends ConsoleProgram 2 4 6 public void runCO // Start here! 7

User Sdz
by
4.4k points

1 Answer

1 vote

Answer:

public class Dragon {

private String name;

private int level;

private boolean canBreatheFire;

public Dragon(String name,int level){

this.name=name;

this.level=level;

if(level>=70) {

this.canBreatheFire=true;

}

}

public String getName() {

return name;

}

public int getLevel() {

return level;

}

public boolean getCanBreatheFire() {

return canBreatheFire;

}

public void attack() {

if(getCanBreatheFire()) {

System.out.println(">>> 999");

}else {

System.out.println("Dragon does not have fire breath.");

}

}

public void gainExperience() {

level += 5;

}

public String toString() {

return "Dragon "+ name + " is at level "+ level;

}

Step-by-step explanation:

The Java program above is a class called Dragon. An object instance of the dragon class has a name and level class variable. The level is used to determine the Boolean value of the canBreathFire class variable. The variables can be retrieved with the getter methods and the level updates by the experience method.

User UmmaGumma
by
4.2k points