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

Java Question Say that you are writing a computer game that involves a hero who

ID: 3805994 • Letter: J

Question

Java Question

Say that you are writing a computer game that involves a hero who encounters various monsters. Monsters have several characteristics: hit points, strength, age, and name (the class Monster.java is supplied). The number of hit points a monster has is how far it is from death. A monster with a large number of hit points is hard to kill. The hero also has hit points.

When the hit points of the hero reach zero, the game is over. The strength of a monster affects how many hit points the hero looses when the monster hits the hero. The age and name of the monster do not affect the outcome of battles between the monster and a hero.

Can you make the necessary changes to the Monster class and write a tester (client) class with main method that will present the hero with monsters in increasing order of difficulty.

Hints: Monster needs a compareTo() method.

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/


/**
* Say that you are writing a computer game that involves a hero who encounters
* various monsters. Monsters have several characteristics: hit points, strength,
* age, and name. The number of hit points a monster has is how far it is from
* death. A monster with a large number of hit points is hard to kill. The hero
* also has hit points.

* When the hit points of the hero reach zero, the game is over. The strength of
* a monster affects how many hit points the hero looses when the monster hits
* the hero. The age and name of the monster do not affect the outcome of battles
* between the monster and a hero.
*
*

Can you make the necessary changes to the Monster class and write a tester
* class with main method that will present the hero with monsters in
* increasing order of difficulty.
* Hints: Monster needs a compareTo() method.
*/
public class Monster {
  
private int hitPoints, strength, age;
private String name;
  
public Monster( int hp, int str, int age, String nm )
{
hitPoints = hp; strength = str; this.age = age; name = nm;
}
  
public int getHitPoints() { return hitPoints; }
  
public int getStrength() { return strength; }
  
public int getAge() { return age; }
  
public String getName() { return name; }
  
@Override
public String toString()
{
return "HP: " + getHitPoints() + " Str: " + getStrength() + " " + getName();
}
  
}

Explanation / Answer

package monster;

public class Monster implements Comparable<Monster>{

   private int hitPoints, strength, age;
   private String name;

   public Monster(int hp, int str, int age, String nm) {
       hitPoints = hp;
       strength = str;
       this.age = age;
       name = nm;
   }

   public int getHitPoints() {
       return hitPoints;
   }

   public int getStrength() {
       return strength;
   }

   public int getAge() {
       return age;
   }

   public String getName() {
       return name;
   }

   @Override
   public String toString() {
       return "HP: " + getHitPoints() + " Str: " + getStrength() + " " + getName();
   }

   @Override
   public int compareTo(Monster monster) {
       if(this.hitPoints > monster.hitPoints){
           return 1;
       }
       else if(this.hitPoints < monster.hitPoints){
           return -1;
       }
       else{
           if(this.strength > monster.strength){
               return 1;
           }
           else if(this.strength < monster.strength){
               return -1;
           }
       }
       return 0;
   }

}

Tester.java

package monster;

import java.util.ArrayList;
import java.util.Collections;

public class Tester {

   public Tester() {
      
   }

   public static void main(String[] args) {
      
       Monster m1 = new Monster(500,500,50,"m1");
       Monster m2 = new Monster(400,400,50,"m2");
       Monster m3 = new Monster(300,300,50,"m3");
      
       Monster hero = new Monster(700,700,50,"hero");
       ArrayList<Monster> monsters = new ArrayList<>(3);
       monsters.add(m1);
       monsters.add(m2);
       monsters.add(m3);
      
       Collections.sort(monsters);
      
       for(int i=0;i<3;i++){
           System.out.println(hero.getName()+" is fighting with "+monsters.get(i).getName());
       }

   }

}

Output:

hero is fighting with m3
hero is fighting with m2
hero is fighting with m1

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote