Stacks: Railroad switching network (IN JAVA) A program is to be written to model
ID: 3661472 • Letter: S
Question
Stacks: Railroad switching network (IN JAVA)
A program is to be written to model a railroad switching yard. One part of the switching network consists of a main track and a siding that contains four exits onto which cars may be shunted and removed later. Railroad cars are numbered 1-7 on the right track(randomly arranged or entered by the user), they have to be moved to the left track (they have to be sorted in the left track), using the four exits. A car may be moved directly to the left track, or to one of the exits then to the left track. Use stacks to solve the problem.
Input
The order of the cars on the right track (random or by user)
Output
You should show a script showing the car number and the source and destination till the final solution is reached.
Explanation / Answer
import java.util.EmptyStackException;
public class ArraysStack
{
// data members
int arrayTopMost; // current arrayTopMost of stack
Object [] arrayOfStacks; // element array
// constructors
/** create a arrayOfStacks with the given initCapacity
* @throws IllegalArgumentException when initCapacity < 1 */
public ArraysStack(int initCapacity)
{
if (initCapacity < 1)
throw new IllegalArgumentException
("initCapacity must be >= 1");
arrayOfStacks = new Object [initCapacity];
arrayTopMost = -1;
}
/** create a arrayOfStacks with initCapacity 10 */
public ArraysStack()
{this(10);}
// methods
/** @return true if arrayOfStacks is IsEmptyStack */
public boolean IsEmptyStack()
{return arrayTopMost == -1;}
/** @return arrayTopMost element of arrayOfStacks
* @throws EmptyStackException when the arrayOfStacks is IsEmptyStack */
public Object top()
{
if (IsEmptyStack())
throw new EmptyStackException();
return arrayOfStacks[arrayTopMost];
}
/** add theStackElement to the arrayTopMost of the arrayOfStacks */
public void push(Object theStackElement)
{
// increase array size if necessary
if (arrayTopMost == arrayOfStacks.length - 1)
arrayOfStacks = ChangeArrayLength.changeLength1D(arrayOfStacks, 2 * arrayOfStacks.length);
// put theStackElement at the arrayTopMost of the arrayOfStacks
arrayOfStacks[++arrayTopMost] = theStackElement;
}
/** remove arrayTopMost element of arrayOfStacks and return it
* @throws EmptyStackException when the arrayOfStacks is empty */
public Object delete()
{
if (IsEmptyStack())
throw new EmptyStackException();
Object arrayTopMostElement = arrayOfStacks[arrayTopMost];
arrayOfStacks[arrayTopMost--] = null; // enable garbage collection
return arrayTopMostElement;
}
/*test program */
public static void main(String argumnts[])
{
int x;
ArraysStack as = new ArraysStack(3);
// add a few elements
as.push(new Integer(1));
as.push(new Integer(2));
as.push(new Integer(3));
as.push(new Integer(4));
// remove all elements
while (!as.IsEmptyStack())
{
System.out.println("Array Top Most element is " + as.top());
System.out.println("Deleted element is " + as.delete());
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.