Question: Program Specification: A Stack is a simple container that is initially empty and need only support three simple operations: isEmpty - reports true if the

Program Specification: A Stack is a simple container that is initially empty and need only support three simple operations: isEmpty - reports true if the stack contains no values, otherwise reports false. push value - adds value onto the stack. pop - removes the most currently pushed value on the stack FIFO. You are to extend your DynArray class to implement a stack which can hold double values and adheres to the following: Mandatory Instance methods: public int size() // returns the number of values which are currently on the stack public boolean isEmpty() // returns true only if there are no values on the stack public void push(double value) // add the specified value onto the stack public double pop() // if the stack is not empty, // remove and returns the most currently pushd value on the stack // otherwise, // returns Double.NaN public void stackDump() // print all of the values currenty on the stack, in the order that // they would be popd off of the stack A Queue is a simple container that is initially empty and need only support three simple operations: isEmpty - reports true if the queue contains no values, otherwise reports false. que value - adds value into the queue. deQue - removes the least currently queed value in the queue FILO. You are to extend your DynArray class to implement a queue which can hold double values and adheres to the following: Mandatory Instance methods: public int size() // returns the number of values which are currently in the queue public boolean isEmpty() // returns true only if there are no values in the queue public void que(double value) // add the specified value into the queue public double deQue() // if the queue is not empty, // remove and returns the least currently qued value in the queue // otherwise, / returns Double.NaN public void queueDump() // print all of the values currenty in the queue, in the order that // they would be deQued from the queue Your Class must also work with the following Driver Class StackQueueDriver: public class StackQueueDriver { public static void main(String[] args) { Stack myStack = new Stack(); Queue myQueue = new Queue(); System.out.println("Filling Stack:"); double value; for (int s = 1; s < 11; ++s) { value = s + s/10.0; System.out.println("\tpushing " + value); myStack.push(value); } System.out.println(" Stack Dump:"); myStack.stackDump(); System.out.println(" Emptying Stack:"); while(!myStack.isEmpty()) { value = myStack.pop(); System.out.println("\tpop = " + value); } System.out.println(" Stack Dump:"); myStack.stackDump(); System.out.println(" A pop too far = " + myStack.pop()); System.out.println(" "); System.out.println("Filling Queue:"); for (int q = 1; q < 11; ++q) { value = 2*q + q/10.0; System.out.println("\tqueing " + value); myQueue.que(value); } System.out.println(" Queue Dump:"); myQueue.queueDump(); System.out.println(" Emptying Queue:"); while(!myQueue.isEmpty()) { value = myQueue.deQue(); System.out.println("\tdeQue = " + value); } System.out.println(" Queue Dump:"); myQueue.queueDump(); System.out.println(" A deQue too far = " + myQueue.deQue()); } } And produce the following output (or something equivalent): Filling Stack: pushing 1.1 pushing 2.2 pushing 3.3 pushing 4.4 pushing 5.5 pushing 6.6 pushing 7.7 pushing 8.8 pushing 9.9 pushing 11.0 Stack Dump: 11.0 9.9 8.8 7.7 6.6 5.5 4.4 3.3 2.2 1.1 Emptying Stack: pop = 11.0 pop = 9.9 pop = 8.8 pop = 7.7 pop = 6.6 pop = 5.5 pop = 4.4 pop = 3.3 pop = 2.2 pop = 1.1 Stack Dump: A pop too far = NaN Filling Queue: queing 2.1 queing 4.2 queing 6.3 queing 8.4 queing 10.5 queing 12.6 queing 14.7 queing 16.8 queing 18.9 queing 21.0 Queue Dump: array.at(0) = 2.1 array.at(1) = 4.2 array.at(2) = 6.3 array.at(3) = 8.4 array.at(4) = 10.5 array.at(5) = 12.6 array.at(6) = 14.7 array.at(7) = 16.8 array.at(8) = 18.9 array.at(9) = 21.0 Emptying Queue: deQue = 2.1 deQue = 4.2 deQue = 6.3 deQue = 8.4 deQue = 10.5 deQue = 12.6 deQue = 14.7 deQue = 16.8 deQue = 18.9 deQue = 21.0 Queue Dump: A deQue too far = NaN

Here's my DynArray,

public class DynArray { private double[] array; private int size; private int nextIndex;

public int arraySize() { return this.size; }

public int elements() { return this.nextIndex; }

public double at(int index) { if(0 <= index && index < this.nextIndex) { return this.array[index]; } else { return Double.NaN; } }

public DynArray() { this.size = 1; this.array = new double[this.size]; this.nextIndex = 0; }

private void grow() { double[] growArray = new double[this.size * 2]; for(int i = 0; i < this.nextIndex; i++) { growArray[i] = this.array[i]; } this.array = growArray; this.size *= 2; }

private void shrink() { if(this.size/2 == this.nextIndex) { int shrink = this.size / 2; if(shrink <= 0) { shrink = 1; } double[] shrinkArray = new double[shrink]; for(int i = 0; i < this.nextIndex; i++) { shrinkArray[i] = this.array[i]; } this.array = shrinkArray; this.size = shrink; } }

public void insertAt(int index, double value) { if(0 <= index && index <= this.nextIndex) { if(this.nextIndex == this.size) { this.grow(); } for(int i = this.nextIndex; i > index; i--) { this.array[i] = this.array[i - 1]; } this.array[index] = value; this.nextIndex++; } }

public void insert(double value) { this.insertAt(this.nextIndex, value); }

public double removeAt(int index) { if(0 <= index && index < this.nextIndex) { double value = at(index); for(int i = index; i < this.nextIndex-1; i++) { this.array[i] = this.array[i + 1]; } this.shrink(); this.nextIndex--; return value; } else { return Double.NaN; } }

public double remove() { double value = at(this.nextIndex-1); this.nextIndex--; if(this.nextIndex == -1) { this.nextIndex = 0; } this.shrink(); return value; }

//accessor // prints the values of all occupied locations of the array to // the screen public void printArray() { for(int i = 0; i < this.nextIndex; i++) { System.out.println("array.at" + "(" + i + ")" + " " + this.array[i]); } } }

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!