Monster class: package question2; public abstract class Monster { private int he
ID: 3830038 • Letter: M
Question
Monster class:
package question2;
public abstract class Monster {
private int health;
/**
* Single-argument constructor
* @param health The initial health of the monster.
*/
public Monster(int health) {
this.health = health;
}
/**
* Determine if the monster is dead (health <= 0)
* @return true if monster is dead, otherwise false
*/
public boolean isDead() { return health <= 0; }
/**
* Reduce the Monster's health.
* @param damage amount of health reduction
*/
public void takeDamage(int damage) { health -= damage; }
/**
* Monster's current health
* @return current health value.
*/
public int getHealth() {return health; }
/**
* Abstract method to be implemented by subclasses
* @return the amount of damage dealt to the opponent.
*/
public abstract int dealDamage();
/**
* Abstract method to be implemented by subclasses
* @return Special affliction to be kept track of by the opponent.
*/
public abstract String afflict();
}
Explanation / Answer
public abstract class Monster {
private int health;
/**
* Single-argument constructor
* @param health The initial health of the monster.
*/
public Monster(int health) {
this.health = health;
}
/**
* Determine if the monster is dead (health <= 0)
* @return true if monster is dead, otherwise false
*/
public boolean isDead() { return health <= 0; }
/**
* Reduce the Monster's health.
* @param damage amount of health reduction
*/
public void takeDamage(int damage) { health -= damage; }
/**
* Monster's current health
* @return current health value.
*/
public int getHealth() {return health; }
/**
* Abstract method to be implemented by subclasses
* @return the amount of damage dealt to the opponent.
*/
public abstract int dealDamage();
/**
* Abstract method to be implemented by subclasses
* @return Special affliction to be kept track of by the opponent.
*/
public abstract String afflict();
public String toString(){
return "Helth :"+health;
}
}
//========================================================
import java.util.Random;
public class Goblin extends Monster {
public Goblin(int health) {
super(health);
}
@Override
public int dealDamage() {
Random rand = new Random();
int damage = rand.nextInt(5);
return damage + 10;
}
@Override
public String afflict() {
Random rand = new Random();
int aff = rand.nextInt(100);
if(aff<12) return "scared";
return "";
}
public String toString(){
return "Monster Goblin : "+super.toString()+" ";
}
}
//========================================================
import java.util.Random;
public class Orc extends Monster {
public Orc(int health) {
super(health);
}
@Override
public int dealDamage() {
Random rand = new Random();
int damage = rand.nextInt(6);
return damage + 9;
}
@Override
public String afflict() {
Random rand = new Random();
int aff = rand.nextInt(100);
if(aff<39) return "scared";
return "";
}
public String toString(){
return "Monster Orc : "+super.toString()+" ";
}
}
//========================================================
import java.util.Random;
public class Sauron extends Monster {
public Sauron(int health) {
super(health);
}
@Override
public int dealDamage() {
Random rand = new Random();
int damage = rand.nextInt(8);
return damage + 12;
}
@Override
public String afflict() {
Random rand = new Random();
int aff = rand.nextInt(100);
if(aff<50) return "scared";
return "";
}
public String toString(){
return "Monster Sauron : "+super.toString()+" ";
}
}
//========================================================
public class MonsterTest {
public static void main(String[] args) {
Monster[] monster = new Monster[5];
monster[0] = new Goblin(10);
monster[1] = new Goblin(12);
monster[2] = new Orc(14);
monster[3] = new Orc(17);
monster[4] = new Sauron(33);
for(int i=0;i<5;i++){
System.out.println(monster[i]);
}
monster[1].takeDamage(4);
}
}