简体   繁体   中英

How do you pass a class into a method and return a boolean based on a parameter of the class?

I am having trouble with a method taking in two class objects and returning a boolean value. I understand i need a return statement in each condition of my if statement, one returning false and another returning true. the issue im facing is each object has a (i, j) coordinate on a 10x10 multi-dimensional array grid. When both objects have the same (i, j) the game should end.

This is the method i am having trouble with. It takes in Creature c1 and Creature c2 and returns a boolean. What would i put in the method to make this work?

public static boolean sameSquare(Creature c1, Creature c2) {
    // if c1 and c2 have identical (i, j) coordinates, return true
    // else return false
    c1 = Creature(human);
    c2 = Creature(vampire);
    if (c1 == c2) {
      return true;
      System.out.println("you bit the human");
    }else {
      return false;
    }

   }

The call to this method looks like this

sameSquare(human, vampire);

The creation of the objects human and vampire are as follows:

System.out.print("Enter (i, j) for vampire: ");
int newI = input.nextInt();
int newJ = input.nextInt();
Creature vampire = new Creature('V', newI, newJ);


System.out.print("Enter (i, j) for human: ");
int humanI = input.nextInt();
int humanJ = input.nextInt();
Creature human = new Creature('H', humanI, humanJ);

You can override the isEqual method in your Creature class to customize how classes are compared.

In this case it looks like you'll want to compare each class's I and J positions.

However, it's a bit odd to consider these Creatures "equal" just because of their position. They're still different!

Perhaps define a method in your creature class like:

public Boolean hasSamePositionAsCreature( Creature other )
{ 
   if( other.I == this.I && other.J == this.J )
   {
       return true
   }

   return false
}

When you use == on two objects your comparing references not values.

c1 = Creature(human);
c2 = Creature(vampire);
c1 == c2 // Will always be false!!

You need to override the equals method in the Creature class.

@Override
public boolean equals(Object o) {
    // You might also want to verify that o is an instance of Creature
    if (! (o instanceof Creature))
        return false;
    Creature other= (Creature) o;

    // Write logic to check if they are in the same cell
    if (this.i == other.i && this.j == other.j)
        return true;
    else
        return false;
}

You cant use == for object comparison, this will check address of objects instead of their contents.

In order to compare two objects you need to override equals method in your class and then do proper checking and return boolean accordingly. In your case equals method would be :

public boolean equals(Creature obj){
    if(obj!=null &&(this.i==obj.i && this.j==obj.j))
        return true
    else 
        return false;
}

Now you can compare two objects as shown below :

c1.equals(c2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM