Question: I need help for answering the write up questions import java.util.EmptyStackException; public class ArrayStack implements DStack { // (alternate version, using an array) private double[]

I need help for answering the write up questions

import java.util.EmptyStackException;

public class ArrayStack implements DStack { // (alternate version, using an array)

private double[] items = new double[10]; // Holds the items on the stack.

private int top = 0; // The number of items currently on the stack.

/** * Add N to the top of the stack. */ public void push( double N ) { if (top == items.length) { // The array is full, so make a new, larger array and // copy the current stack items into it. (Note that // java.util.Arrays must be imported.) double[] b=new double[2*items.length];

for(int i=0;i

items=b; } items[top] = N; // Put N in next available spot. top++; // Number of items goes up by one. }

/** * Remove the top item from the stack, and return it. * Throws an IllegalStateException if the stack is empty when * this method is called. */ public double pop() { if ( top == 0 ) throw new EmptyStackException(); double topItem = items[top - 1]; // Top item in the stack. top--; // Number of items on the stack goes down by one. return topItem; }

/** * Returns true if the stack is empty. Returns false * if there are one or more items on the stack. */ public boolean isEmpty() { return (top == 0); }

@Override public double peek() { // TODO Auto-generated method stub if ( top == 0 ) throw new EmptyStackException(); double topItem = items[top - 1]; // Top item in the stack. return topItem; }

} // end class Stack

--------------------------------------------------------

import java.util.EmptyStackException;

public class ListStack implements DStack{

/** * An object of type Node holds one of the items in the linked list * that represents the stack. */ private static class Node { double item; Node next; }

private Node top; // Pointer to the Node that is at the top of // of the stack. If top == null, then the // stack is empty.

/** * Add N to the top of the stack. */ public void push( double N ) { Node newTop; // A Node to hold the new item. newTop = new Node(); newTop.item = N; // Store N in the new Node. newTop.next = top; // The new Node points to the old top. top = newTop; // The new item is now on top. }

/** * Remove the top item from the stack, and return it. * Throws an IllegalStateException if the stack is empty when * this method is called. */ public double pop() { if ( top == null ) throw new EmptyStackException(); double topItem = top.item; // The item that is being popped. top = top.next; // The previous second item is now on top. return topItem; }

/** * Returns true if the stack is empty. Returns false * if there are one or more items on the stack. */ public boolean isEmpty() { return (top == null); }

@Override public double peek() { // TODO Auto-generated method stub if ( top == null ) throw new EmptyStackException(); double topItem = top.item; // The item that is being popped. return topItem; }

} // end class Stack

Write-Up Questions 1.Who and what did you find helpful for this project? 2.How did you test that your stack implementations were correct? 3.Other than java.util.EmptyStackException, did you use any classes from the Java framework or other class library? (You will get a low score on this project if you use a library to implement your stacks.) 4.Your array stacks start with a small array and double in size if they become full. For a .dat file with 1 million lines, how many times would this resizing occur? What about with 1 billion lines or 1 trillion lines (assuming the computer had enough memory)? Explain your answer. 5.Instead of a DStack interface, pretend you were given a fullyfunctional FIFO Queue class. How might you implement this project (i.e., simulate a Stack) with one or more instances of a FIFO Queue? 6.Write pseudocode for your push and pop operations. Assume your Queue class provides the operations enqueue, dequeue, isEmpty, and size. 7.Why would a stack implementation using a queue, as you described in the previous problem, be worse than your array and linked-list stack implementations?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!