JAVA code please include comments... Using the model railroad switching system a
ID: 3829018 • Letter: J
Question
JAVA code please include comments... Using the model railroad switching system as above. Railroad cars are to -- enter from the right and are sent to the output on the left by routing -- through the stack. Each car can be brought into the stack and removed at -- any time. This program aids the switch yard manager in determining the -- ordering of the outgoing cars. The program expects two sequences of cars. -- The first is the incomming sequence, and the second is the desired out-going -- sequence. The program tells the manager if the mapping can be performed -- and the ordering of the pushes and pops. -- For 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 -- -- Specifications: -- -- 1. This program will read pairs of input sequences consisting of up to -- 10 strings -- -- 2. The program prints the sequence of push and pop operations that are -- needed to be performed. -- -- 3. If the mapping can be accomplished successfully, outputs: -- -- The input can be mapped to the output -- -- 4. If the mapping cannot be accomplished successfully, outputs: -- -- The input cannot be mapped to the output -- -- The preceeding 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 -- The input can be mapped to the output -- -- 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 -- -- 5. The program processes all input to the end of the input. -- -- -- Error Handling: -- -- 1. If the user attempts to enter more than the maximum allowable number of -- elements the program outputs: -- -- A maximum of only 10 items are allowed to be input for each sequence - Please re-enter -- -- The input lines are cleared for the next input pair. -- --
Explanation / Answer
private static int result = 0;
private static int n = 4;
public static void g(int right,int spur)
{
if (right == n)
result++;
else if (right + spur < n)
g(right,spur + 1);
if (spur > 0)
g(right + 1,spur - 1);
public static void main (String[] args)
{
g(0,0);
System.out.println(result);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.