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

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();
  

  
}

(Goblin,java, Orc java, Sauron java, Monter Tester,java -30 marks) Consider the abstract Monster class that is given to you in the project file. This class defines the essential characteristics of Monsters that might be used in a game. Your job is to create three specific Monster classes as described below and to follow the standard rules of inheritance in doing so (override methods that need to be overridden, implement methods that need to be implemented, do NOT override or implement methods that are adequately implemented in the superclass, etc etc Goblin: A goblin starts off with between 10 and 14 health. They aren't very healthy and they do only between 7 and 100 damage per turn. They have an affliction called "chewed" that will take an extra amount of damage from the hero at the end of his turn (see below). There is only a 12% chance that a goblin will inflict "chewed" so 88% of the time you return nothing (empty string) and return "chewed" otherwise. Orc: Orcs are a bit tougher so they start off with health between 12 and 18. They also do a bit more damage between 9 and 15 per turn. They have an affliction called "scared" that they inflict 39% of the time Sauron: Sauron is kind of a bad guy he means well, but still... Thanks to the recent finding of the "One Ring" his starting health is between 28 and 36 and he does a damaging 12 to 20 per turn. There is a 50% chance per turn that the hero will be "seen" which will do extra damage In addition to the health data field that is inherited from the superclass, each Monster subclass has an "affliction" data field to make it easier to keep track of. Make sure that each Monster has a toString0 method for easy display Afflictions: If the hero is "chewed" he takes 3 extra damage that round. If the hero is "scared" he does half damage this round. If the hero is "seen" he does no damage this round. There are two ways to write a tester for your program

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);
      
   }

}