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...
Python Design a class named IP_address to represent IP address objects. The IP_address class contains the...
Python Design a class named IP_address to represent IP address objects. The IP_address class contains the following A number of instance variables/fields to store a table of data. You can design them by your own. A constructor that creates a table with the following: a list of data. ip address a integer to indicate the number of elements in the sum_list/freq_list/average_list A get_ip_address() method that returns the ip address For example, consider the following code fragment: ip_key = '192.168.0.24' data_list...
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...
In JAVA write the following program: Objective: Practice object-oriented principles by making two Peanut Butter and...
In JAVA write the following program: Objective: Practice object-oriented principles by making two Peanut Butter and Jelly Sandwiches. The program must create two sandwiches based on user input. The sandwich information for both must then print out their details and determine if the two sandwiches are equal. Requirements: Write a class called Bread with the following Instance Variables Name: The name brand of the bread. o   Calories: The number of calories per slice assumed to be between 50 and 250 inclusively....
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...
TODO 1: Constructor. It assigns the songList member variable to a new instance of an ArrayList...
TODO 1: Constructor. It assigns the songList member variable to a new instance of an ArrayList that stores Song objects.. TODO 2: Implement the isEmpty method. Takes no parameters. Returns true if there are no songs on the list, false otherwise. TODO 3: Implement the addSong method. This method takes a Song object as a parameter and does not return a value. It adds the song to the songList. TODO 4: Implement the getSongListAsString method. This method takes no parameters....
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...