Question

JAVA PROGRAM: public class Stack { private DList mList; public Stack() { setList(new DList<>()); } private int indexOfTop() { return getList().isEmpty() ? -1 : getList().getSize() - 1; } public...

JAVA PROGRAM:

public class Stack {

private DList mList;

public Stack() { setList(new DList<>()); }

private int indexOfTop() { return getList().isEmpty() ? -1 : getList().getSize() - 1; }

public E peek() { return getList().get(indexOfTop()); }

public E pop() { return getList().remove(indexOfTop()); }

public Stack push(E pData) { getList().append(pData); return this; }

private DList getList() { return mList; }

private void setList(DList pList) { mList = pList;

}

Q1

Because Stack encapsulates an instance variable which is of the class DList, storing one or more objects of class E, there is a relationship between Stack and class E. Which type of relationship is it ?

a. Dependency

b. Aggregation

c. Generalization/Inheritance

d. Composition

Q2.

2. See Ex 19 in the second midterm exam, where we used a DList list to store the elements of the Stack data structure. What would be the worst case time complexities of the three standard Stack operations/methods: peek(), push(), and pop(), respectively ?

a. O(1), O(1), O(1)

b. O(1), O(2), O(2)

c. O(1), O(n), O(n)

d. O(lg n), O(lg n), O(lg n)

e. O(n), O(n), O(n)

f. O(n lg n), O(n lg n), O(n lg n)

g. O(lg n), O(n lg n), O(n lg n)

h. We cannot determine this without knowing what the class of E will be when a Stack object is instantiated

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Q 1) It is a has-A type of relationship. Stack class has a DList so it is a composition answer is d

Q2) Push pop operations in stack occur from the same side of the list, that is top. So length of the list wouldn't matter even in worst case scenario. Even the peek method is retrieving top most element without removing it. So even that is independent of length of list. Worst case of all three is O(1)

Answer is a

Add a comment
Know the answer?
Add Answer to:
JAVA PROGRAM: public class Stack { private DList mList; public Stack() { setList(new DList<>()); } private int indexOfTop() { return getList().isEmpty() ? -1 : getList().getSize() - 1; } public...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • e. public class Queue { // Uses the correct Stack class from ME2 Ex 19 private Stack mStack; public Queue() { setStack(new Stack()); } public Queue enqueue(E pData) { getStack().push(pData); return t...

    e. public class Queue { // Uses the correct Stack class from ME2 Ex 19 private Stack mStack; public Queue() { setStack(new Stack()); } public Queue enqueue(E pData) { getStack().push(pData); return this; } public E dequeue() { return getStack().pop(); } public E peek() { return getStack.peek(); } private Stack getStack() { return mStack; } private void setStack(Stack pStack) { mStack = pStack; } } f. public class Queue extends Stack { // Uses the correct Stack class from ME2 Ex...

  • stack.h template class Stack{ public: Stack(int max = 10); ~Stack() {delete [] stack;} bool isEmpty() const...

    stack.h template class Stack{ public: Stack(int max = 10); ~Stack() {delete [] stack;} bool isEmpty() const { return top == -1; } bool isFull() const { return top == MaxStackSize; } T peek() const; void push(const T& x); void pop(); private: int top; int MaxTop; T * stack; } source.cpp What is printed by the following program segment? Stack s; int n; s.push(4); s.push(6); s.push(8); while(!s.isEmpty()) { n = s.peek(); cout << n << ‘ ‘; s.pop(); } cout<< endl;...

  • Java - data structures Suppose that in the array-based stack, the array doubles in size after...

    Java - data structures Suppose that in the array-based stack, the array doubles in size after multiple push operations. But later on, fewer than half of the array’s locations might actually be used by the stack due to pop operations. Revise the implementation so that its array also can shrink in size as objects are removed from the stack. Accomplishing this task will require two new private methods, as follows: The first new method checks whether we should reduce the...

  • Java. Must not use Java API java.util.Stack /** A class of stacks whose entries are stored in an ...

    Java. Must not use Java API java.util.Stack /** A class of stacks whose entries are stored in an array. Implement all methods in ArrayStack class using resizable array strategy, i.e. usedoubleArray() Must throw StackException during exception events in methods:    peek(), pop(), ArrayStack(int initialCapacity) Do not change or add data fields Do not add new methods */ import java.util.Arrays; public class Arraystack«Т> implements Stack!nterface«T> private TI stack;// Array of stack entries private int topIndex; /7 Index of top entry private static...

  • Java. Must not use Java API java.util.Stack /** A class of stacks whose entries are stored...

    Java. Must not use Java API java.util.Stack /** A class of stacks whose entries are stored in an array. Implement all methods in ArrayStack class using resizable array strategy, i.e. usedoubleArray() Must throw StackException during exception events in methods:    peek(), pop(), ArrayStack(int initialCapacity) Do not change or add data fields Do not add new methods */ import java.util.Arrays; public class Arraystack«Т> implements Stack!nterface«T> private TI stack;// Array of stack entries private int topIndex; /7 Index of top entry private...

  • JUnit5 JAVA. Need help to make a unit test of my GenericStack.java code below. Feel free...

    JUnit5 JAVA. Need help to make a unit test of my GenericStack.java code below. Feel free to change the code below to fit the task better. thanks :) Assigment Create a new version of GenericStack (Have started on the code below) that uses an array instead of an ArrayList (this version should also be generic). Be sure to check the size of the array before adding a new item; - if the array becomes full, double the size of the...

  • how do I change my code to generic form *********************************************************************** public class UnboundedStackQueue { //question#3 }...

    how do I change my code to generic form *********************************************************************** public class UnboundedStackQueue { //question#3 } class Stack { Node head; int size; Stack() //default constructor { this.head=null; this.size=0; } //Input = data //Output = void (just adds value to list) // method pushes elements on stack // time: O(1) // space: O(1) public void push(int data) { Node node=new Node(data); node.next=head; head=node; size++; } //Input = none //Output = top of stack // method pops value from top of...

  • ) Consider Java's Stack class, and its five standard stack operations: push, pop, peek, isEmpty, and...

    ) Consider Java's Stack class, and its five standard stack operations: push, pop, peek, isEmpty, and clear. Complete the two unfinished methods. Do not modify any other parts of the class.               // Looks at the top two elements of the stack, and removes and returns the larger        // of the two elements from the stack, returning the other element to the stack.        // For example, if the stack, from the top, is 8 10 7 2...

  • JAVA Lab Create a class called ArrayBasedStack. Declare the following variables: • data: references an array...

    JAVA Lab Create a class called ArrayBasedStack. Declare the following variables: • data: references an array storing elements in the list • topOfStack: an int value representing the location of the stack top in the array • INITIAL_CAPACITY: the default capacity of the stack public class ArrayBasedStack <E> { private E[] data; private int topOfStack; private static final int INITIAL_CAPACITY = 5; } Add a constructor that will initialize the stack with a user-defined initial capacity. The top of the...

  • public class NumStack implements Comparable{ private Integer[] data; private int index; public NumStack(int cap){ data=new Integer[cap];...

    public class NumStack implements Comparable{ private Integer[] data; private int index; public NumStack(int cap){ data=new Integer[cap]; index =-1; } public boolean isEmpty(){ return index == -1; } public boolean isFull(){ return index==data.length -1; } public NumStack pop(){ if(!isEmpty()) data[index--]=null; return this; } public int size(){ return index+1; } public Integer top(){ if(isEmpty()) return null; return data[index]; } public NumStack push(int num){ if(index < data.length-1) data[++index]=num; return this; } public int compareTo(NumStack s){} } public int compareTo(NumStack s) compares two stack...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT