Say that you are writing a computer game that involves a hero who encounters var
ID: 3806949 • Letter: S
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 (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.
" I need a java program"
Explanation / Answer
package com;
public class Monster implements Comparable<Monster>{
//name ,age ,strength,hit points
private String name;
private int age;
private int strength;
private int hitpoints;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getStrength() {
return strength;
}
public void setStrength(int strength) {
this.strength = strength;
}
public int getHitpoints() {
return hitpoints;
}
public void setHitpoints(int hitpoints) {
this.hitpoints = hitpoints;
}
public void hitHero(Hero hero){
hero.setHitpoints(hero.getHitpoints()-strength);
}
public Monster() {
// TODO Auto-generated constructor stub
}
//name ,age ,strength,hit points
public Monster(String name,int age,int strength,int hitpoints) {
// TODO Auto-generated constructor stub
this.name=name;
this.age=age;
this.strength=strength;
this.hitpoints=hitpoints;
}
@Override
public int compareTo(Monster monster) {
// TODO Auto-generated method stub
if(this.getHitpoints()>monster.getHitpoints())
return 1; //current monster difficult to kill than supplied monster
if(this.getHitpoints()<monster.getHitpoints())
return -1;
return 0;
}
}
//***************************************************************************************************
package com;
public class Hero {
public Hero() {
// TODO Auto-generated constructor stub
}
//name ,age ,strength,hit points
private String name;
private int age;
private int strength;
private int hitpoints;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getStrength() {
return strength;
}
public void setStrength(int strength) {
this.strength = strength;
}
public int getHitpoints() {
return hitpoints;
}
public void setHitpoints(int hitpoints) {
this.hitpoints = hitpoints;
}
public void incrementStrength()
{
strength++;
}
public void hitMonster(Monster monster){
monster.setHitpoints(monster.getHitpoints()-strength);
}
//name ,age ,strength,hit points
public Hero(String name,int age,int strength,int hitpoints) {
// TODO Auto-generated constructor stub
this.name=name;
this.age=age;
this.strength=strength;
this.hitpoints=hitpoints;
}
}
//****************************************************************************************************************