Lab 5A: Stacks Q1 Fun with stacks 4 Points Consider a generic Stack class implemented using...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Lab 5A: Stacks Q1 Fun with stacks 4 Points Consider a generic Stack class implemented using a linked list with the following methods: public boolean isEmpty(), which returns true if and only if the stack is empty; public T pop() throws StackUnderflowException, which removes and returns the top element of the stack (if the stack is empty, it throws StackUnderflowException); public T peek () throws StackUnderflowException, which returns the top element of the stack (but does not remove it; it throws an exception if the stack is empty); public void push (T element), which pushes an element onto the stack Predict the output of each of the following code snippets Q1.1 1 Point A. B. Stack s = new Stack (); s.push(5); s.push(50); try{ What is the output? s.push(30); } catch (Exception e) { System.out.println("An exception was thrown"); } D. System.out.println(s.pop()); System.out.println(s.pop()); System.out.println(s.peek()); 5 50 30 50 5 30 C. 50 An exception was thrown 50 5 An exception was thrown O A OB OC OD Q1.2 1 Point A. B. Stack s = new Stack (); s.push(5); try{ What is the output? System.out.println(s.peek()); } catch (Exception e) { System.out.println("An exception was thrown"); } C. System.out.println(s.pop()); System.out.println(s.isEmpty()); s.push(10); s.push(43); s.push(s.pop()); 5 True 10 D. 5 False 10 5 True 43 OA OB OC D 5 True An exception was thrown Q1.3 1 Point Stack s new Stack (); try { B. A. What is the output? } catch (Exception e) { System.out.println("An exception was thrown"); } 10 5 s.push(5); s.push(10); C. while (!s.isEmpty()) System.out.println (s.peek()); D. } 10 10 10 ... // infinite loop. // nothing is output OA OB OC OD { 10 5 An exception was thrown Q1.4 1 Point Stack s = new Stack (); try { } s.push(5); s.push(9); s.push(3); s.push(s.pop() + s.pop()); System.out.println(s.peek()); } catch (Exception e) { System.out.println("An exception was thrown"); } What is the output? O 12 O 14 O 3 An exception was thrown Q2 Stack Implementation of Postfix Operations 3 Points Postfix notation: operators are placed after operands. Terminology: Operands: numbers Operators: + -/* Study the example below: An operator acts on the two values to its left, where a value may be either a number in the original expression or result of a previous operator. Note the order of the operands (further left -> operator -> closer left). This matters to - and /. Infix Postfix 53 A / B (214) x 23 Study the algorithm to evaluate postfix expressions using a stack. 1. Scan the expression. 2. When we see an operand, push it onto the stack. 3. When we see an operator, pop two values off of the stack, and apply the operator to them, then push the result back onto the stack. 4. In the end, if we have exactly one value on the stack, that is our answer. See the worked example below for the expression: 5 4 - 7 * push(5) push(4) - sign encountered pop() 4 pop() 5 53- A B / 24 23 * push(1) 5-4 = 1 push(7) *sign encountered 7 pop() 1 push(7) 1*7=7 Ans: 7 Q2.1 1 Point Evaluate the expression using a stack: 5 7 + 6 2 Enter your answer here Save Answer Q2.2 1 Point Evaluate the expression using a stack: 4 5 7 2 + Enter your answer here Save Answer Q2.3 1 Point Evaluate the expression using a stack: 4 2 3 5 1 Too many operands-stack overflow Not enough operands-stack underflow Save Answer I + * * + * Q3 Implementing Stack using Linked List 8 Points Please download the Stack starter code. In this section, you will implement a generic Stack class implemented using a linked list. Refer to the LLNode class in the starter code. Note that both class variables are private so you cannot access them directly. You must use the get and set methods defined for you. Also, assume that the StackUnderflowException class has been defined such that it inherits Java's Exception class. Your task is to implement four methods in the generic class LinkedListStack . public class Linked ListStack { private LLNode head; // constructor public Linked ListStack() { head = null; } Q3.1 1 Point public boolean isEmpty() { // TODO: return true if the stack is empty, false otherwise // Hint: You do not need more than 1 line of code Enter your answer here Save Answer Q3.2 2 Points // head of linked list, also stack top pointer public void push(T element) { // TODO: push an element to the stack Enter your answer here Save Answer Q3.3 2 Points public T peek () throws StackUnderflowException { // TODO: return the top element of the stack (but do NOT remove it). // Throw StackUnderflowException if stack is empty Enter your answer here Save Answer Q3.4 3 Points public T pop() throws StackUnderflowException { // TODO: remove and return the top element of the stack // Throw StackUnderflowException if stack is empty Enter your answer here Save Answer Lab 5A: Stacks Q1 Fun with stacks 4 Points Consider a generic Stack class implemented using a linked list with the following methods: public boolean isEmpty(), which returns true if and only if the stack is empty; public T pop() throws StackUnderflowException, which removes and returns the top element of the stack (if the stack is empty, it throws StackUnderflowException); public T peek () throws StackUnderflowException, which returns the top element of the stack (but does not remove it; it throws an exception if the stack is empty); public void push (T element), which pushes an element onto the stack Predict the output of each of the following code snippets Q1.1 1 Point A. B. Stack s = new Stack (); s.push(5); s.push(50); try{ What is the output? s.push(30); } catch (Exception e) { System.out.println("An exception was thrown"); } D. System.out.println(s.pop()); System.out.println(s.pop()); System.out.println(s.peek()); 5 50 30 50 5 30 C. 50 An exception was thrown 50 5 An exception was thrown O A OB OC OD Q1.2 1 Point A. B. Stack s = new Stack (); s.push(5); try{ What is the output? System.out.println(s.peek()); } catch (Exception e) { System.out.println("An exception was thrown"); } C. System.out.println(s.pop()); System.out.println(s.isEmpty()); s.push(10); s.push(43); s.push(s.pop()); 5 True 10 D. 5 False 10 5 True 43 OA OB OC D 5 True An exception was thrown Q1.3 1 Point Stack s new Stack (); try { B. A. What is the output? } catch (Exception e) { System.out.println("An exception was thrown"); } 10 5 s.push(5); s.push(10); C. while (!s.isEmpty()) System.out.println (s.peek()); D. } 10 10 10 ... // infinite loop. // nothing is output OA OB OC OD { 10 5 An exception was thrown Q1.4 1 Point Stack s = new Stack (); try { } s.push(5); s.push(9); s.push(3); s.push(s.pop() + s.pop()); System.out.println(s.peek()); } catch (Exception e) { System.out.println("An exception was thrown"); } What is the output? O 12 O 14 O 3 An exception was thrown Q2 Stack Implementation of Postfix Operations 3 Points Postfix notation: operators are placed after operands. Terminology: Operands: numbers Operators: + -/* Study the example below: An operator acts on the two values to its left, where a value may be either a number in the original expression or result of a previous operator. Note the order of the operands (further left -> operator -> closer left). This matters to - and /. Infix Postfix 53 A / B (214) x 23 Study the algorithm to evaluate postfix expressions using a stack. 1. Scan the expression. 2. When we see an operand, push it onto the stack. 3. When we see an operator, pop two values off of the stack, and apply the operator to them, then push the result back onto the stack. 4. In the end, if we have exactly one value on the stack, that is our answer. See the worked example below for the expression: 5 4 - 7 * push(5) push(4) - sign encountered pop() 4 pop() 5 53- A B / 24 23 * push(1) 5-4 = 1 push(7) *sign encountered 7 pop() 1 push(7) 1*7=7 Ans: 7 Q2.1 1 Point Evaluate the expression using a stack: 5 7 + 6 2 Enter your answer here Save Answer Q2.2 1 Point Evaluate the expression using a stack: 4 5 7 2 + Enter your answer here Save Answer Q2.3 1 Point Evaluate the expression using a stack: 4 2 3 5 1 Too many operands-stack overflow Not enough operands-stack underflow Save Answer I + * * + * Q3 Implementing Stack using Linked List 8 Points Please download the Stack starter code. In this section, you will implement a generic Stack class implemented using a linked list. Refer to the LLNode class in the starter code. Note that both class variables are private so you cannot access them directly. You must use the get and set methods defined for you. Also, assume that the StackUnderflowException class has been defined such that it inherits Java's Exception class. Your task is to implement four methods in the generic class LinkedListStack . public class Linked ListStack { private LLNode head; // constructor public Linked ListStack() { head = null; } Q3.1 1 Point public boolean isEmpty() { // TODO: return true if the stack is empty, false otherwise // Hint: You do not need more than 1 line of code Enter your answer here Save Answer Q3.2 2 Points // head of linked list, also stack top pointer public void push(T element) { // TODO: push an element to the stack Enter your answer here Save Answer Q3.3 2 Points public T peek () throws StackUnderflowException { // TODO: return the top element of the stack (but do NOT remove it). // Throw StackUnderflowException if stack is empty Enter your answer here Save Answer Q3.4 3 Points public T pop() throws StackUnderflowException { // TODO: remove and return the top element of the stack // Throw StackUnderflowException if stack is empty Enter your answer here Save Answer
Expert Answer:
Related Book For
Java How To Program Late Objects Version
ISBN: 9780136123712
8th Edition
Authors: Paul Deitel, Deitel & Associates
Posted Date:
Students also viewed these programming questions
-
can someone solve this Modern workstations typically have memory systems that incorporate two or three levels of caching. Explain why they are designed like this. [4 marks] In order to investigate...
-
answer the question clearly (a) What is the von Neumann bottleneck and why can it limit performance on today's RISC machines? [4 marks] (b) What computer architecture techniques are used to mitigate...
-
A. Discuss the following conceptual model/framework of Orem's Self-Care Theory CONDITIONING FACTORS (12 MARKS) Orem's Self-Care Theory Conceptual Framework SELF-CARE AGENCY SELF-CARE DEFICIT NURSING...
-
Identify the accounting concept that was violated in each of the following situations. I. Pastel Paint Company purchased land two years ago at a price of $250.000. Because the value of the land has...
-
Are bonds part of Modern Portfolio Theory? Can a portfolio manager employ the security market line (SML) or the capital market line (CML) to bond returns? Is a beta measure is a proper measure of...
-
The journal entry for the purchase of $900 of inventory on account is as follows: DATE ACCOUNTS POST REF. DR. CR. a. Cost of Goods Sold 900 900 9 b. C. Accounts Payable Inventory Accounts Payable...
-
Pavin acquires all of Stablers outstanding shares on January 1, 2009, for $460,000 in cash. Of this amount, $30,000 was attributed to equipment with a 10-year remaining life and $40,000 was assigned...
-
https://rpc.cfainstitute.org/-/media/documents/article/industry-research/climate-data-in-investment-process.pdf Can you briefly summarize the intention and meaning of the CFA Report: Climate Data in...
-
For her daughter's university education, Carla Hackl has invested an inheritance in a fund paying 9.2% compounded quarterly. If ordinary annuity payments of $4750.00 per month are to be made out of...
-
1. Dr. Bill is an Audit partner at Deloittes Tyson's Corner office and was required to attend a conference in the California Bay Area due to one of his major clients, Microsoft, being affected by the...
-
Calculate the mass of carbon dioxide released when 255 g of copper carbonate [molar mass = 123.5 g/mol] is heated (assuming that the reaction goes to completion). The molar mass of CO2 is 44.0 g/mol.
-
A dose of 320 mg of acetaminophen is prescribed for a patient. If one teaspoon contains 5.0 ml, how many teaspoons of Children's Tylenol (80 mg of acetaminophen per 2.5 mL) must be administered?...
-
A. Thecompound dimethylamine , (CH 3 ) 2 NH ,is a weak base when dissolved in water. Write the K b expression for the weak baseequilibrium that occurs in an aqueous solutionof dimethylamine : Part...
-
Blenders Eyewear has received a request for a special order for 1 0 , 0 0 0 pairs of sunglasses for the Buffs homecoming game. The client will pay $ 2 7 per pair of sunglasses.Blenders costs are...
-
Consider the boiling points of 2-methylcyclohexanol, 1-methylcyclohexene, 3- methylcyclohexene and phosphoric acid. What is happening when we heat the mixture to 110C?
-
1. Calculate the unknown side in each of the following: a) sin 20 = d) sin 52 = b) cos 37 = c) tan 64 15 30 5 e) cos 10 = f) tan 25 = 32 18 J 22
-
On April 29, 2015, Auk Corporation acquires 100% of the outstanding stock of Amazon Corporation (E & P of $750,000) for $1.2 million. Amazon has assets with a fair market value of $1.4 million (basis...
-
Assuming that x = 5 and y = 1, what does each of the following statements display? a) System.out.printf("x = %d", x + 5); b) System.out.printf("Value of %d * %d is %d", x, y, (x * y)); c)...
-
In this problem, youll re-create the classic race of the tortoise and the hare. Youll use random-number generation to develop a simulation of this memorable event. Our contenders begin the race at...
-
Provide the inorder, preorder and postorder traversals of the binary search tree of Fig. 21.18. Fig. 21.18 18 11 19 28 40 49 71 32 44 69 72 83 97 92 99
-
a. How are specific audit objectives derived for the revenue cycle? b. State the specific audit objectives for credit sales transactions.
-
a. State the applicability of several control environment factors to revenue cycle assertions. b. What is the primary source document for each of the three major transaction classes in the manual...
-
For the billing function, identify: a. Several potential misstatements. b. Necessary controls to prevent or detect the misstatements identified in a. c. Potential tests of the controls identified in...
Study smarter with the SolutionInn App