Question: Question 4 (7 + 12 = 19 marks) Write a complete Java program that provides a method named sumPairs() that takes a stack of integers

 Question 4 (7 + 12 = 19 marks) Write a complete

Question 4 (7 + 12 = 19 marks) 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, I]t op Then the stack should store the following values after calling the method sum Pairs(): After: botton 5, 11, 9, 5, 3)t op The number of elements and their values should be read from the user. Sample Output: Enter # of elements: 9 5 Before: bott on 5, 5, 6, 6, 3, 3, 2, 2, 1]t op After: botton[ 5, 11, 9, 5, 3]t op

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!