System.out.println
System.out.println

Reputation: 21

How to access an instance through a method, within an array of objects in Java?

I have this simple class that creates a vehicle with three instances (brand, tank capacity and fuel consumption -l/100km-). These are public, and objects can be created with the constructor below:

public class VehicleClass 
{
    String brand;
    int tank;
    double consumption;

    VehicleClass (String brand, int tank, double consumption)
    {
        this.brand = brand;
        this.tank = tank;
        this.consumption = consumption;
    }
}

I want to create an array containing two vehicles, so in the main class, I create these three objects:

public class VehicleMain 
{
    public static void main (String[] args) 
    {           
        VehicleClass car1 = new VehicleClass ("Ford", 40, 6.5);
        VehicleClass car2 = new VehicleClass ("Volkswagen", 50, 5.0);
        VehicleClass cars[] = new VehicleClass[] {car1, car2}; 
    }
}

I want to create a method, that using the array cars, will return an array of the maximum range of both vehicles. This is my approach:

public int[] distances (VehicleClass[] car)
{
    int[] range = new int[car.length];
    for (int i = 0; i < car.length; i++)
    {
        range[i] = (int) ((tank * 100) / consumption);
    }
    return range;
}

Accessing this method in the main class with the import java.util.Arrays:

System.out.println (Arrays.toString (car<number>.distances(cars)));

However, I only get [615, 615] if I put car1, and [1000, 1000] with car2. And my goal is that it will return [615, 1000]. Putting this, gives me an error:

System.out.println (Arrays.toString (cars.distances(cars)));

What am I doing wrong? Is it possible to do what I was thinking? I would really appreciate if someone could clarify me this question. I'm a beginner at OOP.

Upvotes: 0

Views: 199

Answers (1)

xdhmoore
xdhmoore

Reputation: 9876

I think I would do 2 things. Make a distance() function on your Vehicle class that returns the distance for one car, then use a Java map function to operate on your array:

public class Vehicle //renamed from VehicleClass
{
    String brand;
    int tank;
    double consumption;

    //etc, constructor stuff...

    public int distance() {
        return (int) ((tank * 100) / consumption);
    }
}

Then in your main:

public class VehicleMain 
{
    public static void main (String[] args) 
    {           
        VehicleClass car1 = new VehicleClass ("Ford", 40, 6.5);
        VehicleClass car2 = new VehicleClass ("Volkswagen", 50, 5.0);
        VehicleClass cars[] = new VehicleClass[] {car1, car2}; 
        Integer[] results = Stream.of(cars).map(VehicleClass::distance).toArray(Integer[]::new);
        for (int i : results) {
           System.out.println(i);
        }
    }
}

Generally speaking, I think the problem is that you are confusing the individual VehicleClass with the collection of VehicleClass objects. In your distance method you are looping over the passed in cars, but then you are using the same this.tank and this.consumption value from the this instance repeatedly, instead of for example, car[i].tank.

Often you want to separate the the collection logic (getting the distance from everything in the array) from the actual calculation of the distance. The distance calculation has to do with a single vehicle, so it goes in the VehicleClass, while the collection logic goes in the main method. Alternatively, you could also put the "collection logic" in a new VehicleCollection class in a distances method that calls all the distance methods on the contained objects. Or, you could make a static distances method on your VehicleClass that does the same thing.

However, since the only "collection logic" you need is to map all the distances into an array, I think putting it right in the main method makes sense.

Upvotes: 2

Related Questions