18. Aused car lot contains cars various and of Make can he available. The car Th
ID: 3839938 • Letter: 1
Question
Explanation / Answer
The Car class implementation is as follows:
public class Car {
private String Make;
private int Year;
private double Mileage;
private double Cost;
public Car(String mk, int yr, double ml, double cs){
Make = mk;
Year = yr;
Mileage = ml;
Cost = cs;
}
public void setMake(String s){
Make = s;
}
public void setYear(int a){
Year = a;
}
public void setMileage(double a){
Mileage = a;
}
public void setCost(double a){
Cost = a;
}
public String getMake(){
return Make;
}
public int getYear(){
return Year;
}
public double getMileage(){
return Mileage;
}
public double getCost(){
return Cost;
}
public void toString(){
System.out.println(Make + " " + Year + " " + Mileage + " " + Cost);
}
}
public class CarLot {
private static final int NUM_CARS = 100;
private Car[] lot;
private static int numberOfCars = 0; // Current size of the lot.Can not be more that MAX_CARS
public CarLot(){
lot = new Car[MAX_CARS];
}
public void addCar (Car a){
if (numberOfCars == MAX_CARS)
System.out.println("Lot is Full, can not add");
if (numberOfCars < MAX_CARS){
lot[lotsize] = a;
lotsize++;
}
}
public ArrayList<Car> findMatchingCars(int year, String make){
int i;
ArrayList<Car> list = new ArrayList<Car>();
for (i = 0; i<numberOfCars; i++){
if (lot[i].getMake() == make && lot[i].getYear() == year)
list.add(lot[i]);
}
return list;
}
public void printMatchingCars(int year, String make){
int i;
for (i = 0; i<numberOfCars; i++){
if (lot[i].getMake() == make && lot[i].getYear() == year)
lot[i].toString();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.