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

I am trying to add a \"car\" to the end of a train. \"car\" is a node of a linke

ID: 3596747 • Letter: I

Question

I am trying to add a "car" to the end of a train. "car" is a node of a linked list. I pass car node to train. For example, car C1 has car C2 attached to it. Now I want to attach car C1 to Train T1. So T1 will be attached to C1, which in turn is attached to C2 (shown below). I have to write a connect method. Here is what I have.

public void connectCar(Car c) {

//connects the car to the end of the cars for this train

//requied Big-O: O(n) where n=the length of the linked list

//of cars starting at c, NOT n=the number of cars already

//connected to this train.

Train temp = new Train(name);

// if(head==null){

// head = c;

// temp.car.setNext(head);

// } else {

// tail = head.getNext();

// temp.car = tail;

// }

if(head==null){

head = tail = temp;

}else{

temp.car = c;

temp.car.setNext(temp.car)

}

}

public void connectCar(Car c) { //connects the car to the end of the cars for this train //requied Big-O: O(n) where n=the length of the linked list //of cars starting at c, NOT n=the number of cars already //connected to this train. }

public Car disconnectCar(Car c) { //returns the car disconnected from the train //should throw the following exception if the car isn't one //the train: RuntimeException("Can not disconnect a car that doesn't exist"); //required Big-O: O(n) where n=the number of cars on this train return null; }

/O-O-O

Explanation / Answer

package com.test;

public class List {

class Car{

String name;

Car next;

public Car(String name) {

super();

this.name = name;

this.next = null;

}

public String getName() {

return name;

}

public Car getNext() {

return next;

}

public void setName(String name) {

this.name = name;

}

public void setNext(Car next) {

this.next = next;

}

}

class Train{

String name;

Car car;

public Train(String name) {

super();

this.name = name;

this.car = null;

}

}

Train T = null;

Car head = null;

Car tail = null;

public void connectCar(Car c) {

//connects the car to the end of the cars for this train

//requied Big-O: O(n) where n=the length of the linked list

//of cars starting at c, NOT n=the number of cars already

//connected to this train.

Train temp = T;

Car head = T.car;

Car curr = head;

//traverse till last car node

while(curr.next != null) {

curr = curr.next;

}

//add car object at end of car list

curr.next = c;

}

public Car disconnectCar(Car c) {

//returns the car disconnected from the train

//should throw the following exception if the car isn't one

//the train: RuntimeException("Can not disconnect a car that doesn't exist");

//required Big-O: O(n) where n=the number of cars on this train return null;

if(T == null)

throw new RuntimeException("Can not disconnect a car that doesn't exist");

else

{

if(T.car == null)

throw new RuntimeException("Can not disconnect a car that doesn't exist");

Car c1 = T.car;

return c1;

}

}

}

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