Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please answer questions e, 4, 5, and 6 e. Why do we use more than one case in this switch statement for pushing new drawing

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedPlease answer questions e, 4, 5, and 6

e. Why do we use more than one case in this switch statement for pushing new drawing objects? (If you aren't sure, comment out case 0 and case 1, leaving only case 3 to push new operations, and run the project to see what's different). 4. What happens if you comment out the try-catch structure that surrounds the pop and peek operation? Try it and see run the simulation multiple times until you see the problem in the output. What happens without the try-catch structure? Why does this happen? Sinca. myOpStack. .add(DrawingOp e) boolean for (Dra o add (int index, DrawingOp element) void Syst o addAll(Collections? extends DrawingOp> c) boolean } o addAll(int index, Collection extends DrawingOp> c) boolean } o addElement(DrawingOp obj) void o capacity ( ) int oclear() void oclone () Object o contains (Object o) boolean o containsAll(Collection> c) boolean Operation{name=Char o copyInto (Object[] anArray) void Operation{name=Draw o elementAt(int index) new op on top of s DrawingOp g stack: o elements ( ) Enumeration o empty() boolean Operation{name=Char o ensureCapacity(int minCapacity) Operation{name=Drav void Operation{name=char o equals(Object o) boolean current op at top ofirstElement() DrawingOp g stack: ationUndo (run) There are many methods here, because Java's stack implementation inherits from another collection, Vector. If you scroll through this list, what do you notice about the operations that are specific to a Stack (such as push and pop)? 6. What methods are available for Java stack objects that really shouldn't be, given how a stack operates and what it is designed to do? * * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ 6 package operationundo; 7 8 import java.util. EmptyStackException; 9 import java.util. Stack; 10 11 *** 12 13 * @author clatulip 14 */ 15 public class DrawingUndoStackSimulation { 22 private Stack myOpStack; 17 18 19 F public DrawingUndoStackSimulation () { 20 myOpStack = new Stack(); 21 runSimulation(); 22 } 23 24 25 private Drawingop createNewOp() { 26 int opName = (int) (Math.random()*8); 27 DrawingOp op = null; 28 switch (opName) { 29 case 0: 30 op = new DrawingOp ("Draw Line"); 31 break; 32 case 1: 33 op = new DrawingOp("Draw Rectangle"); 34 break; 35 case 2: 36 op = new Drawingop ("Draw Oval"); 37 break; 38 case 3: 39 op = new Drawing0p ("Draw Curve"); 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 D 103 104 105 106 System.out.println("Peeked, current op at top of stack is:" + p.toString()); } catch (EmptyStackException e) { System.out.println("EXCEPTION: Stack is empty, can't peek."); } break; case 4: // pop try { DrawingOp q = myOpStack.pop(); System.out.println("UNDO! Popped, got this off stack:" + q.toString(); } catch (EmptyStackException e) { System.out.println("EXCEPTION: Stack is empty, can't pop."); } printStack(); break; case 5: // check size System.out.println("Stack size is: " + myOpStack.size()); break; } countDown--; } public void printStack() { System.out.println("\t Printing stack: "); // note that this collections-based for loop prints the collection, // but in bottom-to-top order. for (DrawingOpo : myOpStack) { System.out.println("\t\t" + o.toString()); } if (myOpStack.empty() System.out.println("\t\t ***Stack is empty.***"); } 108 109 110 111 112 113 114 115 }

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

Next Generation Databases NoSQLand Big Data

Authors: Guy Harrison

1st Edition

1484213300, 978-1484213308

More Books

Students also viewed these Databases questions