tomatofighter
tomatofighter

Reputation: 69

How to write a method that returns an object and an integer?

Working on a project where I making an AI that can successfully play pac-man. I have a method that gets a specific ghost and the distance from pac-man but I can only return the specific ghost object or the distance from it to pac-man, when I need both values to proceed. My current solution basically has two methods that are exactly the same but with different names and different returns, one an object and one an int.

Ex.

public Ghost closestDefender1()
{
code

return Ghost;
}

public int closestDefender2()
{
code

return int;
}

Again the code in both methods is exactly the same.

Is there a more efficient way?

Upvotes: 1

Views: 1538

Answers (7)

tomatofighter
tomatofighter

Reputation: 69

I fixed my problem by making my method "public static Object[]", i made my method return an object array and initialized [0] to be the ghost object, and [1] to be an integer

Upvotes: 0

SantiBailors
SantiBailors

Reputation: 1634

If you cannot have your data class to return more than one value from a method, you can return an array or a list or a map, containing Objects that you then cast to what you know they are.

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Test {

    public static void main(String[] args) {

        Map<Integer, Object> myResult = myMethod();

        Integer myInteger = (Integer) myResult.get(1);

        System.out.println("1st value: " + myInteger);

        Date myDate = (Date) myResult.get(2);

        System.out.println("2nd value: " + myDate);

        String myString = (String) myResult.get(3);

        System.out.println("3rd value: " + myString);

        List<Object> myResultB = myMethodB();

        myInteger = (Integer) myResultB.get(0);

        System.out.println("1st value: " + myInteger);

        myDate = (Date) myResultB.get(1);

        System.out.println("2nd value: " + myDate);

        myString = (String) myResultB.get(2);

        System.out.println("3rd value: " + myString);

        Object[] myResultC = myMethodC();

        myInteger = (Integer) myResultC[0];

        System.out.println("1st value: " + myInteger);

        myDate = (Date) myResultC[1];

        System.out.println("2nd value: " + myDate);

        myString = (String) myResultC[2];

        System.out.println("3rd value: " + myString);
    }

    static Map<Integer, Object> myMethod() {

        Map<Integer, Object> result = new HashMap<>();

        result.put(1, Integer.valueOf(100));

        result.put(2, new Date());

        result.put(3, "Test");

        return result;
    }

    static List<Object> myMethodB() {

        List<Object> result = new ArrayList<>();

        result.add(Integer.valueOf(100));

        result.add(new Date());

        result.add("Test");

        return result;
    }

    static Object[] myMethodC() {

        Object[] result = new Object[3];

        result[0] = Integer.valueOf(100);

        result[1] = new Date();

        result[2] = "Test";

        return result;
    }

}

Upvotes: 0

Ahmed Sayed
Ahmed Sayed

Reputation: 1554

You could create your own Class that extends the Number class, and implements an interface.

public class NumericObject extends java.lang.Number implements AnInterface {

}

You can now use it either as a Number or as a AnInterface

public NumericObject closestDefender() {

}

// all are fine
Number closestDefender = closestDefender();
AnInterface closestDefender = closestDefender();
NumericObject closestDefender = closestDefender();

Upvotes: 0

WJS
WJS

Reputation: 40034

You can do it like this. Just declare a special class to return both the Object and the int. By using generics you can support any type of object that the method is capable of processing.

Either value will be available for appropriate access.

    public <T> MyObject<T> method() {
           // do something
          T ob = null;  // or some object value
          int x = 10;
          return new MyObject<>(ob, x);
    }

    class MyObject<T> {
         public T object;
         public int v;
         public MyObject(T o, int v) {
             this.v = v;
             this.object = o;
         }
    }

Upvotes: 0

c.abate
c.abate

Reputation: 442

I assume your Ghost object holds some coordinates for its position, and I assume you use these coordinates to calculate the distance between Pac-Man and the Ghost...

So maybe make a function for getting the closest ghost. Then make a function for calculating the distance from Pac-Man to any arbitrary Ghost.

Something like this :

public Ghost getClosestGhost() {
    // Find Closest Ghost...
    return closestGhost;
}

public int getDistanceFromGhost(Ghost g) {
    // Calculate Distance from g using its coordinates.
    return distance;
}

To use this code, we simply need to run the following...

Ghost closest = getClosestGhost();
int distanceFromClosest = getDistanceFromGhost(closest);

With this solution, no code should be repeated. You could even use getDistanceFromGhost in your getClosestGhost function.

Upvotes: 0

Sarangan
Sarangan

Reputation: 967

You can't have a single method to return two types of objects. But instead of having redundant code in two places, Create a method to do the processing.

public Ghost closestDefender1()
{
    somefunction();
    return Ghost;
}

public int closestDefender2()
{
    somefunction();
    return int;
}

Here somefunction() will do the necessary processing and let the methods to decide the return type.

Upvotes: 0

CJW
CJW

Reputation: 342

In pure OOP you can't really set a value because you initialise some values and then call a method to calculate something with those values. So think about letting the Ghost object be able to calculate the value of the integer, if you have to keep to code that modifies the integer in the closestDefender function then consider setting the integer into the Ghost object and then only return that so you don't need to have two functions with exactly the same code but returning different things.

Upvotes: 1

Related Questions