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

1. Create a class called car. A has instance parameters, se and getters for \"ye

ID: 3576414 • Letter: 1

Question

1. Create a class called car. A has instance parameters, se and getters for "year variables, constructor make and (e.g. 2016 manufactured" and "car Honda Civic) 2. Create a class called Caruot which has an array of 5 car references. There are two constructors: (a) One constructor takes no parameters and simply populates the array with these cars: cars [oj 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); (b)The other constructor takes one parameter: a Car, and puts into the array as the first (and only) element. 3. Create a method public void addCar(Car car) which adds the new Car to the first empty element in the array; ifthere is no empty element just System out println("no room") instead. 4. Create a method public Car getCar(int index) which returns a reference to the car at the specified array index; if the index is not valid (i.e. not 0 to 4) return null instead. 5. Create a method public Car getoldestCar() which returns a reference to the oldest car in the array. 6. create a method public int getNewestCar() which returns the year in which the newest car in the array was manufactured.

Explanation / Answer

Please find the required solution:

public class Car {

   private int yearOfManufactured;
   private String carMake;
   private String carModel;


   public Car(int yearOfManufactured, String carMake, String carModel) {
       super();
       this.yearOfManufactured = yearOfManufactured;
       this.carMake = carMake;
       this.carModel = carModel;
   }


   public int getYearOfManufactured() {
       return yearOfManufactured;
   }


   public void setYearOfManufactured(int yearOfManufactured) {
       this.yearOfManufactured = yearOfManufactured;
   }


   public String getCarMake() {
       return carMake;
   }


   public void setCarMake(String carMake) {
       this.carMake = carMake;
   }


   public String getCarModel() {
       return carModel;
   }


   public void setCarModel(String carModel) {
       this.carModel = carModel;
   }

}

public class CarLot {

   Car[] cars = new Car[5];


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


   CarLot(Car car) {
       cars[0] = car;
   }


   public void addCar(Car car) {
       for (int i = 0; i < cars.length; i++) {
           if (cars[i] == null) {
               cars[i] = car;
               return;
           }
       }
       System.out.println("no room");
   }


   public Car getCar(int index) {
       if (index < 0 || index > 4)
           return null;
       return cars[index];
   }


   public Car getOldestCar() {
       Car car = null;
       int highest = 0;
       for (int i = 0; i < cars.length; i++) {
           if (cars[i] == null)
               continue;
           else {
               if (highest > cars[i].getYearOfManufactured()) {
                   car = cars[i];
                   highest = cars[i].getYearOfManufactured();
               }
           }
       }
       return car;
   }


   public int getNewestCar() {
       int highest = 0;
       for (int i = 0; i < cars.length; i++) {
           if (cars[i] == null)
               continue;
           else {
               if (highest < cars[i].getYearOfManufactured()) {
                   highest = cars[i].getYearOfManufactured();
               }
           }
       }
       return highest;
   }
}