plz solve this q In this exercise you will design and construct a Train ADT in p
ID: 3665500 • Letter: P
Question
plz solve this q
In this exercise you will design and construct a Train ADT in pseudocode. This train will be made up of a series of linked TrainCar types. Each TrainCar should have a field for the car’s type (Engine, coal, etc), its cargo capacity in kilograms, a serial number, and a refToTrainCar which will be a pointer to the car behind it on the train. You should define three functions for this ADT, which will be used in constructing and manipulating a train:
createTrainCar(kind, cargo, serial): will create a new TrainCar, not connected to any other car.
connectTrains(front, back): given two refToTrainCar, join them together (connect the first car in the back to the last car in the front). For example: if we have two trains A-->B-->C
X-->Y
the result of connectTrains would be A-->B-->C-->X-->Y. In this function, the second train really becomes part of the first train.
disconnectCars(train, serial): will break a train into two smaller trains, splitting the train after the TrainCar containing the given serial number. Return a refToTrainCar which points to the car after the one with the given serial number. For example: if we have a train A-->B-->C-->X-->Y, disconnecting at B would cause B to have no train after it, and would return the refToTrainCar for C. In this case C would be the start of a smaller train C-->X-->Y.
printTrain(train): given a refToTrainCar, display the cars one by one from the first to the last car
HINT: You will likely need multiple algorithms for each of the above operations. Use top-down design to break each of the problems into smaller subproblems.
Using your operations, the following pseudo-code for main would make sense:
Algorithm main
refToTrainCar a createTrainCar("engine", 0, 111)
refToTrainCar b createTrainCar("tanker", 8000, 115)
refToTrainCar c createTrainCar("tanker", 8000, 117)
refToTrainCar f createTrainCar("stock", 5000, 270)
refToTrainCar d createTrainCar("cargo", 6000, 214)
refToTrainCar e createTrainCar("cargo", 5000, 260)
connectTrains(a,b)
connectTrains (a,c)
connectTrains(a,d)
connectTrains(a,e)
connectTrains(a,f )
printTrain(a) // prints them all
refToTrainCar g = disconnectCars(a,214)
printTrain(a) // prints from 111 to 214
printTrain(g) // prints from 260 to 270
In this exercise you will design and construct a Train ADT in pseudocode. This train will be made up of a series of linked TrainCar types. Each TrainCar should have a field for the car's type (Engine, coal, etc), its cargo capacity in kilograms, a serial number, and a refToTrainCar which will be a pointer to the car behind it on the train. You should define three functions for this ADT, which will be used in constructing and manipulating a train: 1.createTrainCar(kind, cargo, serial): will create a new TrainCar, not kind, cargo, serial): will create a new TrainCar, not connected to any other car 2.connectTrains(front, back): given two refToTrainCar, join them together (connect the first car in the back to the last car in the front). For example: if we have two trains ABCExplanation / Answer
class TrainCar {
carType
carCargo
serialNumber
refToTrainCar
createTrainCar(kind, cargo, serial) {
carType = kind
carCargo = cargo
serialNumber = serial
}
connectTrains(front, back) {
while (front.refToTrainCar != null) {
front = front.refToTrainCar
}
front.refToTrainCar = back
}
disconnectCars(train, serial) {
while (front.refToTrainCar != null) {
if front.serial == serial {
front.refToTrainCar = null
break
}
else {
front = front.refToTrainCar
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.