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

Create a class called Car 1. One constructor takes no parameters and simply popu

ID: 3813643 • Letter: C

Question

Create a class called Car 1. One constructor takes no parameters and simply populates the array with these cars: cars[0] = 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); 2. The other constructor takes one parameter: a Car and puts into the array as the first and only element. 3. Create a method called public void addCar(Car car) which adds the new car to hte first empty element in the array. 4. Create a method public Car getCar(int index) which returns a reference to the car at the specific array index; if the index is not valid, return null. 5. Create a method public Car getOldestCar() which returns a refernece to the oldest car in the array. 6. Create a public int getNewestCar() which returns the year in which the newest car in the array was manufacturered.

Explanation / Answer

import java.util.ArrayList;

import java.util.Collections;

public class Car{

  

   private ArrayList<Car> lis = new ArrayList<Car>();

   private int year;

   private String car_name;

   private String type;

  

   public Car(){

       System.out.println(this.lis.size());

       this.lis.add(new Car(2016, "honda", "civic"));

       this.lis.add(new Car(2017, "Lamborghini", "aventador"));

       this.lis.add(new Car(2000, null, "caravan"));

       this.lis.add(new Car(2010, "dodge", null));

      

   }

  

   public Car(int year, String car_name, String type){

      

       this.year = year;

       this.car_name = car_name;

       this.type = type;

       System.out.println(this.year+" "+this.car_name+" "+this.type);

   }

  

   public Car(Car c){

       this.lis.add(c);

   }

   public void addCar(Car car){

       this.lis.add(car);

   }

   public Car getCar(int index){

       if(index >=0 && index < this.lis.size()){

           return this.lis.get(index);

       }

       else{

           return null;

       }

   }

   public Car getOldestCar(){

       ArrayList<Car> x = this.lis;

       Collections.sort(x, (p1, p2) -> p1.getYear() - p2.getYear());

      

       return x.get(0);

   }

   public int getNewestCar(){

       ArrayList<Car> x = this.lis;

       Collections.sort(x, (p1, p2) -> p1.getYear() - p2.getYear());

      

       return x.get(x.size()-1).getYear();

      

   }

  

   public int getYear() {

       return year;

   }

  

  

}

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