Question: Write a complete Java program that provides a method named sumpairs() that takes a stack of integers as a parameter, replaces every 2 consecutive elements
Write a complete Java program that provides a method named sumpairs() that takes a stack of integers as a parameter, replaces every 2 consecutive elements (starting from the top) with their sum and returns the stack containing the sum values only. If the number of elements is odd, the last element in the stack should be kept as it is. For example, suppose a stack storing the following elements: Before: botton[ 5, 5, 6, 6, 3, 3, 2, 2, 1]top Then the stack should store the following values after calling the method sumpairs(): Aft er: botton[ , 11, 9, 5, 3]t op The number of elements and their values should be read from the user. Sample Output: Ent er # of elements: 9 5 5 6 2 1 Before: bott on 5, 5, 6, 6, 3, 3, 2, 2, 1) top After: botton 5, 11, 9, 5, 3]t op
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
