import java.util.Arrays; import java.util.LinkedList; import java.util.Queue; import java.util.Stack;
/* * Practice Problem for Week 6: * * Look at the code and answer the questions included as comments. * Turn in your responses in a separate document (e.g. a .pdf or .doc). */
public class Stack_Q {
public static void main(String[] args) { // 1. What do the following 3 lines do? Stack stringStack = new Stack(); Queue initialQueue = new LinkedList(Arrays.asList("Computer", "Computers", "Fun", "Fantastic", "Science","Is", "Great", "Awesome")); Queue finalQueue = new LinkedList(); // 2. What does the line below do? stringStack.push(initialQueue.remove()); finalQueue.add(initialQueue.remove()); for(int i = 0; i < 3; i++) { initialQueue.add(initialQueue.remove()); stringStack.push(initialQueue.remove()); stringStack.push(initialQueue.remove()); initialQueue.add(finalQueue.remove()); finalQueue.add(initialQueue.remove()); finalQueue.add(stringStack.pop()); } // 3. Create graphical representations of the initialQueue // and the stringStack structures and contents. // You can ignore finalQueue, since LinkedLists come later. // (You may hand draw these or create them some other way) // Only consider the contents they contain at THIS point in the program. // 4. Explain what happens in the following loop for(int j = 0; j < finalQueue.size(); j++) { String temp = finalQueue.remove(); System.out.print(temp + " "); finalQueue.add(temp); } // 5. What is the value of (String) temp now? stringStack.pop(); stringStack.pop(); String toPrint = stringStack.pop(); // 6. What is the purpose of the following line? System.out.println(); System.out.println(toPrint); // 7. Create graphical representations of the initialQueue // and the stringStack structures and contents. // (You may hand draw these or create them some other way) // Only consider the contents they contain at THIS point in the program. } }