Question

Consider a generic Stack class implemented using linked list with the following methods: public boolean isEmpty(),...

Consider a generic Stack class implemented using linked list with the following methods:

  • public boolean isEmpty(), which returns true if and only if the stack is empty;

  • public T pop() throws StackUnderflowException, which removes and returns the top element of the stack (if the stack is empty, it throws StackUnderflowException);

  • public T peek() throws StackUnderflowException, which returns the top element of the stack (but does not remove it; it throws exception if stack is empty);

  • public void push(T element), which pushes an element onto the stack

Predict the output of each of the following code snippets

(a) Stack<Integer> s = new Stack<Integer>();

try{

s.push(5);

s.push(10);

System.out.println(s.peek());

System.out.println(s.pop());

s.push(20);

System.out.println(s.pop());

}
catch(Exception e) { System.out.print(“An exception was thrown”); }





(b) Stack<Integer> s = new Stack<Integer>();

try{

s.push(5);

s.push(50);

System.out.println(s.pop());

System.out.println(s.pop());

System.out.println(s.peek());

s.push(30);

}
catch(Exception e) { System.out.println(“An exception was thrown”); }





(c) Stack<Integer> s = new Stack<Integer>();

try{

s.push(5);

System.out.println(s.pop());

System.out.println(s.isEmpty());

s.push(10);

s.push(43);

s.push(s.pop());

System.out.println(s.peek());

} catch(Exception e) {

System.out.println(“An exception was thrown”);

}





(d) Stack<Integer> s = new Stack<Integer>();

try {

s.push(5);

s.push(10);

while(!s.isEmpty())

{

System.out.println(s.peek());

}

}
catch(Exception e) { System.out.println(“An exception was thrown”); }




Homework Answers

Answer #1

(a) Stack<Integer> s = new Stack<Integer>();
try{
s.push(5); // Stores value 5 on to stack. Now Stack contains: 5
s.push(10); // Stores value 10 on to stack. Now Stack contains: 5 10
System.out.println(s.peek()); // Prints element at top of the stack which is 10 => Prints value 10
System.out.println(s.pop()); // Prints element at top of the stack which is 10 and removes it from stack. Now stack contains: 5 => Prints value 10
s.push(20); // Now stack contains: 5 20
System.out.println(s.pop()); // Prints element at top of the stack which is 20 and removes it from stack. Now stack contains: 5 => Prints value 20
}
catch(Exception e) { System.out.print(“An exception was thrown”); }


Hence output will be:

10
10
20

______________________________________________________________________________________________________________________________________________________

(b) Stack<Integer> s = new Stack<Integer>();
try{
s.push(5); // Stores value 5 on to stack. Now Stack contains: 5
s.push(50); // Stores value 50 on to stack. Now Stack contains: 5 50
System.out.println(s.pop()); // Prints element at top of the stack which is 50 and removes it from stack. Now stack contains: 5 => Prints value 50
System.out.println(s.pop()); // Prints element at top of the stack which is 5 and removes it from stack. Now stack contains: EMPTY => Prints value 5
System.out.println(s.peek()); // As stack is empty Raises an exception and prints the exception
s.push(30);
}
catch(Exception e) { System.out.println(“An exception was thrown”); }


Hence output will be:
50
5
An exception was thrown

______________________________________________________________________________________________________________________________________________________

(c) Stack<Integer> s = new Stack<Integer>();
try{
s.push(5); // Stores value 5 on to stack. Now Stack contains: 5
System.out.println(s.pop()); // Prints element at top of the stack which is 5 and removes it from stack. Now stack contains: EMPTY => Prints value 5
System.out.println(s.isEmpty()); // As stack is empty prints true
s.push(10); // Stores value 10 on to stack. Now Stack contains: 10
s.push(43); // Stores value 43 on to stack. Now Stack contains: 10 43
s.push(s.pop()); // Pushes element present at top of stack which is 43 => Stack contains: 10 43
System.out.println(s.peek()); // Element present at top of the stack is 43 => Prints value 43
} catch(Exception e) {
System.out.println(“An exception was thrown”);
}


Hence output will be:
5
true
43

______________________________________________________________________________________________________________________________________________________

(d) Stack<Integer> s = new Stack<Integer>();
try {
s.push(5); // Stores value 5 on to stack. Now Stack contains: 5
s.push(10); // Stores value 10 on to stack. Now Stack contains: 5 10
while(!s.isEmpty())
{
System.out.println(s.peek()); // Prints element 10 which is at top of the stack infinite times
}
}
catch(Exception e) { System.out.println(“An exception was thrown”); }


