(java coding language) Write a sequence of code (application level) to perform e
ID: 3815654 • Letter: #
Question
(java coding language)
Write a sequence of code (application level) to perform each of the following operations. Assume myStack is an object of the class ArrayStack. You may call any of the public methods of ArrayStack.
a. Set secondElement to the second element from the top of myStack, leaving my stack without its original top two elements.
b. Set bottom equal to the bottom element in myStack, leaving myStack empty.
c. Set bottom equal to the bottom element in myStack, leaving myStack unchanged.
Explanation / Answer
queue.enqueue(third);
A ANS]
printQueue(myQueue);
B Ans]
printQueue(myQueue);
queue.enqueue(last);
return queue;
}
C Ans)
public class ArrayStack { static final String first = "first", second = "second", third = "third", last = "last"; private UseQueue(){}; public static LinkedUnbndQueue buildTestQueue(){ // Build a fresh queue to test on. LinkedUnbndQueue queue = new LinkedUnbndQueue(); queue.enqueue(first); queue.enqueue(second);queue.enqueue(third);
A ANS]
System.out.println("A"); printQueue(myQueue); try{ myQueue.dequeue(); myQueue.dequeue(); }catch(QueueUnderflowException que){ System.err.println(que.getMessage()); }printQueue(myQueue);
B Ans]
System.out.println(" B"); printQueue(myQueue); while(!myQueue.isEmpty()){ // keep going until queue is empty try{ bottom.setInfo(myQueue.dequeue()); }catch(QueueUnderflowException que){ // this will never happen because of the test condition } } System.out.printf("Bottom is: %s ", Bottom.getInfo());printQueue(myQueue);
queue.enqueue(last);
return queue;
}
C Ans)
myQueue = buildTestQueue(); LinkedUnbndQueue tempQueue; tempQueue = new LinkedUnbndQueue(); // temp queue System.out.println(" C."); printQueue(myQueue); while(!myQueue.isEmpty()){ try{ rear.setInfo(myQueue.dequeue()); tempQueue.enqueue(bottom.getInfo()); }catch(QueueUnderflowException que){ // this will never happen because of the test condition } } myQueue = tempQueue; System.out.printf("bottom is: %s ", bottom.getInfo()); printQueue(myQueue);Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.