Question: This is the same version of IntStack that we discussed in class. In this version, items are added to the stack from low memory to

This is the same version of IntStack that we discussed in class. In this version, items are added to the stack from low memory to high memory.

Your task is to rewrite/modify IntStack so that items are stored in memory from high memory to low memory:

  • For example, if the array for the stack is of size 5, the first item pushed will be stored in stack[4]; the last item that can be stored will be stored in stack[0].

Make changes as necessary to IntStack so that the methods (push()/pop()/ stackFull()/stackEmpty()/printStack()) all operate correctly with this new storage scheme.

// IntStack.java // Integer stack.

public class IntStack { // Constructor. public IntStack(int size) { stack = new int[size]; top = 0; } // Pop method. public int pop() { top--; return stack[top]; } // Push method. public void push(int nr) { stack[top] = nr; top++; } // Stack full method. public boolean stackFull() { if(top == stack.length) return true; else return false; } // Stack empty method. public boolean stackEmpty() { if(top == 0) return true; else return false; } // Print stack method. public void printStack() { for(int ctr = 0; ctr < top; ctr++) { System.out.println("Stack[" + ctr + "] = " + stack[ctr]); } }

// Instance variables. private int[] stack; private int top;

}

// IntStackTest.java // Jim Jerkofsky 07/06/20 // Menu-driven program to test IntStack class. import java.util.Scanner;

public class IntStackTest { public static void main(String[] args) { // Set up Scanner object for keyboard input. Scanner keyboard = new Scanner(System.in); // Input size of stack desired. System.out.print("Size of stack: "); int limit = keyboard.nextInt(); // Set up IntStack object. IntStack stk = new IntStack(limit); // Menu for testing IntStack object. boolean flag = true; while(flag) { int result = 0; // Print menu. System.out.println(" "); System.out.println("1) Push"); System.out.println("2) Pop"); System.out.println("3) Print stack"); System.out.println("0) Exit menu"); System.out.print("Selection: "); result = keyboard.nextInt(); switch(result) { //Push an item onto stack. case 1: if(stk.stackFull()) { System.out.println(" Stack is full."); } else { System.out.print(" Item to push: "); int nr = keyboard.nextInt(); stk.push(nr); } break; // Pop an item off stack. case 2: if(stk.stackEmpty()) { System.out.println(" Stack is empty."); } else { int nr = stk.pop(); System.out.println(" Item popped: " + nr); } break; // Print current contents of stack. case 3: if(stk.stackEmpty()) System.out.println(" Stack is empty."); else { System.out.println(" Current contents of stack:"); stk.printStack(); } break; // Exit menu. case 0: flag = false; break; // Invalid menu selection. default: System.out.println(" Invalid menu selection."); break; } } } }

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!