Hence output will be:
10
10
10
.. Infinite Times

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
(JAVA) Why is my toString method not printing out the last node? It will not print...
(JAVA) Why is my toString method not printing out the last node? It will not print out "A" which is for Alice. Everything else is working just fine except for this. package driver; import exception.StackException; import stack.*; public class StackDriver { public static void main(String[] args) throws Exception { StackInterface<Painting> painting = new LinkedStack <Painting>(); try { System.out.println("Peeking at top of player stack.\n" + painting.peek()); }catch (StackException e) { System.out.println(e); System.out.println("Let's double check if it's empty: " + painting.isEmpty()); }...
Create a class StacktwoPopArr using Arrays, Change the pop function so that two elements get popped...
Create a class StacktwoPopArr using Arrays, Change the pop function so that two elements get popped everytime instead of the one. If there is only one element in the stack, just pop the one element and report that the stack is now empty. If stack is empty, it should just report that stack is empty. All other functions for StackArray remain the same. using java StackArray.java: public class StackArray {       private final int size = 20; //Size of...
class Car{ private String make; public Car(String make){ this.make = make; } public String toString(){ return...
class Car{ private String make; public Car(String make){ this.make = make; } public String toString(){ return make; } } class Node<T>{ public T data; public Node next; public Node(T data){ this.data = data; this.next = null; } } public class StackLinkedList<T>{ //Single field of type Node    //to represent the front of the stack //operation push() /* The push operation is equivalent to the inserting a node at the head of the list. */ public void push(T data){ } //operation...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {    public static final int DEFAULT_SIZE = 10;    private Object data[];    private int index; code 2 package test; import java.util.*; /* Class Node */ class Node { protected Object data; protected Node link; /* Constructor */ public Node() { link = null; data = 0; } /* Constructor */ public Node(Object d,Node n) { data = d; link = n; } /*...
For this part, you will write a PostfixCalculator class that has methods for processing each possible...
For this part, you will write a PostfixCalculator class that has methods for processing each possible input. You will write a Tester class that reads a line of input from the user, with each symbol separated by a space, and prints out the numeric value of the top of the stack. If the user specifies an incomplete expression, print out the top of the stack and print out a message saying that the stack contains more than one item. If...
Given main() complete the Stack class by writing the methods push() and pop(). The stack uses...
Given main() complete the Stack class by writing the methods push() and pop(). The stack uses an array of size 5 to store elements. The command Push followed by a positive number pushes the number onto the stack. The command Pop pops the top element from the stack. Entering -1 exits the program. Ex. If the input is Push 1 Push 2 Push 3 Push 4 Push 5 Pop -1 the output is Stack contents (top to bottom): 1 Stack...
In this code, I build a single-linked list using a node class that has been created....
In this code, I build a single-linked list using a node class that has been created. How could I change this code to take data of type T, rather than int. (PS: ignore the fact that IOHelper.getInt won't work for the type T... ie second half of main). Here's my code right now: public class SLList { public SLNode head = null; public SLNode tail = null; public void add(int a) {// add() method present for testing purposes SLNode newNode...
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue...
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue class . Call your class Queue. it must be a template class. public class Queue { } I have put a driver program in the module . It is called CircularQueue.java This driver program should then run with your Queue class (no modifications allowed to the driver program). Your Queue class should have at least the following methods: one or more constructors, enqueue, dequeue,...
import java.util.ArrayList; /* Rules:         1. Allow Tester to iterate through all nodes using the...
import java.util.ArrayList; /* Rules:         1. Allow Tester to iterate through all nodes using the in-order traversal as the default.             This means, in Tester the following code should work for an instance of this class             called bst that is storing Student objects for the data:                 BinarySearchTree_Lab08<String> bst = new BinarySearchTree_Lab08<String>();                 bst.add("Man");       bst.add("Soda");   bst.add("Flag");                 bst.add("Home");   bst.add("Today");   bst.add("Jack");                ...
Code in JAVA The requirements are as follows: The input will be in a text file...
Code in JAVA The requirements are as follows: The input will be in a text file whose name is given by arg[0] of main(). It will contain a fully-parenthesized infix expression containing only: "(", ")", "+", "-" and integers. Need help on the main and fixing the Queue. //Input: ( ( 1 + 2 ) - ( ( 3 - 4 ) + ( 7 - 2 ) ) ) ( ( 1 + 2 ) - ( 3 -...