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

Please do the following question using Java. Also please separate these into the

ID: 3851896 • Letter: P

Question

Please do the following question using Java. Also please separate these into the separate classes (use Inheritance [extends...]): Vampire.java, Zombie.java, GeneralZod.java and finally MonsterTester.java. You are also given the following abstract Monster.java class:

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

}

Given those, you have the following questions:

And for the tester:

2. A) (Vampire.java, Zombie.java, GeneralZod.java, MonterTester,java 25 marks) (15 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 jobs 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.) Vampire: A vampire starts off with between 8 and 12 health. They aren't very healthy and they do only between 6 and 9 damage per turn. Zombie: Zombies are a bit tougher so they start off with health between 11 and 16. They also do a bit more damage - between 8 and 14 per turn. General Zod: General Zod is kind of a bad guy- he means well, but still... Thanks to the Earth's yellow sun his starting health is between 25 and 30 and he does a damaging 12 to 20 per turn. Make sure that each Monster has a toString) method for easy display.

Explanation / Answer

Vampire.java

import java.util.Random;
public class Vampire extends Monster{
   public Vampire(int health){
       super(health);
   }
  
   //attacks with some random amount(between 6 and 9) of damage to opponent
   public int dealDamage(){
       Random r = new Random();
       return 6+r.nextInt(4);
   }
   //I dont understand what this string to be returned
   public String afflict(){
       return "";
   }
   public String toString(){
       return "Vampire";
   }
}


Zombie.java

import java.util.Random;
public class Zombie extends Monster{
   public Zombie(int health){
       super(health);
   }
  
   //attacks with some random amount(between 8 and 14) of damage to opponent
   public int dealDamage(){
       Random r = new Random();
       return 8+r.nextInt(7);
   }
   //I dont understand what this string to be returned
   public String afflict(){
       return "";
   }
   public String toString(){
       return "Zombie";
   }
}


GeneralZod.java

import java.util.Random;
public class GeneralZod extends Monster{
   public GeneralZod(int health){
       super(health);
   }
  
   //attacks with some random amount(between 12 and 20) of damage to opponent
   public int dealDamage(){
       Random r = new Random();
       return 12+r.nextInt(9);
   }
   //I dont understand what this string to be returned
   public String afflict(){
       return "";
   }
   public String toString(){
       return "GeneralZod";
   }
}

MonsterTester.java

import java.util.Random;
import java.util.Scanner;
public class MonsterTester{
   public static void main(String a[]){
       Random r = new Random();
       //creating random monsters by generating random numbers
       //each number represends unique monster
       //0-Vampire       1-Zombine       2-GeneralZod
       //we want to create 5 monsters array
       Monster monsters[] = new Monster[5];
      
       System.out.println("All the monsters randomly created are: ");
       for(int i=0;i<5;i++){
           int randomIndex = r.nextInt(3);
           //we should have GeneralZod as our last monster in our monsters array.right?(thats mentioned in question)
           if(i==4)
           randomIndex = 2;
           switch(randomIndex){
               case 0:
               //so, we need to create a Vampire
               //generate it's health should be between 8 and 12
               int vampireHealth = 8+r.nextInt(5);
               monsters[i] = new Vampire(vampireHealth);
               break;
               case 1:
               //so, we need to create a Zombie
               //generate it's health should be between 11 and 16
               int zombieHealth = 11+r.nextInt(6);
               monsters[i] = new Zombie(zombieHealth);
               break;
               case 2:
               //so, we need to create a GeneralZod
               //generate it's health should be between 25 and 30
               int generalZodHealth = 25+r.nextInt(6);
               monsters[i] = new GeneralZod(generalZodHealth);
               break;
           }
           System.out.println(" "+monsters[i]);
           System.out.println("I got Health:"+monsters[i].getHealth());
       }
      
       //its hero time
       //Get Hero name from console
       Scanner sc = new Scanner(System.in);
       System.out.println(" Enter Hero Name:");
       String heroName = sc.nextLine();
       //assuming that hero's health is between 60 and 80
       int heroHealth = 60+r.nextInt(21);
       System.out.println(heroName+" got Health "+heroHealth+" ");
      
       //Lets start war!!!
       System.out.println("Let us start WAR!!! ");
       //first chance is always given to hero to attack monsters
      
       //isHeroAlreadyAttacked variable used to decide to find whose turn to attack
       boolean isHeroAlreadyAttacked = false;
      
      
       int indexOfMonster = 0;
       while(heroHealth>0){
           Monster monster = monsters[indexOfMonster];
           if(isHeroAlreadyAttacked){
               //turn of monster to attack hero
               int damage = monster.dealDamage();
               heroHealth-=damage;
               isHeroAlreadyAttacked = false;
               System.out.println(monster+" damaged Hero "+heroName+" with "+damage);
           }
           else{
               //turn of hero to attack monster
               //assuming that Hero can damage monster with a value between 10 to 15
               int damage = 10+r.nextInt(6);
               monster.takeDamage(damage);
               isHeroAlreadyAttacked = true;
               System.out.println(heroName+" damaged "+monster+" with "+damage);
               if(monster.isDead()){
                   System.out.println(monster+" is Dead");
                   indexOfMonster++;
               }
               if(indexOfMonster>=5){
                   System.out.println("All monsters are died");
                   break;
               }
           }
       }
       if(heroHealth>0){
           System.out.println(" Wow!!!Finally After a long Hard Time HERO won!!!!");
       }
       else{
           System.out.println(" "+monsters[indexOfMonster]+" has Won this Game ");
       }
   }
}