Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assume you have an array of integers. You want to organize it such that all numb

ID: 3833145 • Letter: A

Question

Assume you have an array of integers. You want to organize it such that all numbers that are smaller than -100 go to the bottom, numbers between -100 and +100 in the middle and numbers larger than 100 in the top.

Write a Java code that uses no more than three additional Stacks to solve the problem.

NB: You do not need to write the code for Stacks, you are using a Stack from the library with a name MAC286Stack that exposes the following interface: MAC286Stack() constructor, pop, push, isEmpty, size(), peek()).

Explanation / Answer

You have not provided the MAC286Stack interface. But i have used Java built in stack. All you need to do is change the change name from Stack to MAC286Stack from the below program.

PROGRAM CODE:

package util;

import java.util.Stack;

public class NumberOrganizer {

   public static void main(String[] args) {
       int array[] = {100,-294,-472,568, -9834, 9822, 829, 24, 54, -38, -27, 483, 22, 9, -3};
       Stack<Integer> mainStack = new Stack<>();
       Stack<Integer> middleNumbers = new Stack<>();
       Stack<Integer> topNumbers = new Stack<>();
       for(int i=0; i<array.length; i++)
       {
           if(array[i]<-100)
               mainStack.push(array[i]);
           else if(array[i]>=-100 && array[i]<=100)
               middleNumbers.push(array[i]);
           else topNumbers.push(array[i]);
       }
       while(!middleNumbers.isEmpty())
       {
           mainStack.push(middleNumbers.peek());
           middleNumbers.pop();
       }
       while(!topNumbers.isEmpty())
       {
           mainStack.push(topNumbers.peek());
           topNumbers.pop();
       }
       System.out.println(mainStack);
   }

}

OUTPUT:

[-294, -472, -9834, -3, 9, 22, -27, -38, 54, 24, 100, 483, 829, 9822, 568]

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote