Question: Need help with Java Let Loose the Caboose Purpose: To learn the basics of doubly linked lists You are to implement 2 classes, Cabin and

Need help with Java

Let Loose the Caboose Purpose: To learn the basics of doubly linked lists You are to implement 2 classes, Cabin and Train. The Cabin class (also known as the node class) has the following fields and methods: private String cabinType private Cabin nextCabin private Cabin prevCabin Cabin(String type, Cabin next, Cabin prev) - A constructor that creates a Cabin object with a set cabinType and the prev/nextCabin fields set Hint: if we want next/prev to point to null we can pass in null for those values public Cabin getNextCabin() - returns the nextCabin from the current one public void setNextCabin(Cabin newCabin) - changes the nextCabin to the cabin passed in public Cabin getPrevCabin() - returns the prevCabin from the current one public void setPrevCabin(Cabin newCabin) - changes the prevCabin to the cabin passed in public String getCabintype() - returns the cabintype Your Train class has the following fields and methods: public Cabin Front (this is also known as head) - stores the first Cabin in the train public Cabin Caboose (this is also known as tail) - stores the last Cabin in the train public Train() - A Train constructor which initializes the the Front and Caboose variables to null public void addCabin(String cabinType) - A method that adds a new Cabin to the end of the Train (aka it becomes the new tail) public void addCabin(String cabinType, int pos) - A method that adds a new Cabin to the Train at the position passed in, if that position is not accessible such as if we try to add something at pos=45 and there are only 3 elements in the list we dont add anything Hint: Use a while loop that both keeps track of if we are at the end of the list and how many indexes we have passed public Cabin removeCabin() - A method that removes a Cabin from the end of the Trains cabinManifest, and returns the removed object (aka we remove the tail of the train) public Cabin removeCabin(int pos) - A method that removes the Cabin at the passed in position from the Trains cabinManifest, and returns the removed object if the position is not in the linked list nothing is removed we just return null. Note: The head of the linked list should indexed at 0 Hint: Use a while loop that both keeps track of if we are at the end of the list and how many indexes we have passed public void swap(int pos1, int pos2) - A method that takes the Cabins at each of the passed in positions and swaps them, if there are not cars at both positions it does nothing Hint: Similar to the above position dependent methods use a while loop for EACH of the cabins to find them (so we will use 2 seperate loops NOT NESTED) then we can change the next and previous cabin fields public String toString() - returns a string where each of the Cabins cabinType is separated by a comma and space. Ex: cabintype1, cabintype2, cabintype3 Note/Hint:It may be helpful to create a helper method that finds a node at a given position in the linked list, that way you would only have to write the code once and could use it in other methods. It is definitely is not necessary, but can help simplify the code. You also need to create a Driver class with a main method. In main: Create a Train object Add Cabins until it is: Tanker,Passenger1,Passenger2,Bananas,Monkeys Print out the toString() of the Train object Call removeCabin() on the Train Print out the toString() of the Train object Call removeCabin(2) on the Train Print out the toString() of the Train object Call swap(1,2) on the train Print out the toString() of the Train object Add a Cabin called Monkey Madness to the front of the train Print out the toString() of the Train object Example Output: Tanker,Passenger1,Passenger2,Bananas,Monkeys Tanker,Passenger1,Passenger2,Bananas Tanker,Passenger1,Bananas Tanker,Bananas,Passenger1 Monkey Madness,Tanker,Bananas,Passenger1

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!