Question: Assignment11 class This class displays a menu for the Stack Rearranging. If a user enters E, then it asks to enter a size of the

Assignment11 class
This class displays a menu for the Stack Rearranging. If a user enters "E", then it asks to enter a size of the initial stack and also a number of holding stacks, then it will display a result for the problem. This class is given by the instructor.
-------------------------------------------------------------
StackRearranger class
The StackRearranger class contains a constructor to set up an initial configuration. It also contains methods to print out stacks as well as to rearrange stack content. You need to complete the following methods. Please see the StackRearranger.java for more details. There are 6 portions where you need to complete the code. Please look for the line such as: /****1. ADD Your Code Here ****/ since that is where you need to add more code.
public boolean rearrange()
You need to write the rearrange method that rearranges the initial stack and also determines if it is feasible. It should return true if a rearrangement is successful, false otherwise. Please see the StackRearranger.java file for more details.
public void fromHoldingStackToOutputStack()
You need to write the fromHoldingStackToOutputStack method that moves the smallest element among all holding stacks into the output stack. It also keeps track of the next smallest number and the holding stack that contains in it using the variable smallestNumber and stackWithNextSmallest. Please see the StackRearranger.java file for more details.
public boolean putInHoldingStack(int number)
You need to write the putInHoldingStack method that tries to push the parameternumber into the best stack, i.e., the one with the top element larger than the parameter number and also with the top element smallest among such holding stacks. If it cannot find such holding stack, it should return false. It should return true otherwise return true. Please see the StackRearranger.java file for more details.
Requirements:
You need to implement this method using an object of the Stack class in java.util package.
Files provided by instructor:
Assignment11.java (Needs modified)
import java.io.*; public class Assignment11 { public static void main (String[] args) throws IOException { char input1; String line = new String(); int mazeSize = 10; String[] mazeInfo = new String[mazeSize]; printMenu(); InputStreamReader isr = new InputStreamReader(System.in); BufferedReader stdin = new BufferedReader(isr); do // will ask for user input { System.out.println("What action would you like to perform?"); line = stdin.readLine(); input1 = line.charAt(0); input1 = Character.toUpperCase(input1); if (line.length() == 1) { // matches one of the case statements switch (input1) { case 'E': //Enter Problem parameters try { System.out.print("Please specify how many numbers will be entered: "); int stackSize = Integer.parseInt(stdin.readLine().trim()); System.out.print("Please specify how many holding stacks to use: "); int numberOfHoldingStacks = Integer.parseInt(stdin.readLine().trim()); if (stackSize > 0 && numberOfHoldingStacks > 0) { //Create a re-arranger object StackRearranger rearranger = new StackRearranger(stackSize, numberOfHoldingStacks); //Read-in numbers and add them to the stack for (int i = 0; i -------------------------------------------------------------
StackRearranger.java (needs completed)
import java.util.Stack; public class StackRearranger { private Stack initialStack; //stack contains numbers in an order specified by a user private Stack outputStack; //stack to have numbers in sorted order after running this algorithm private Stack[] holdingStacks; //stacks to hold some numbers that are not in either initial or output stack private int sizeOfInitialStack; //the initial stack size private int numberOfHoldingStacks; /umber of holding stacks, specified by a user private int smallestNumber; //current smallest number that can be moved to the output stack private int stackWithNextSmallest; //the index of holding stack that contains the next smallest number //Constructor to initialize member variables public StackRearranger(int sizeOfInitialStack, int numOfHoldingStacks) { initialStack = new Stack(); outputStack = new Stack(); this.sizeOfInitialStack = sizeOfInitialStack; this.numberOfHoldingStacks = numOfHoldingStacks; holdingStacks = new Stack[numberOfHoldingStacks]; for (int i=0; i= 0 && holdingStacks[stackWithNextSmallest].isEmpty() == false) { //remove(pop) the smallest number from its stack to move to the output stack //and move(pop) it to output stack /****3. ADD Your Code Here ****/ System.out.println("Move number " + smallestNumber + " from holding stack#" + stackWithNextSmallest + " to output stack"); printHoldingStacks(); } //Find the next smallest number and the holding stack that contains it //by checking the top of all holding stacks smallestNumber = sizeOfInitialStack + 1; for (int i = 0; i Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
