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

Create a Java program, RailcarSwitcher, that simulates a railroad switching stat

ID: 3854205 • Letter: C

Question

Create a Java program, RailcarSwitcher, that simulates a railroad switching station.

-------------------------- --------------------------

Input --> / Output -->

|

----------------------- -----------------------

| |

| Y |

| a |

| r |

| d |

| |

Railroad engineers often have to switch cars on the trains that they manage. Because

of the lack of maneuverability of these railcars (and the fact that they can only move on

tracks), getting the correct cars off of one train and on to another can be problematic.

Using the above diagram as a guide, cars can enter from the right and are sent to the

output on the left by routing through the yard "stack". Each car can be brought into the

stack and removed at any time.

The RailcarSwitcher program will aid railroad engineers in determining the ordering of

outgoing cars. Design the program with the following specifications:

1.

The RailcarSwitcher class should have the following member variables:

String inSequence

: The railcars to evaluate as the incoming sequence.

String outSequence

: The target sequence that the program will attempt

to build.

final int maxCars = 10

: Largest number of cars that the entire program

can accept at any one time.

inSequence and outSequence should be obtained from the user.

2.

A queue to represent the original car arrangement.

3.

A stack to represent the yard.

4.

A second queue to represent the output arrangement.

5.

push

,

pop

, and

peek

methods to add, remove, and scan items from the stack.

6.

The program should work through the input queue until either the output

sequence is created in its entirety or the program cannot continue to build the

desired output.

Example:

An input of:

1 2 3 4

can be mapped to an output of

4 3 2 1

using the sequence:

push push push push pop pop pop pop

But: An input of:

1 2 3 4

cannot be mapped to an output of

4 2 3 1

The proceeding examples would produce the output:

Attempting to map the input

1 2 3 4

to the output

4 3 2

1

...

The sequence of moves are:

push push push push pop pop pop

pop

Attempting to map the input

1 2 3 4

to the output

4 2 3

1

...

The sequence of moves are:

push push push push pop

The input cannot be mapped to the output.

Explanation / Answer

You can use the following java code

import java.util.Queue;

import java.util.LinkedList;

import java.util.Scanner;

import java.util.Stack;

public class RailSwitcher{

String inSequence;

String outSequence;

final int maxCars = 10;

static Queue<Integer> OriginalCarArrangement = new LinkedList<Integer>();

static Stack<Integer> Yard = new Stack<Integer>();

static Queue<Integer> OutputArrangement = new LinkedList<Integer>();

public static void main(String[] args){

int n;

System.out.println("Enter number of elements");

Scanner reader = new Scanner(System.in);

n = reader.nextInt();

System.out.println("Enter input queue");

for(int i = 0;i<n;i++){

OriginalCarArrangement.add(reader.nextInt());

}

System.out.println("Enter output queue");

for(int i = 0;i<n;i++){

OutputArrangement.add(reader.nextInt());

}

reader.close();

while(OriginalCarArrangement.size() > 0){

Yard.push(OriginalCarArrangement.remove());

System.out.print("push ");

}

while(Yard.size()>0){

int op = Yard.pop();

if(op != OutputArrangement.remove()){

System.out.println("The input" +

" cannot be mapped to the output.");

break;

}

System.out.print("pop ");

}

}

}

The explanation for the code is as follows:

Using the right imports, create the Stack and Queues as mentioned in the question. It is important to note here that, Queue is an interface. So it needs a concrete class to declare it. So usually a LinkedList class is used. Stack is a concrete class itself, so no issue while using that directly.

Moreover, pop and push functions of the Stack have been used here.

The logic behind the code is that, first the numbers in input queue are removed and put one by one in the stack (Yard). Once all numbers are in Stack, pop them and each time compare the number popped to the number in output queue. If there is a mismatch somewhere it means that input cannot be mapped to output.