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...
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...
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...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT
Active Questions
  • Consider insertsort. Suppose that the input array A has 1% probability to be monotonically decreasing. Show...
    asked 7 minutes ago
  • Your company is thinking of introducing a Bring Your Own Device (BYOD) policy. You have been...
    asked 14 minutes ago
  • Attached is the file GeometricObject.java. Include this in your project, but do not change. Create a...
    asked 16 minutes ago
  • Suppose the number of cars in a household has a binomial distribution with parameters n =...
    asked 19 minutes ago
  • HR needs some information on the new interns put into a database. Given an id, email,...
    asked 40 minutes ago
  • Problem solving strategies Questions years = input("Enter a number of years and I'll tell you how...
    asked 44 minutes ago
  • Calculate ?Hrxn for the following reaction: CH4(g)+4Cl2(g)?CCl4(g)+4HCl(g) Use the following reactions and given ?H?s. C(s)+2H2(g)?CH4(g)?H=?74.6kJC(s)+2Cl2(g)?CCl4(g)?H=?95.7kJH2(g)+Cl2(g)?2HCl(g)?H=?184.6kJ Express...
    asked 51 minutes ago
  • ASCII (American Standard Code for Information Interchange) has an encoding for every character of the alphabet,...
    asked 1 hour ago
  • Is home confinement with electronic monitoring a deterrent? Are there negatives to being confined to one’s...
    asked 1 hour ago
  • Social hostility can have severe lasting effects of interperpersonal relationship during our adolescence years, which if...
    asked 1 hour ago
  • - A series RLC circuit has R=15 ?, L=1.5 H, and C=15 ?F. (a) For what...
    asked 1 hour ago
  • TV Circuit has 30 large-screen televisions in a warehouse in Erie and 60 large-screen televisions in...
    asked 1 hour ago