Incompatible types when passing method in another class

305 views Asked by At

I'm getting incompatible type error when I use these methods from another class, I'm supposed to return an array of all the cars whose average horsepower for a given year is within the range specified.

here is my methods from the other class which returns arraylist:

public ArrayList<Lamborghini> getCarsFromThisYear(int year){
    ArrayList<Lamborghini> fromYear = new ArrayList<Lamborghini>();

    for( Lamborghini c : inventory){
        if(c.getModelYear() == year){
            fromYear.add(c);
        }
    }

    if( fromYear.size() == ZERO){
        return new ArrayList<>();
    }
    return fromYear;
}

public Lamborghini[] getCarsWithHorsepowerRange(double lowHP, double highHP){
    int matches = ZERO;

    for (Lamborghini c : inventory){
        if(c.getHorsepower() >= lowHP && c.getHorsepower() <= highHP){
            matches++;
        }
    }
    Lamborghini[] horsepower = new Lamborghini[matches];
    int indexInArray = ZERO;
    for (Lamborghini c : inventory){
        if(c.getHorsepower() >= lowHP && c.getHorsepower() <= highHP){
            horsepower[indexInArray] = c;
            indexInArray++;
        }
    }
    return horsepower;
}

and here is what im working on, i'm getting error where it calls : c.getCarsFromThisYear(modelYear) and (c.getCarsWithHorsepowerRange(lowHP, highHP)

public LamborghiniCarLot[] getAllCarLotsWithAverageHorsepowerInRangeForYear(int modelYear, double lowHP, double highHP){
    int matches = ZERO;

    for(LamborghiniCarLot c : carLots){
        if (c.getCarsFromThisYear(modelYear)){
            if(c.getCarsWithHorsepowerRange(lowHP, highHP)){
                matches++;
            }
        }
    }

    LamborghiniCarLot[] search = new LamborghiniCarLot[matches];
    int index = ZERO;

    for(LamborghiniCarLot c : carLots){
        search[index] = c;
        index++;
    }
}
1

There are 1 answers

3
Armaiti On BEST ANSWER

There are number of issues with the following method:

  public ArrayList<Lamborghini> getCarsFromThisYear(int year){
    ArrayList<Lamborghini> fromYear = new ArrayList<Lamborghini>();

    for( Lamborghini c : inventory){
        if(c.getModelYear() == year){
            fromYear.add(c);
        }
    }

    if( fromYear.size() == ZERO){
        return new ArrayList<>();
    }
    return fromYear;
  }
  • Return List<Lamborghini> instead of ArrayList<Lamborghini>
  • When the size of fromYear list "ZERO" just return the array list you have instantiated in the second line of the method. Or change the ArrayList<>() to ArrayList<Laborghini> in your if statement.

Here is a what I would suggest:

public List<Lamborghini> getCarsFromThisYear(int year){
    List<Lamborghini> fromYear = new ArrayList<Lamborghini>();

    for( Lamborghini c : inventory){
        if(c.getModelYear() == year){
            fromYear.add(c);
        }
    }

    return fromYear;
  }

The other issue is that in if (c.getCarsFromThisYear(modelYear)). The method returns List<Lamborghini> but the if statement expecting boolean value.