Question

Java Program Write a method called reverseBottomHalf that accepts a Stack of integers as a parameter...

Java Program

Write a method called reverseBottomHalf that accepts a Stack of integers as a parameter and reverses only the values in the bottom half of the Stack. For example, if a Stack containing the values [1, 2, 3, 4, 5] were passed in (with 1 at the bottom and 5 at the top), the Stack would be changed to [2, 1, 3, 4, 5] (with 2 at the bottom and 5 at the top) after this method was called on it. You may only use one Queue as additional storage for this problem.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

NOTE: FEEL FREE TO ASK ANY DOUBTS IN THE COMMENT SECTION

  • HERE I DID NOT USED ANY QUEUES
  • I SWAPPED STACK VALUES TO REVERSE VALUS USING INDEX
  • IF YOU FIND THIS IS WRONG, PLEASE LET ME KNOW IN THE COMMENTS, I WILL EDIT CODE

CODE

Main.java

/* HERE i did not used any QUEUE
* I just swapped stack values at indexes
*/

import java.io.*;
// package for Stack class object
import java.util.*;

// Main Class which is public
public class Main
{
    // method to add array to stack
    static Stack<Integer> addArrayToStack(int[] array){
        // new stack for array values
        Stack<Integer> stack=new Stack<Integer>();

        // LOOP for all values in array
        for(int i=0;i<array.length;i++){
            // adding value to stack
            stack.add(array[i]);
        }
        // returning new stack
        return stack;
    }

    // Method to reverse Bottom half of stack
    static Stack<Integer> reverseBottomHalf(Stack<Integer> stack){
        // starting and half index of stack
        int start=0;
        int half=stack.size()/2;
        // LOOP for half of the hlaf values of stack
        for(int i=0;i<half/2;i++){
            // getting value at index i
            int newFirst=stack.get(i);
            // getting value at index from last of half
            int newLast=stack.get(half-i-1);
            // setting new values to stack
            stack.set(i,newLast);
            stack.set(half-i-1,newFirst);
        }
        // retuning updated stack
        return stack;
    }
    // main method to test above methods
   public static void main(String[] args) {
        // array1 for stack 1
       int[] arr1=new int[]{1,2,3,4,5};
       // array2 for stack 2
       int[] arr2=new int[]{1,2,3,4,5,6,7,8,9,10};

        // new stack for above two array values
        // calling method to insert array values to stacks
       Stack<Integer> stack1 = addArrayToStack(arr1);
       Stack<Integer> stack2 = addArrayToStack(arr2);

        // printing stack without reverse
       System.out.println("Stack1 without reverse: "+stack1);
       // calling method to reverse bottom half of stack1
       stack1 = reverseBottomHalf(stack1);
       // printing stack after reverse
       System.out.println("Stack1 after reverse: "+stack1);

        // printing stack without reverse
       System.out.println("Stack2 without reverse: "+stack2);
       // calling method to reverse bottom half of stack2
       stack2 = reverseBottomHalf(stack2);
       // printing stack after reverse
       System.out.println("Stack2 after reverse: "+stack2);
   }
}

OUTPUT in CONSOLE

