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++;
}
}
There are number of issues with the following method:
List<Lamborghini>
instead ofArrayList<Lamborghini>
ArrayList<>()
toArrayList<Laborghini>
in yourif
statement.Here is a what I would suggest:
The other issue is that in
if (c.getCarsFromThisYear(modelYear))
. The method returnsList<Lamborghini>
but the if statement expectingboolean
value.