Question
Provided code: // CAR CLASS public class Car { //instance variable private CarType type; private boolean available; private int id; //class variable nextID tracks the
Provided code:
//CAR CLASS
public class Car { //instance variable private CarType type; private boolean available; private int id;
//class variable nextID tracks the id to be given to the next Car static private int nextID = 20210001;
// The constructor initializes both instances variables uses the next available id public Car(CarType type) { this.type = type;
// All new cars are set to available this.available = true;
// Assign the nextID, it will get incremented after the assignment this.id = nextID++; }
// accessor method public CarType getType() { return type; }
// accessor method public int getID() { return id; }
// accessor method public boolean isAvailable() { return available; }
// Two Cars are equal if their ids match public boolean matches(int id) { return this.id == id; }
// return a car, it is now available public void returnCar() { available = true; }
// rent a car, it is no longer available public void rentCar() { available = false; }
// returns a String representation of the object and it's availability public String toString() { return this.type.toString() + ", ID-" + id + ", " + (available ? "Available" : "Rented"); } }
//CAR TYPE CLASS
public class CarType {
// Instance Variables private String make; private String model; private int year; private String colour;
// The constructor initializes the basic instance variables (Phase 1-3) and the // Partially Filled Array (Phase 4) public CarType(String make, String model, int year, String colour) { this.make = make; this.model = model; this.year = year; this.colour = colour;
}
// returns a String representation of the object's basic instance variables, // seperated by commas public String toString() { return make + ", " + model + ", " + year + ", " + colour; }
// Two CarModels are equal if they have the same Make, Model, Year, and Colour public boolean equals(CarType otherCar) { return this.make.equals(otherCar.make) && this.model.equals(otherCar.model) && this.year == otherCar.year && this.colour.equals(otherCar.colour); } }
//main code
//CAR LIST CLASS
public class CarList { private CarNode head; public static final int NOT_FOUND = -1;
public CarList() { head = null; }
public int remove(CarType carType) { int removed = 0; CarNode current = head; CarNode previous = null; while (current != null) { if (current.getCar().getType() == carType) { if (current == head) { head = head.getNext(); } else { previous.setNext(current.getNext()); } removed++; } previous = current; current = current.getNext(); } return removed; }
public boolean remove(int id) { if (head.getCar().getID() == id) { head = head.getNext(); return true; } else { CarNode current = head; CarNode previous = null; while (current != null) { if (current.getCar().getID() == id) { previous.setNext(current.getNext()); return true; } previous = current; current = current.getNext(); } return false; } }
public CarList getRented() { CarList rentedList = new CarList(); CarNode current = head; while (current != null) { if (!current.getCar().isAvailable()) { rentedList.insert(current.getCar()); }
current = current.getNext(); }
return rentedList; }
public boolean returnCar(int id) { CarNode current = head; while (current != null) { if (current.getCar().getID() == id) { current.getCar().returnCar(); return true; }
current = current.getNext(); } return false; }
public int rentType(CarType carType) { CarNode current = head; while (current != null && (current.getCar().getType() != carType || (!current.getCar().isAvailable()))) { current = current.getNext(); } if (current == null) { return NOT_FOUND; } else { current.getCar().rentCar(); return current.getCar().getID(); } }
public Car get(int pos) { CarNode current = head; while (current != null && pos > 0) { current = current.getNext(); pos--; } return current == null? null : current.getCar(); }
public void insert(Car car) { CarNode newNode = new CarNode(car, null); if (head == null) { head = newNode; } else if (head.getCar().getID() > car.getID()) { newNode.setNext(head); head = newNode; } else { CarNode current = head; CarNode previous = null; while (current != null && current.getCar().getID()
public int size() { CarNode current = head; int size = 0; while (current != null) { size++; current = current.getNext(); }
return size; }
@Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append("Cars in the list: "); if (head == null) { return "There are no cars in the list"; } else { CarNode current = head; while (current != null) { builder.append(current.getCar().toString() + " "); current = current.getNext(); } return builder.toString().trim(); } } }
//CAR NODE CLASS
public class CarNode { private Car car; private CarNode next;
public CarNode(Car car, CarNode next) { this.car = car; this.next = next; }
public Car getCar() { return car; }
public CarNode getNext() { return next; }
public void setCar(Car car) { this.car = car; }
public void setNext(CarNode next) { this.next = next; } }
//TEST CLASS
public class TestPhase2CarList {
public static void main(String[] args) { // Creating some new Car and CarType objects CarType t1 = new CarType("Toyota", "Corolla", 2010, "blue"); CarType t2 = new CarType("Toyota", "Rav4", 2018, "red"); CarType t3 = new CarType("Ford", "Explorer", 2012, "white"); CarType t4 = new CarType("Honda", "CRV", 2021, "blue"); Car c1 = new Car(t1); Car c2 = new Car(t2); Car c3 = new Car(t1); Car c4 = new Car(t3); // Creating an initial empty list CarList dealership = new CarList(); // populating the list with data dealership.insert(c1); dealership.insert(c2); dealership.insert(c3); dealership.insert(c4); dealership.rentType(t2); System.out.println(dealership); //Trying out the getNumAvaible method System.out.println("The number of available cars: " + dealership.getNumAvailable()); //Creating and populating a second list. CarList d2 = new CarList(); d2.insert(c1); d2.insert(c2); d2.insert(c3); d2.insert(c4); System.out.println(" d2 is: "); System.out.println(d2); //Trying out the equals method System.out.println(" Are dealership and d2 equal(Should be true)? " + dealership.equals(d2)); d2.remove(c4.getID()); System.out.println("After removing an item. Are dealership and d2 equal now (Should be false)? " + dealership.equals(d2)); //Trying out the getAvailable method CarList d3 = dealership.getAvailable(t1); System.out.println(" Calling getAvailable with type: " + t1); System.out.println(d3); System.out.println("Checking that the original list remained unchanged is: "); System.out.println(dealership); System.out.println(" Calling getAvailable with type: " + t2); CarList d4 = dealership.getAvailable(t2); System.out.println("d4 is: "); System.out.println(d4); } }
Phase 2: Linked Lists and Recursion Add the following instance three methods and their helper methods) to the Carlist class. These problems must be solved using recursion. There should be no loops of any kind (including calls to other methods that have loops). public int getNum Available() which returns of the number of Cars available to rent in the list. This method should call a private recursive helper method that traverses the list recursively. public boolean equals(Carlist), which returns true if the two lists contain the same Car data in the same order. This method should call a private recursive helper method that traverses the lists recursively. public Carlist getAvailable(CarType), which returns a new Carlist that contains all of the available cars of the given CarType. This method should call a private helper method to build up the elements in the new list recursively. This instance's linked list should remain unchangedStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started