Stackl without reverse: [1, 2, 3, 4, 5] Stacki after reverse: [2, 1, 3, 4, 5] Stack2 without reverse: [1, 2, 3, 4, 5, 6, 7, 8

CODE in EDITOR

Main.java 1- /* HERE i did not used any QUEUE *I just swapped stack values at indexes 3 * 6 7 import java.io.*; // package fostack.set(i,newLast); stack.set(half-i-1, newFirst); // retuning updated stack return stack; // main method to test above met

PLEASE HELP ME by GIVING an UP VOTE

Thank YOU :-)

Add a comment
Know the answer?
Add Answer to:
Java Program Write a method called reverseBottomHalf that accepts a Stack of integers as a parameter...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a method called reverseFirstK that accepts an integer k and a queue of integers as...

    Write a method called reverseFirstK that accepts an integer k and a queue of integers as parameters and reverses the order of the first k elements of the queue, leaving the other elements in the same relative order. For example, if a queue named q stores [10, 20 30, 40, 50, 60, 70, 80, 90], the call of reverseFirstK (4, q):should change the queue to store [40, 30 20, 10, 50, 60, 70, 80, 90]. If k is 0 or...

  • Page ot 9 2. Stacks/Queues: Write a method splitStack that takes a stack of integers as...

    Page ot 9 2. Stacks/Queues: Write a method splitStack that takes a stack of integers as a parameter and splits it into negatives and non-negatives. The numbers in the stack should be rearranged so that all the negatives appear on the bottom of the stack and all the non-negatives appear on the top. In other words, if after this method is called you were to pop numbers off the stack, you would first get all the nonnegative numbers and then...

  • java /* Q2 (10 pts): Write a method called method that accepts an integer parameter *...

    java /* Q2 (10 pts): Write a method called method that accepts an integer parameter * * * * and returns a sum of the first n terms of the sequence. * In other words, the method should generate the following sequence: 1 + 1/2 + 1/3 + 1/4 + ... 1/n * For example, method2(2) will return 1.5 since 1+1/2 = 1.5 * method2 (15) will return 3.3182289932289937 * You may assume that the parameter n is nonnegative. */...

  • I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that a...

    I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...

  • ****WRITE A JAVA PROGRAM THAT : Write a method named stretch that accepts an array of...

    ****WRITE A JAVA PROGRAM THAT : Write a method named stretch that accepts an array of integers (that the user inputs) as a parameter and returns a new array twice as large as the original, replacing every integer from the original array with a pair of integers, each half the original. If a number in the original array is odd, then the first number in the new pair should be one higher than the second so that the sum equals...

  • Write a method named factorial that accepts an integer n as a parameter and returns the...

    Write a method named factorial that accepts an integer n as a parameter and returns the factorial of n, or n!. A factorial of an integer is defined as the product of all integers from 1 through that integer inclusive. For example, the call of factorial(4) should return 1 2 3 4, or 24. The factorial of 0 and 1 are defined to be 1. You may assume that the value passed is non-negative and that its factorial can fit...

  • Write a complete Java program called MethodTest according to the following guidelines. The main method hard-codes...

    Write a complete Java program called MethodTest according to the following guidelines. The main method hard-codes three integer values into the first three positions of an array of integers calls a method you write called doubleEachValue that takes an array of integers (the one whose values you hard-coded in main) as its only argument and returns an ArrayList of integers, with each value in returned ArrayList equal to double the correspondingly indexed value in the array that is passed in...

  • Submit Chapter7.java with four (4) public static methods as follows: A. Write a method called mostCommon...

    Submit Chapter7.java with four (4) public static methods as follows: A. Write a method called mostCommon that accepts an array of integers as its only parameter, and returns the int that occurs most frequently. Break ties by returning the lower value For example, {1,2,2,3,4,4} would return 2 as the most common int. B. Write mostCommon (same as above) that accepts an array of doubles, and returns the double that occurs most frequently. Consider any double values that are within 0.1%...

  • Write a method maxOccurrences that accepts a list of integers as a parameter and returns the...

    Write a method maxOccurrences that accepts a list of integers as a parameter and returns the number of times the most frequently occurring integer (the “mode”) occurs in the list. Solve this problem using a map as auxiliary storage. If the list is empty, return 0.

  • write in java split() A static method that takes as input parameter a ThingArrayQueue called inputQ,...

    write in java split() A static method that takes as input parameter a ThingArrayQueue called inputQ, that includes things with positive number attributes. The method returns an array of two ThingArrayQueues as output where the first queue includes all things with even values from input and the second queue includes all things with odd values from inputQ. Zero is considered an even number. The input queue should be empty after calling this method. *Queue has a mixture of things with...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT