the following Monster class: package comparators; public class Monster { private String name; private int hitPoints; private int id; public Monster(String name, int hitPoints, int id) { this.name = name; this.id = id; this.hitPoints = hitPoints; } public String getName() {return this.name;} public int getHitPoints() {return this.hitPoints; } public int getId() {return this.id;} @Override public boolean equals(Object other) { if (other == this) return true; if (other.getClass() != this.getClass()) return false; if (this.name.equalsIgnoreCase(((Monster)other).name) && (this.id == ((Monster)other).id)){ return true; } return false; } } (a) Create a Comparator called MonsterComparator that will allow us to sort a list of monsters in ascending order, by their names (b) Create a second comparator, called MonsterHPComparator that will allow us to sort a list of monsters from least to greatest with respect to their hitPoints power
Given the following Monster class:
package comparators;
public class Monster {
private String name;
private int hitPoints;
private int id;
public Monster(String name, int hitPoints, int id) {
this.name = name;
this.id = id;
this.hitPoints = hitPoints;
}
public String getName() {return this.name;}
public int getHitPoints() {return this.hitPoints; }
public int getId() {return this.id;}
@Override
public boolean equals(Object other) {
if (other == this) return true;
if (other.getClass() != this.getClass()) return false;
if (this.name.equalsIgnoreCase(((Monster)other).name)
&& (this.id == ((Monster)other).id)){
return true;
}
return false;
}
}
(a) Create a Comparator called MonsterComparator that will allow us to sort a list of monsters in ascending order, by their names
(b) Create a second comparator, called MonsterHPComparator that will allow us to sort a list of monsters from least to greatest with respect to their hitPoints power.
Trending now
This is a popular solution!
Step by step
Solved in 2 steps