logo Practice-It logo


Related Links:
Author: Roy McElmurry

Suppose you have a pre-existing class Pokemon that represents characters in a game. The class has the following data and behavior:

private String name name of the Pokemon, such as "Pikachu"
private boolean aggressive true if this Pokemon likes to fight
private int attack attack power of the Pokemon
private int defense defense power of the Pokemon
public Pokemon(String name, boolean aggressive, int attack, int defense) makes a Pokemon with the given attributes
public int getAttack() returns the Pokemon's attack power
public int getDefense() returns the Pokemon's defense power
public String getName() returns the Pokemon's name
public boolean isAggressive() returns true if the Pokemon likes to fight, and false if he is defensive

Your task is to modify the class to be Comparable by adding an appropriate compareTo method. All defensive Pokemon are considered to be "less than" aggressive Pokemon. An defensive Pokemon is considered to be "less than" another if it has a lower defense power. An aggressive Pokemon is considered to be "less than" another if it has a lower sum of attack and defense power. If two Pokemon are both aggressive or defensive and have the same defense or attack+defense power, they are considered to be "equal." Your method should not modify any Pokemon object's state. You may assume the parameter passed is not null.

Type your solution here:

This is a partial class problem. Submit code that will become part of an existing Java class as described. You do not need to write the complete class, just the portion described in the problem.

You must log in before you can solve this problem.

Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.