2009-01-23 08:04:09 UTC
public class Hero {
public static final int MARSHMALLOW_MAN = 1;
public static final int IRON_GIRL = 2;
public static final int SURFER_DUDE = 3;
public static final int POWER_BOY = 4;
public static final int SCRUNCHIE_WOMAN = 5;
private int type; //Stores one above types
private int health; //0 = dead to 100 = full strength
public Hero (int type)
{
this.type = type;
switch (type)
{
case MARSHMALLOW_MAN:
health = 50;
break;
case IRON_GIRL:
health = 100;
break;
case SURFER_DUDE:
health = 75;
break;
case POWER_BOY:
health = 100;
break;
case SCRUNCHIE_WOMAN:
health = 50;
break;
}
}
int getType ()
{
return type;
}
int getHealth ()
{
return health;
}
public String toString ()
{
return String.format("type %d health %d", type, health);
}
public static void main(String[] args)
{
}
}
and
public class HeroPack {
private Hero[] heroes;
private int packNumber;
HeroPack (int packNumber, int nHeroes)
{
this.packNumber = packNumber;
heroes = new Hero[nHeroes];
for (int i = 0; i < heroes.length; i++)
heroes[i] = null;
}
boolean addHero (Hero hero)
{
for (int i = 0; i < heroes.length; i++)
{
if (heroes[i] == null)
{
heroes[i] = hero;
return true;
}
}
return false;
}
public String toString()
{
String s = "";
for(int i = 0 ; i < heroes.length; i++)
{
s += heroes[i].toString()+"\n";
}
return s;
}
public static void main(String[] args)
{
HeroPack hp1 = new HeroPack(1, 5);
HeroPack hp2 = new HeroPack(2, 5);
for (int i = 0; i < 5; i++)
{
int randomNumber = (int) (Math.random() * 5) + 1;
Hero hero = new Hero(randomNumber);
hp1.addHero(hero);
}
//System.out.println(hp1);
}
}
I have to do the following:
Add the following method:
public Boolean HeroPack.equals (HeroPack otherObject)
Safely determine if 2 HeroPack objects are equal. Equality is defined as the same number and types of heroes. Note that the heroes do not have to be in the array in the same order to be equal.
How do I do this?