((Java)) Imagine you are writing a computer game that involves a hero who encoun
ID: 3732148 • Letter: #
Question
((Java))
Imagine 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. Similarly, 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 loses when the monster hits the hero. The age and name of the monster do not affect the outcome of battles between the monster and the hero.
Can you make the necessary changes to the Monster class (supplied with this lab) 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.
Save your program in the MonsterClient.java file.
---------------------------------------MonsterClient File below -----------------------------------------
/**
* 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
MonsterClient.java
public class MonsterClient implements Comparable<MonsterClient>{
private int hitPoints, strength, age;
private String name;
public MonsterClient( 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();
}
/**
* This method is used to sort the monsters depending on the hitpoints
*/
@Override
public int compareTo(MonsterClient o2) {
// TODO Auto-generated method stub
MonsterClient o1 = this;
if(o1.getHitPoints() == o2.getHitPoints())
return 0;
else if(o1.getHitPoints() > o2.getHitPoints())
return 1;
else
return -1;
}
}
MonsterTest.java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class MonsterTest {
public static void main(String[] args) {
MonsterClient a = new MonsterClient(100, 1550, 21, "Dog");
MonsterClient b = new MonsterClient(600, 150, 22, "Wolf");
MonsterClient c = new MonsterClient(110, 900, 16, "Fox");
MonsterClient d = new MonsterClient(550, 2550, 40, "Tiger");
MonsterClient e = new MonsterClient(340, 850, 13, "Lion");
MonsterClient f = new MonsterClient(250, 350, 7, "Leapord");
List<MonsterClient> monstersList = new ArrayList<>();
monstersList.add(a);
monstersList.add(b);
monstersList.add(c);
monstersList.add(d);
monstersList.add(e);
monstersList.add(f);
Collections.sort(monstersList); //Sorts the Monsters list in order of hitpoints using compareTo
for(MonsterClient mon : monstersList) {
System.out.println(mon);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.