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

Write a method from the client perspective to display the contents of a stack in

ID: 3709964 • Letter: W

Question

Write a method from the client perspective to display the contents of a stack in the order in which they were added- one element per line.

For example, if you push a, b, and c onto a stack, and invoke the method, the method would print a, b, c.

The method header is: public void printInAddOrder(StackInterface<T> stack)

Notes:

The stack sent in as a parameter must not be destroyed.

Or, if you destroy it, you must recreate it.

The methods of StackInterface are defined here: https://docs.oracle.com/javase/7/docs/api/java/util/Stack.html

For full credit, your method should not be recursive.

If necessary, you can create a new object of type LinkedStack. For full credit, do notuse other kinds of data structures (arrays, lists, etc.) in your solution. (The purpose of this question is to practice using stacks!)

Explanation / Answer

As per your requirement the below one is solution please follow it step by step

import java.util.*;

public class HomeworkW12Driver {

public static void main(String[] args) {

// Q16 print in order

StackInterface s = new LinkedStack();

System.out.println("**********Q16");

System.out.println("Should print cat, dog, hamster, zebra (one per line)");

s.push("cat");

s.push("dog");

s.push("hamster");

s.push("zebra");

printInAddOrder(s);

System.out.println();

// Q17 min on top

System.out.println("**********Q17");

ArrayStack<Integer> numStack = new ArrayStack<>();

addToMinStack(numStack, 8);

System.out.println("Top of stack should be 8");

numStack.display(); // NOTE: THIS RELIES ON YOUR DISPLAY METHOD FROM Q19

addToMinStack(numStack, 4);

System.out.println("Top of stack should be 4");

numStack.display();

addToMinStack(numStack, 3);

System.out.println("Top of stack should be 3");

numStack.display();

addToMinStack(numStack, 6);

System.out.println("Top of stack should be 3");

numStack.display();

addToMinStack(numStack, 7);

System.out.println("Top of stack should be 3");

numStack.display();

addToMinStack(numStack, 1);

System.out.println("Top of stack should be 1");

numStack.display();

// Q18 fibonacci numbers

System.out.println("**********Q18");

int fibNum = 9;

System.out.println("Fib recursive should be the same as Fib using stacks (" + fibonacciRecursive(fibNum) + "): "

+ fibonacciStack(fibNum));

System.out.println();

// Q19 display methods

System.out.println("**********Q19");

ArrayStack displayArrayStack = new ArrayStack();

System.out.println("Should give a message that the stack is empty.");

displayArrayStack.display();

displayArrayStack.push("California");

displayArrayStack.push("Florida");

displayArrayStack.push("Georgia");

displayArrayStack.push("Hawaii");

System.out.println("Should print BOTTOM California Florida Georgia Hawaii TOP");

displayArrayStack.display();

System.out.println();

System.out.println("**********Q20");

LinkedStack displayLinkedStack = new LinkedStack();

System.out.println("Should give a message that the stack is empty.");

displayLinkedStack.display();

displayLinkedStack.push("Alaska");

displayLinkedStack.push("Delaware");

displayLinkedStack.push("Iowa");

displayLinkedStack.push("New York");

System.out.println("Should print BOTTOM Alaska Delaware Iowa New York TOP");

displayLinkedStack.display();

// QEC1 peek2 in LinkedStack

/*

* System.out.println("**********EC1"); LinkedStack peekStackLinked = new

* LinkedStack(); System.out.println("Should print null/throw exception: " +

* peekStackLinked.peek2()); peekStackLinked.push("hello");

* System.out.println("Should print null/throw exception: " +

* peekStackLinked.peek2()); peekStackLinked.push("goodbye");

* System.out.println("Should print hello: " + peekStackLinked.peek2());

* peekStackLinked.push("and good night");

* System.out.println("Should print goodbye: " + peekStackLinked.peek2());

* System.out.println();

*/

// QEC2 peek2 in ArrayStack

/*

* System.out.println("**********QEC2"); ArrayStack peekStackArray = new

* ArrayStack(); System.out.println("Should print null/throw exception: " +

* peekStackArray.peek2()); peekStackArray.push("hello");

* System.out.println("Should print null/throw exception: " +

* peekStackArray.peek2()); peekStackArray.push("goodbye");

* System.out.println("Should print hello: " + peekStackArray.peek2());

* peekStackArray.push("and good night");

* System.out.println("Should print goodbye: " + peekStackArray.peek2());

* System.out.println();

*/

}

public static void printInAddOrder(StackInterface stack) {

if (stack == null || stack.isEmpty()) {

return;

}

Object object = null;

StackInterface copy = new LinkedStack<>();

// This will create a nw stak which will add element in copy stack in reverse

// order of input stack which will give us insertion order.

while (!stack.isEmpty()) {

object = stack.pop();

copy.push(object);

}

while (!copy.isEmpty()) {

object = copy.pop();

System.out.println(object);

stack.push(object);

}

}

public static void addToMinStack(StackInterface<Integer> stack, int newVal) {

// YOUR CODE HERE!

}

public static int fibonacciRecursive(int n) {

if (n == 1 || n == 2) {

return 1;

} else {

return fibonacciRecursive(n - 1) + fibonacciRecursive(n - 2);

}

}

public static int fibonacciStack(int n) {

// YOUR CODE HERE!

return 0;

}

}

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