Question: Write a program named CarList that maintains a linked list of Car objects and provides a set of operations on the list. Begin with the
Write a program named CarList that maintains a linked list of Car objects and provides a set of operations on the list. Begin with the CarType and Car included in the Assignment 4 folder (these are similar to the A1 solutions, but with the Dealership information removed). You will also have to implement a CarNode class to represent an individual node in the CarList. Your CarList class list should contain one instance variable (a reference to the start of the linked list) and one class constant (a NOT_FOUND int). No other instance variables should be added to your CarList class. Your CarList must implement the methods below. For full marks, your methods should be efficient in that they should not do any unnecessary extra traversals through the list. Your CarList methods should not instantiate any new Car objects or CarType objects.
A constructor with no parameters, which creates an empty list
public void insert(Car newCar) the insert method should order the Cars in the list such that they are in ascending order according to ID. public int size(), which returns the number of Cars in the list.
public Car get(int pos), which returns a reference to the Car at that position in the list. The method should return null if pos is not a valid position. Like with arrays, position 0 is the first element in the list.
public String toString() which returns a String representation of the list as shown in the sample output below. If there are no items in the list, the String representation should contain There are no cars in the list as shown in the sample output.
public int rentType(CarType), which finds the first available Car of the given CarType in the list and marks the Car as rented. The method should return the rented Cars id or the value of the NOT_FOUND class constant if no such car exists.
public boolean returnCar(int id) , which finds the Car that matches the car ID passed as a parameter, and marks it as now available. The method should return true if the correct Car was found and false otherwise.
public CarList getRented(). This method should return a new CarList containing only the rented Cars from this instance. This instances linked list should remain unchanged.
public boolean remove(int id). This method should remove the car whose ID matches the one passed as a parameter from the list. The method should return true if the operation was successful and false if there is no such Car in the list.
public int remove(CarType). This method should remove all Cars from the list whose CarType matches the parameter. The method should return the number of Cars that were removed.
//Code result
Created an empty CarList
Cars in the list:
There are no cars in the list
Size: 0
After inserting c1
Cars in the list:
Toyota, Corolla, 2010, blue, ID20210001, Available
Size: 1
After inserting c3, c4 and c2
Cars in the list:
Toyota, Corolla, 2010, blue, ID20210001, Available Toyota, Rav4, 2018, red, ID20210002, Available
Toyota, Corolla, 2010, blue, ID20210003, Available
Ford, Explorer, 2012, white, ID20210004, Available
Size: 4
Trying out the get method
Car at position 0: Toyota, Corolla, 2010, blue, ID20210001, Available
Car at position 3: Ford, Explorer, 2012, white, ID20210004, Available
Car at position 6: null
Renting a t2 car
Returned id: 20210002
Renting a t4 that isn't isn't in the list
Should return NOT_FOUND: true
Trying to rent another t2 car
Should return NOT_FOUND: true
Cars in the list:
Toyota, Corolla, 2010, blue, ID20210001, Available
Toyota, Rav4, 2018, red, ID20210002, Rented
Toyota, Corolla, 2010, blue, ID20210003, Available
Ford, Explorer, 2012, white, ID20210004, Available
Returning the rented car
Returning a car that doesn't exist. Should return false: false
Cars in the list:
Toyota, Corolla, 2010, blue, ID20210001, Available
Toyota, Rav4, 2018, red, ID20210002, Available
Toyota, Corolla, 2010, blue, ID20210003, Available
Ford, Explorer, 2012, white, ID20210004, Available
Renting two cars: a t1 and t2
Getting the list of rented cars
Cars in the list:
Toyota, Corolla, 2010, blue, ID20210001, Rented
Toyota, Rav4, 2018, red, ID20210002, Rented
Removing some cars c2 removed. Size is now: 3
Cars in the list: Toyota, Corolla, 2010, blue, ID20210001, Rented
Toyota, Corolla, 2010, blue, ID20210003, Available
Ford, Explorer, 2012, white, ID20210004, Available
c1 removed. Size is now: 2
Cars in the list: Toyota, Corolla, 2010, blue, ID20210003, Available
Ford, Explorer, 2012, white, ID20210004, Available
Tried to removed an ID not in the list. Size is now: 2
Cars in the list:
Toyota, Corolla, 2010, blue, ID20210003, Available
Ford, Explorer, 2012, white, ID20210004, Available
Adding c1 and c2 back Cars in the list:
Toyota, Corolla, 2010, blue, ID20210001, Rented
Toyota, Rav4, 2018, red, ID20210002, Rented
Toyota, Corolla, 2010, blue, ID20210003, Available
Ford, Explorer, 2012, white, ID20210004, Available
Removing cars of this type: Toyota, Corolla, 2010, blue
Number of cars removed: 2.
Size of list is: 2 Cars in the list:
Toyota, Rav4, 2018, red, ID20210002, Rented
Ford, Explorer, 2012, white, ID20210004, Available
//Here is the test class
public class TestPhase1CarList {
public static void main(String[] args) {
// Creating some CarType objects and Car object for use in the CarList
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);
// Instantiate a CarList object
CarList dealership = new CarList();
System.out.println("Created an empty CarList");
System.out.println(dealership);
System.out.println("Size: " + dealership.size());
//Insert one Car
dealership.insert(c1);
System.out.println(" After inserting c1");
System.out.println(dealership);
System.out.println("Size: " + dealership.size());
//Insert three more Cars
dealership.insert(c3);
dealership.insert(c4);
dealership.insert(c2);
System.out.println(" After inserting c3, c4 and c2 ");
System.out.println(dealership);
System.out.println("Size: " + dealership.size());
// Trying out the get method
System.out.println(" Trying out the get method");
System.out.println("Car at position 0: " + dealership.get(0));
System.out.println("Car at position 3: " + dealership.get(3));
System.out.println("Car at position 6: " + dealership.get(6));
//Trying out the rentType method
System.out.println(" Renting a t2 car");
System.out.println("Returned id: " + dealership.rentType(t2));
System.out.println("Renting a t4 that isn't isn't in the list");
System.out.println("Should return NOT_FOUND: " + (dealership.rentType(t2) == CarList.NOT_FOUND));
System.out.println("Trying to rent another t2 car");
System.out.println("Should return NOT_FOUND: " + (dealership.rentType(t2) == CarList.NOT_FOUND));
System.out.println(dealership);
//Trying out the returnCar method
System.out.println(" Returning the rented car");
dealership.returnCar(c2.getID());
System.out.println("Returning a car that doesn't exist. Should return false: " + dealership.returnCar(1404));
System.out.println(dealership);
//Trying out the getRented method
System.out.println(" Renting two cars: a t1 and t2");
dealership.rentType(t2);
dealership.rentType(t1);
System.out.println("Getting the list of rented cars");
CarList rentedCars = dealership.getRented();
System.out.println(rentedCars);
//Trying out the remove method
System.out.println(" Removing some cars");
dealership.remove(c2.getID());
System.out.println("c2 removed. Size is now: " + dealership.size());
System.out.println(dealership);
dealership.remove(c1.getID());
System.out.println("c1 removed. Size is now: " + dealership.size());
System.out.println(dealership);
dealership.remove(14);
System.out.println("Tried to removed an ID not in the list. Size is now: " + dealership.size());
System.out.println(dealership);
System.out.println(" Adding c1 and c2 back");
dealership.insert(c1);
dealership.insert(c2);
System.out.println(dealership);
//Trying out remove(Type)
System.out.println(" Removing cars of this type: " + t1);
System.out.println("Number of cars removed: " + dealership.remove(t1) + ". Size of list is: " + dealership.size());
System.out.println(dealership);
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
