Fi Ona
Fi Ona

Reputation: 21

How to find a specific object from an array and return it?

I'm trying to figure out how to get the oldest car and return my Car object.

 public CarLot() {

    cars = new Car[5];

    cars[0] = new Car(2016, "honda", "civic");
    cars[2] = new Car(2017, "Lamborghini", "aventador");
    cars[3] = new Car(2000, null, "caravan");
    cars[4] = new Car(2010, "dodge", null);

    }

I'm confused here as my hold object isn't a Car object, super confused.

public Car getOldestCar() {

    int i = 0;
    int oldestCar = 3000;
    int hold = 0;

    while(i < cars.length) {
        if(cars[i].getYearManufactured() < oldestCar) {
            oldestCar = cars[i].getYearManufactured();
            hold = i;
        }
    }
    return hold;
    }

Upvotes: 0

Views: 151

Answers (3)

Elias
Elias

Reputation: 155

 public CarLot() {

    cars = new Car[5];

cars[0] = new Car(2016, "honda", "civic");
cars[2] = new Car(2017, "Lamborghini", "aventador");
cars[3] = new Car(2000, null, "caravan");
cars[4] = new Car(2010, "dodge", null);
}

public Car getOldestCar() {

int oldestCar = 3000;
Car returncar = new Car(); // -> you need to wirte a standard constructor: "public Car(){}"

for(Car x : cars) {
    if(x.getYearManufactured() < oldestCar) {
        oldestCar = cars.getYearManufactured();
        returncar = x;
    }
}

   return returncar;
}

Upvotes: 0

Josef Ginerman
Josef Ginerman

Reputation: 1560

Your code is almost right. In your method

public Car getOldestCar() { ... }

your return type is Car, but what you actually return is hold, which is of type int. How do I know i is int? Easy, i is the index which you use in your for iteration, and every iteration you give i a value greater by one.

If you want to return a Car instead of int, there are two options:

  • Declare hold to be of type Car. int hold = 0 should be Car hold = null. Then, when you want to give hold a value inside the iteration, instead of giving it the value of i, give it the value of cars[i], since that is the Car you're looking for.
  • Second option, which would require you to change less code. In your last line, when you return hold, change it to: return cars[hold]. This will work because hold currently holds the index of the wanted car, so cars[hold] will give you the car you want.

Upvotes: 1

Krishna Majgaonkar
Krishna Majgaonkar

Reputation: 1746

There are some missing things in questions or you may say incorrectly implemented. Please find my program below which will return the car object

public class OldestCar {
    public static void main(String[] args) {
        Car[] cars = new Car[5];

        cars[0] = new Car(2016, "honda", "civic");
        cars[1] = new Car(2006, "honda", "city");
        cars[2] = new Car(2017, "Lamborghini", "aventador");
        cars[3] = new Car(2000, null, "caravan");
        cars[4] = new Car(2010, "dodge", null);

        System.out.print(new OldestCar().getOldestCar(cars).getName());

    }
    public Car getOldestCar(Car[] cars) {
        int i = 0;
        int oldestCar = 3000;
        int hold = 0;
        while(i < cars.length) {
            if(cars[i].getYearManufactured() < oldestCar) {
                oldestCar = cars[i].getYearManufactured();
                hold = i;
            }
            i++;
        }
        return cars[hold];
    }
}

class Car{
    int YearManufactured;
    String name;
    String model;


    Car(int YearManufactured, String name, String model){
        this.YearManufactured = YearManufactured;
        this.name = name;
        this.model = model;
    }

    public int getYearManufactured() {
        return YearManufactured;
    }

    public void setYearManufactured(int yearManufactured) {
        YearManufactured = yearManufactured;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }
}

I have created a Car class to solve this question. You can also cross-check with yours. I am printing the name of the oldest car. In the given case, it's null.

Some missing points I observed in question:

  1. An array of cars is of size 5 but cars[1] is missing which will cause NullPointerException
  2. i is not incremented in while loop hence loop will run continuously
  3. getOldestCar should have car array as parameter to calculate oldest car

Hope this will help

Happy coding~

Upvotes: 1

Related Questions