Java Main topics: Inheritance Program Specification: A Stack is a simple contain
ID: 3729828 • Letter: J
Question
Java
Main topics:
Inheritance
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 – FILO.
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 push’d 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 pop’d 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 – FIFO.
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 que’d 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 deQue’d 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(" pushing " + 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(" pop = " + 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(" queing " + 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(" deQue = " + 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
Explanation / Answer
package com.vinoth.chegg2702;
public class stackqueuedriver {
public static void main(string[] args) {
stack mystack = new stack();
queue < double > myqueue = new queue < double > ();
system.out.println("filling stack:");
double value;
for (int s = 1; s < 11; ++s) {
value = s + s / 10.0;
system.out.println(" pushing " + 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(" pop = " + 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(" queing " + 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(" deque = " + value);
}
system.out.println(" queue dump:");
myqueue.queuedump();
system.out.println(" a deque too far = " + myqueue.deque());
}
}---------------------------
package com.vinoth.chegg2702;
import java.util.iterator;
import java.util.linkedlist;
public class queue < t > implements iterable < t > {
private linkedlist < t > elements = new linkedlist < t > ();
public void que(t element) {
elements.add(element);
}
public t deque() {
if (!isempty()) return elements.removefirst();
return null;
}
public boolean isempty() {
return elements.isempty();
}
public void queuedump() {
for (t elem: elements) {
system.out.println(elem);
}
}@override public iterator < t > iterator() {
return elements.iterator();
}
}----------------------------------------------
package com.vinoth.chegg2702;
public class stack {
private int maxsize = 20;
private double[] stackarray;
private int top;
public stack() { // maxsize = s; stackarray = new double[maxsize]; top = -1; } public void push(double j) { stackarray[++top] = j; } public double pop() { return stackarray[top--]; } public double peek() { return stackarray[top]; } public boolean isempty() { return (top == -1); } public boolean isfull() { return (top == maxsize - 1); } public void stackdump(){ for(int i=top;i>0;i--){ system.out.println(stackarray[i]); } } public int size(){ return top; } }
Output
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 = null
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.