All questions are in Java: 1. explain the rationale for returning a boolean valu
ID: 3875910 • Letter: A
Question
All questions are in Java:
1. explain the rationale for returning a boolean value from the addEntry bag operation
2. explain why the set ADT does not need a getFrequencyOf operation
3. Draw the resultant stack after the following operations. Label the top entry of your stack. push(X), push(Q), push(Y), peek(), pop(), push(T), peek()
4. Convert the following infix expression to a postfix expression w * (x + y) / z
6. In an array based implementation of a stack ADT, explain why is it a bad idea to use first location of the array to reference the top of a stack
7. draw the contents of a queue after the following statements execute. Clearly label the front and back of the queue.
QueueInterface <String> bankLine = new LinkedQueue <>();
bankLine.enqueue("John");
bankLine.enqueue("Mathew");
String next = bankLine. dequeue();
next = bankLine.dequeue();
bankLine.enqueue("Drew");
bankLine.enqueue("Heather");
next=bankLine.dequeue();
bankLine.enqueue("David");
next= bankLine.dequeue();
8. Draw the contents of a deque after the following statements execute. Clearly label the front and back of the deque.
DequeInterface<String> waitingLine = new LinkedDeque<>();
waitingLine.addToBack("Adam");
waitingLine.addToFront("Rudy");
waitingLine.addToBack("Larry");
waitingLine.addToBack("Sam");
waitingLine.addToFront("Jack");
String name = waitingLine.getFront();
name=getFront();
Explanation / Answer
1) The boolean value is returned to indicate if the value was successfully added to the bag. A false could probably be returned whe there was not enough space to add an entry into the bag.
2)Since each value is unique and no duplicates allowed in a set, they is only 1 occurence of an item if it exists. So there is no need to count the frequency and hence no need for getFrequencyOf() operation
3)The stack contents for each of the operations shown below
push(X): stack is {X [top]}
push(Q): stack is {X Q[top]}
push(Y): stack is {X Q Y[top]}
peek(): just returns top of stack and no change in stack
pop(): stack is {X Q [top]}
push(T): stack is {X Q T[top]}
peek(): just returns top of stack and no change in stack
So the final stack contents are {X Q T[top]} with the top of stack being T
4) The postfix expression is w x y + * z /
6) In a stack new elements are always the recent element and on top. If in the array, the 0th location is used to represent the top of stack, then every time a new element is added then rest of elements needed to shift one location to make room for the new top. This leads to added execution time . To reduce the time, we can add the new element at top to end of the array so it operates at constant time.
7) The contents of queue for each line of code is shown
Queue contents: [front] John [rear] //bankLine.enqueue("John");
Queue contents: [front] John Mathew[rear] // bankLine.enqueue("Mathew");
Queue contents: [front] Mathew [rear] // String next = bankLine. dequeue();
Queue contents: [front] [rear] //next = bankLine.dequeue();
Queue contents: [front] Drew [rear] // bankLine.enqueue("Drew");
Queue contents: [front] Drew Heather [rear] bankLine.enqueue("Heather");
Queue contents: [front] Heather [rear] //next=bankLine.dequeue();
Queue contents: [front] Heather David [rear] bankLine.enqueue("David");
Queue contents: [front] David [rear] next= bankLine.dequeue();
The final queue contains a since String David
8)Contents of deque for each of line of code is shown below
Deque contents: [front] Adam [back] //waitingLine.addToBack("Adam");
Deque contents: [front] Rudy Adam [back] //waitingLine.addToFront("Rudy");
Deque contents: [front] Rudy Adam Larry [back] //waitingLine.addToBack("Larry");
Deque contents: [front] Rudy Adam Larry Sam[back] //waitingLine.addToBack("Sam");
Deque contents: [front] Jack Rudy Adam Larry Sam[back] //waitingLine.addToFront("Jack");
String name = waitingLine.getFront();
name is assigned the 1st element in the deque, i.e. Jack