Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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. } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

what is the most common cause of preterm birth in twin pregnancies?

Answered: 1 week ago

Question

Which diagnostic test is most commonly used to confirm PROM?

Answered: 1 week ago

Question

What is the hallmark clinical feature of a molar pregnancy?

Answered: 1 week ago