Question

Write a method named changeStack that takes in a Stack of Integer objects named stackIn as...

Write a method named changeStack that takes in a Stack of Integer objects named stackIn as a parameter and returns another Stack of Integer objects. The returned Stack contains contents that is the result of switching the top half and the bottom half of the stackIn. But the ordering of integers in each half is NOT changed. In the case of odd-size stackIn, the middle element remains in the same position before and after the switch.

This method is OUTSIDE the class <E>.

Example 1:

                  stackIn                   Returned Stack

   top                                                               top     

                    30                                     100

                    10                                     50

                    100                                   30

   bottom    50                                     10        bottom

Example 2:

                  stackIn                   Returned Stack

   top                                                                top     

                    15                                     65

                    3                                       8

                    200                                   200

                  65                                     15     

bottom      8                                        3        bottom

The Stack class includes all methods necessary for the stack operations. You can consider Stack is like the ArrayDeque in Java API used as a Stack.

public static Stack<Integer> changeStack(Stack<Integer> stackIn)

Homework Answers

Answer #1
public static Stack<Integer> changeStack(Stack<Integer> stackIn)
  {
      //checks if the size of stack is odd or even
      if(stackIn.size()%2==0)
      {
          //finds the half length of the stack
          int l=stackIn.size()/2;
          
          //creats two arrays to hold both half of stack       
          int arr[]=new int[l];
          int arr2[]=new int[l];
          
          //pop half stack and store in array
          for (int i = 0; i < l; i++) {
              arr[i]=stackIn.pop();
          }
          //pop second half stack and store in array
          for (int i=0;i<l;i++) {
              arr2[i]=stackIn.pop();
          }
          
          //push the first array elements back into the stack one by one in the reverse order 
          for (int i = l-1; i>=0; i--) {
              stackIn.push(arr[i]);
          }

          //push the second array elements back into the stack one by one in the reverse order 
          for (int i = l-1; i>=0; i--) {
              stackIn.push(arr2[i]);
          }
          
          
      }
      else
      {
          int l=stackIn.size()/2;
          
          int arr[]=new int[l];
          int arr2[]=new int[l];
          
          for (int i = 0; i < l; i++) {
              arr[i]=stackIn.pop();
          }
          
          //size of the stack is odd so the next element after popping half the array 
          //must be the middle element. Store it separately in an integer variable
          int middleElement=stackIn.pop();
          
          for (int i=0;i<l;i++) {
              arr2[i]=stackIn.pop();
          }
          
          for (int i = l-1; i>=0; i--) {
              stackIn.push(arr[i]);
          }
          
          //after storing the half elements back in the array, we mush push the middle element
          //as its position should remain unchanged
          stackIn.push(middleElement);
          
          
          for (int i = l-1; i>=0; i--) {
              stackIn.push(arr2[i]);
          }
      }
      return stackIn;
}

Refer to the screenshot below for better clarity and indentation:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
In JAVA Write a RECURSIVE method that receives as a parameter an integer named n. The...
In JAVA Write a RECURSIVE method that receives as a parameter an integer named n. The method will output n # of lines of stars. For example, the first line will have one star, the second line will have two stars, and so on. The line number n will have "n" number of ****** (stars) so if n is 3 it would print * ** *** The method must not have any loops!
Write a method which takes as input an integer, returns true if the integer is prime,...
Write a method which takes as input an integer, returns true if the integer is prime, and returns false otherwise. Do not import anything. If the input is negative, 0 or 1, false should be returned. If the input x is greater than 1, you can test if it is prime (inefficiently) by checking if it is divisible by any integer from 2 up to half of x. If it is not divisible by any of these numbers, then it...
Write Java code that attempts to call a method named bootUp() which takes a String parameter...
Write Java code that attempts to call a method named bootUp() which takes a String parameter and returns an integer value. The String parameter should be read from a file named "bootConfig.txt" and is the only value in the file. If a BootUpException is thrown, print the Exception object's message. If a DriverException occurs, call the reboot() method and rethrow the original DriverException object. If any other type of Exception is thrown, print a generic error message to the console....
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer...
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer no greater than 15. The program should then display a square on the screen using the character ‘X’. The number entered by the user will be the length of each side of the square. For example, if the user enters 5, the program should display the following:       XXXXX       XXXXX       XXXXX       XXXXX       XXXXX INPUT and PROMPTS. The program prompts for an integer as follows: "Enter...
Q. Write a method add(PolyTerm): Two PolyTerm objects can be added if and only if they...
Q. Write a method add(PolyTerm): Two PolyTerm objects can be added if and only if they have the same exponent. If the calling object can be added to the parameter object, return their sum, otherwise (if they can't be added), return null. For example 2x^3 + -7.3x^3 = -5.3x^3 while 2x^3 + 8x^2 should return null. (It's on JAVA) The JUnit Test for this question is: public class PolyTermTest {    public static int score = 0;    public static...
I've posted this question like 3 times now and I can't seem to find someone that...
I've posted this question like 3 times now and I can't seem to find someone that is able to answer it. Please can someone help me code this? Thank you!! Programming Project #4 – Programmer Jones and the Temple of Gloom Part 1 The stack data structure plays a pivotal role in the design of computer games. Any algorithm that requires the user to retrace their steps is a perfect candidate for using a stack. In this simple game you...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as possible: What is a checked exception, and what is an unchecked exception? What is NullPointerException? Which of the following statements (if any) will throw an exception? If no exception is thrown, what is the output? 1: System.out.println( 1 / 0 ); 2: System.out.println( 1.0 / 0 ); Point out the problem in the following code. Does the code throw any exceptions? 1: long value...
JAVA please Arrays are a very powerful data structure with which you must become very familiar....
JAVA please Arrays are a very powerful data structure with which you must become very familiar. Arrays hold more than one object. The objects must be of the same type. If the array is an integer array then all the objects in the array must be integers. The object in the array is associated with an integer index which can be used to locate the object. The first object of the array has index 0. There are many problems where...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import java.io.*; //This imports input and output (io) classes that we use 3 //to read and write to files. The * is the wildcard that will 4 //make all of the io classes available if I need them 5 //It saves me from having to import each io class separately. 6 /** 7 This program reads numbers from a file, calculates the 8 mean (average)...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code to initialize the CustomerAccountsDB database table and add a set of customer accounts is provided. Finish the code in these 3 methods in CustomerAccountDB.java to update or query the database: -purchase(double amountOfPurchase) -payment(double amountOfPayment) -getCustomerName() Hint: For getCustomerName(), look at the getAccountBalance() method to see an example of querying data from the database. For the purchase() and payment() methods, look at the addCustomerAccount() method...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT