Question
Please help with the connectCar, disconnectCar, and hashCode method. I need to use Doubly LinkedList for the connectCar and disconnectCar but not sure how to
Please help with the connectCar, disconnectCar, and hashCode method. I need to use Doubly LinkedList for the connectCar and disconnectCar but not sure how to implement it. Thanks
import java.util.*;
class Train implements Iterable
private String name;
private Car head = new Car(null);
public Train(String name) {
this.name = name;
}
public String getName() {
//returns the train's name
//O(1)
return name;
}
public Iterator
//returns an iterator which traverses
//the train from the first car (the one closest
//to the front of the train) to the last car
//use an anonymous class here
//required iterator methods: next() and hasNext()
//both methods are required to work in O(1) time
return new Iterator
{
private Car name = head;
//other private variables here, such as for helping next() and hasNext()
public Car next()
{
name = name.getNext();
return name;
}
public boolean hasNext()
{
if(name.getNext() !=null){
return true;
}
else{
return false;
}
}
};
}
public boolean equals(Object o) {
//two trains are equal if they have the same name
//O(1)
// If the object is compared with itself then return true
if (o == this) {
return true;
}
/* Check if o is an instance of Complex or not
"null instanceof [type]" also returns false */
if (!(o instanceof Train)) {
return false;
}
// typecast o to Complex so that we can compare data members
Train c = (Train) o;
// Compare the data members and return accordingly
if(name.equalsIgnoreCase(c.getName()))
return true;
else
return false;
}
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;
}
public int hashCode() {
//returns a hashcode for a train...
//remember: if two objects are equal, they should
//have the same hashcode
//O(1)
return 0;
}
//example test code... edit this as much as you want!
public static void main(String[] args) {
Car c1 = new Car("C1");
Car c2 = new Car("C2");
c1.setNext(c2);
c2.setPrevious(c1);
Train t1 = new Train("T1");
Train t1b = new Train("T1");
if(t1.getName().equals("T1") && t1.equals(t1b) && t1.hashCode() == t1b.hashCode()) {
System.out.println("Yay 1");
}
t1.printAscii();
t1.connectCar(c1);
t1.printAscii();
Car c3 = new Car("C3");
Car c4 = new Car("C4");
c3.setNext(c4);
c4.setPrevious(c3);
t1.connectCar(c3);
t1.printAscii();
}
/*****************************************************************/
/****************** DO NOT EDIT BELOW THIS LINE ******************/
/*****************************************************************/
public String toString() {
String s = getName();
for(Car c : this) {
s += " " + c;
}
return s;
}
public void printAscii() {
/*
From: http://www.ascii-art.de/ascii/t/train.txt
o O___ _________
_][__|o| |O O O O|
<_______|-|_______|
/O-O-O o o
*/
System.out.print(String.format("%-4s",getName())+"o O___");
for(Car c : this) {
System.out.print(" _________");
}
System.out.println();
System.out.print(" _][__|o|");
for(Car c : this) {
System.out.print(" | "+String.format("%-5s",c.getName())+" |");
}
System.out.println();
System.out.print(" |_______|");
for(Car c : this) {
System.out.print("-|_______|");
}
System.out.println();
System.out.print(" /O-O-O ");
for(Car c : this) {
System.out.print(" o o ");
}
System.out.println();
}
}
Step 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