Question
Please help! I need help with these general questions about the code below (if you can't answer all of them help with what you can
Please help! I need help with these general questions about the code below (if you can't answer all of them help with what you can :) ) will rate!
1.
a) The CreateNewOp() method in DrawingUndoStackSimulation has a return type of DrawingOp. This method randomly creates and returns one of 8 different operations. In cases 4-7, a ColorOp object is created and added to the myOpStack, even though it is not a DrawingOp. How is this possible?
b) Now look at The runSimulation() method, Why do we use more than one case in this switch statement for pushing new drawing objects?
c) What happens if you remove 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?
d) Go to the printStack() method. Above the for loop, type `myOpStack, then type a period and see what methods NetBeans offers you for this stack. There are many methods here, because Javas 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)?
e) What methods are available for Java stack objects that really shouldnt be, given how a stack operates and what it is designed to do?
here is the given code that the questions are asking about:
/* * 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. */ package operationundo;
import java.util.EmptyStackException; import java.util.Stack;
/** * * @author clatulip */ public class DrawingUndoStackSimulation { private Stack myOpStack;
public DrawingUndoStackSimulation() { myOpStack = new Stack<>(); runSimulation(); }
private DrawingOp createNewOp() { int opName = (int)(Math.random()*8); DrawingOp op = null; switch (opName) { case 0: op = new DrawingOp("Draw Line"); break; case 1: op = new DrawingOp("Draw Rectangle"); break; case 2: op = new DrawingOp("Draw Oval"); break; case 3: op = new DrawingOp("Draw Curve"); break; case 4: op = new ColorOp("Change Line Color", "red"); break; case 5: op = new ColorOp("Change Line Color", "blue"); break; case 6: op = new ColorOp("Change Fill Color", "yellow"); break; case 7: op = new ColorOp("Change Fill Color", "black"); break; } return op; }
private void runSimulation() { System.out.println("***Running simulation***"); int countDown = 30; while (countDown > 0) { int randStackOp = (int)(Math.random()*6); switch (randStackOp) { case 0: case 1: case 2: // push DrawingOp o = createNewOp(); System.out.println(o.getName() + ". Pushed new op on top of stack"); myOpStack.push(o); printStack(); break; case 3: // peek try { DrawingOp p = myOpStack.peek(); 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 (DrawingOp o : myOpStack) { System.out.println("\t\t" + o.toString()); } if (myOpStack.empty()) System.out.println("\t\t ***Stack is empty.***"); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started