In: Computer Science
its java language.
1. Create a class called Car. A Car has instance variables, constructor parameters, setters, and getters for “year manufactured” and “make” and “model” (e.g. 2016 Honda Civic) 2. Create a class called CarLot 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[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); (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; if there 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. Use your Car class from the previous question. 2. Create a class called UsedCarLot which has an ArrayList of Car references. 3. The constructor takes parameter: the name of the UsedCarLot (e.g. “Jason’s Used Cars”)…store this in the instance variable String CarLotName and has an accessor too: public String getName(). Also the constructor does this: It simply populates the ArrayList with these cars: cars.add(new Car(2016, “honda”, “civic”)); cars.add(new Car(2017, “Lamborghini”, “aventador”)); cars.add(new Car(2000, null, “caravan”)); cars.add(new Car(2010, “dodge”, null)); 4. Create a method public void addCar(Car car) which adds the Car parameter to the ArrayList. 5. Create a method public void removeCarsBetween(int firstYear, int lastYear) which removes all Cars from the list which were manufactured between firstYear and lastYear (inclusive). Use an iterator. 6. Create a method public Car[] getCarsMadeBy(String maker) which returns an array of Car references…all of the Cars which are made by the parameter maker.
If you have any problem with the code feel free to comment.
Car
public class Car {
private int year;
private String make;
private String model;
public Car(int year, String make, String model)
{
this.year = year;
this.make = make;
this.model = model;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
}
CarLot
public class CarLot {
private Car[] cars = new Car[5];
public CarLot() {
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);
}
public CarLot(Car car) {
cars[0] = car;
}
public void addCar(Car car) {
boolean check = true;
for (int i = 0; i < cars.length;
i++) {
if (cars[i] ==
null) {
cars[i] = car;
check = false;
break;
}
}
if (check)
System.out.println("no room");
}
public Car getCar(int index) {
if (index <= cars.length)
return
cars[index];
return null;
}
public Car getOldestCar() {
int oldYear =
cars[0].getYear();
Car oldCar = cars[0];
for (int i = 1; i <
cars.length; i++) {
if (oldYear >
cars[i].getYear()) {
oldYear = cars[i].getYear();
oldCar = cars[i];
}
}
return oldCar;
}
}
UsedCarLot
import java.util.ArrayList;
import java.util.Iterator;
public class UsedCarLot {
private ArrayList<Car> cars = new
ArrayList<>();
private String carLotName;
public UsedCarLot(String carLotName) {
this.carLotName = carLotName;
cars.add(new Car(2016, "honda",
"civic"));
cars.add(new Car(2017,
"Lamborghini", "aventador"));
cars.add(new Car(2000, null,
"caravan"));
cars.add(new Car(2010, "dodge",
null));
}
public String getName() {
return carLotName;
}
public void addCar(Car car) {
cars.add(car);
}
public void removeCarsbetween(int firstYear, int
lastYear) {
Iterator<Car> itr =
cars.iterator();
Car temp;
while(itr.hasNext()) {
temp =
itr.next();
if(temp.getYear()>=firstYear &&
temp.getYear()<=lastYear)
cars.remove(temp);
}
}
public Car[] getCarsMadeBy(String maker) {
int size=0;
for(Car i: cars) {
if(i.getMake().equalsIgnoreCase(maker))
size++;
}
Car[] carAr = new Car[size];
int index=0;
for(Car i: cars) {
if(i.getMake().equalsIgnoreCase(maker))
carAr[index++] = i;
}
return carAr;
}
}