Question:
Java Program Help, Using Boolean Equals Method?
2009-01-23 08:04:09 UTC
I have two classes to use:

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?
Three answers:
Blackcompe
2009-01-23 09:35:44 UTC
public class Example

{

static class inner

{

String name;

int age;

inner(String s, int n)

{

this.name = s;

this.age = n;

}



public boolean equals(Object o)

{

String otherName = ((inner)o).name;

int otherAge = ((inner)o).age;

if(this.name.equals(otherName)

&& this.age == otherAge)

return true;

else

return false;

}

}

public static void main(String[] args)

{



inner i1 = new inner("Mike", 10);

inner i2 = new inner("Joe", 10);

if(i1.equals(i2))

System.out.println("Equal");

else

System.out.println("Not Equal");



i1 = new inner("John", 9);

i2 = new inner("John", 9);

if(i1.equals(i2))

System.out.println("Equal");

else

System.out.println("Not Equal");

}



}
Germann A
2009-01-23 17:15:00 UTC
I suggest you add another method to HeroPack class - contains(hero)

Guess what it will need to do?

Think how it will need to do it...



Then in your equals() method all you will need to do will be:

check number of heroes in both packs (return false if not equal)

get next hero from THIS pack and use contains(hero) method above to check if it exists in other pack...

Consider the implications of having more than one hero of the same type in any (or both) packs...
SPB
2009-01-23 17:10:21 UTC
Here is one way. What it does is put the source Hero array into a Vector and the target Hero array into a Vector. Then, loop through the source Vector. Look for that Hero in the target Vector. If you find it then remove it. When you are done looping through the source Vector the target Vector must be empty in order for the two Vectors to be "equal". I ensured that both the source and target Vectors contained the same number of elements before starting the loop.



public boolean equals(HeroPack h)

{

Vector source = new Vector();

Vector target = new Vector();



int i = 0;

while ((i < heroes.length) && (heroes[i] != null))

{

source.add(heroes[i]);

i++;

}



Hero[] targetArray = h.getHeroes();

i = 0;

while ((i < targetArray.length) && (targetArray[i] != null))

{

target.add(targetArray[i]);

i++;

}



if (source.size() != target.size()) {

return false;

}



for (i = 0; i < source.size(); i++) {

Hero sourceHero = (Hero)source.elementAt(i);



boolean found = false;

for (int j = 0; (j < target.size()) && (!found); j++)

{

Hero targetHero = (Hero)target.elementAt(j);

if (sourceHero.equals(targetHero))

{

target.removeElementAt(j);

found = true;

}

}

}



return target.size() == 0;

}



In order to do this you must add this to HeroPack:



public Hero[] getHeroes()

{

return heroes;

}



and add this to Hero



public boolean equals(Hero target)

{

return (getType() == target.getType()) && (getHealth() == target.getHealth());

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...