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
  • In java please: Write a method that accepts as a parameter a queue of integers that...

    In java please: Write a method that accepts as a parameter a queue of integers that are already sorted by absolute value, and modifies it so that the integers are sorted normally. Only use a single stack as auxiliary storage. For example, if a queue variable named q stores the following elements: front {1, -2, 4, 5, -7, -9, -12, 28, -34} back Then the call of reorder(q); should modify it to store the following values: front {-34, -12, -9,...

  • 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 method called removeDuplicates that accepts a PriorityQueue of integers as a parameter and modifies...

    Write a method called removeDuplicates that accepts a PriorityQueue of integers as a parameter and modifies the queue’s state so that any element that is equal to another element in the queue is removed. For example, if the queue stores [7, 7, 8, 8, 8, 10, 45, 45], your method should modify the queue to store [7, 8, 10, 45]. You may use one stack or queue as auxiliary storage. Please also create a Main Program to test the code....

  • Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and...

    Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: o Write a method called cubeIt that accepts one integer parameter and returns the value raised to the third power as an integer. 2. Write a method called randomInRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. o Write a method called larger that accepts two double parameters and...

  • 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...

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