Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

18. A used car t wants contains cars of various makes and models. The size of a

ID: 3839986 • Letter: 1

Question

18. A used car t wants contains cars of various makes and models. The size of a car lot is fxed. The owner of the car lot each to inventory the cars he has available. For the inventory, he/she needs the following information about car: Make (Toyota, Ford, etc.) Year (1997, 2001, etc.) Mileage (120734.6, 23555.2, etc.) Cost (20000.00, 24500.00, etc.) When a new car is created, the make, the car year, and the mileage are given. The cars are occasionally taken for test rides. A test ride alters the mileage of the car. The price of the car is set by the owner of the car lot. The car dealer will eventually need to print all of the information about each car in the lot. It will also be necessary for the car dealer to request and print a list of all cars with a specified year and make. Implement the Car class. In implementing the class, you should: Choose appropriate variable names, parameter names, and method names. Properly identify access modifiers (private, public) of each method and instance field. Include a toString that will print the information about a car. Consider the CarLot class partially implemented below. public class CarLot{ public CarLot 0H lot new Car(MAX CARS]; //Prints the information about the cars in the car lot. public void addCar (Car aNewCar)...) public void printCarslnLot (00..) Fills the array list with cars in the car Iot that have the same year and make as the parameters passed to the method private ArrayLi findMatchingcars (int year, String make)f..) Prints the year, make, mileage, and price of each car in the lot that has the same year and make as the method parameters public void printMatchingcars (int year, String make)(...)

Explanation / Answer

/**
*
* @author Sam
*/
public class CarLot {
    private static final int MAX_CARS = 100;
    private static int numberOfCars = 0;
    private final Car[] lot;

    public CarLot() {
        lot = new Car[MAX_CARS];
    }
    public void addCar(Car aNewCar) {
        lot[numberOfCars++] = aNewCar;
    }
  
    public void printCarsInLot() {
        for (int i = 0; i < numberOfCars; i++)
            System.out.println(lot[i]);
    }
    private ArrayList<Car> findMatchingCar(int year, String make) {
        ArrayList<Car> cars= new ArrayList<>();
        for (int i = 0; i< numberOfCars; i++)
            if (lot[i].getYear() == year && Objects.equals(make, lot[i].getMake()))
                cars.add(lot[i]);
        return cars;
    }
  
    public void printMAtchingCars(int year, String make) {
        ArrayList<Car> cars = findMatchingCar(year, make);
        for (Car car : cars)
            System.out.println(car);
    }
  
}

class Car {
    private final String make;
    private final int year;
    private double mileage;
    private double price;

    public Car(String make, int year, double mileage) {
        this.make = make;
        this.year = year;
        this.mileage = mileage;
    }

    public String getMake() {
        return make;
    }

    public int getYear() {
        return year;
    }

    public double getMileage() {
        return mileage;
    }

    public void setMileage(double mileage) {
        this.mileage = mileage;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Car{" + "make=" + make + ", year=" + year + ", mileage=" + mileage + ", price=" + price + '}';
    }
  
}


Here you go champ! Answer is ready for you!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote