Question
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
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 what i have so Far,also bold the start of each class: DynArray:
import java.util.Arrays;
class DynArray
{
private double[] array;
private int size;
private int nextIndex;
public DynArray()
{
array = new double[1];
size = 1;
nextIndex = 0;
} public int arraySize() // Accessor
{
return size;
} public int elements() // accessor
{
return nextIndex;
} public double at(int index) // accessor
{
if(index >=0 && index < nextIndex)
return array[index];
else
return Double.NaN;
} private void grow()
{
if(nextIndex == size)
{
array = Arrays.copyOf(array, size * 2);
size = size *2;
}//End of if condition
}//End of method
private void shrink()
{
if(nextIndex != 0)
if(nextIndex <= (size/2))
{
array = Arrays.copyOf(array, size/2);
size = size / 2;
}//End of if condition
}//End of method
public void insertAt(int index, double value) // mutator
{
// if 0 <= index <= nextIndex
if(index >= 0 && index <= nextIndex)
{
grow();
for(int x = nextIndex; x > index; x--)
array[x] = array[x-1];
} array[index] = value;
nextIndex++;
} public void insert(double value) // mutator
{
// Calls grow() method to increase the array size to double
grow();
// insert value at location nextIndex.
array[nextIndex] = value;
// Increase the next index position by one
nextIndex++;
} public double removeAt(int index) // mutator
{
if(index >=0 && index < nextIndex)
{
double value = at(index);
// Calls shrink() method to decrease the array size to half
shrink();
// Loop starts from index position specified in parameter and stops at next index position minus one
for(int x = index; x < nextIndex-1; x++)
// Moves data one position left
array[x] = array[x+1];
// Decrease the next index position by one
nextIndex--;
// Retuns the deleted value
return value;
}// End of if condition
// Otherwise
else
// Returns NAN
return Double.NaN;
} public double remove() // mutator
{
// Calls shrink() method to decrease the array size to half
shrink();
// Extracts data at specified index position as in parameter
double value = at(nextIndex-1);
// Decrease the next index position by one
--nextIndex;
// Checks if next index position is -1 then set it to zero
if(nextIndex == -1)
nextIndex = 0;
// return the removal of the value at location nextIndex-1.
return value;
} public void printArray() //accessor
{
// prints the values of all occupied locations of the array to the screen
for(int x = 0; x < nextIndex; x++)
System.out.printf(" array.at(%d) = %.2f", x, array[x]);
}// End of method
} DynaArrayDriver:
public class DynArrayDriver
{
// main method definition
public static void main(String[] args)
{
// Declares an object of class DynArray()
DynArray myArray = new DynArray();
System.out.println("size = " + myArray.arraySize());
System.out.println("elements = " + myArray.elements());
//System.out.println(" ");
int pot = 1;
// Loops 10 times
for (int v = 0; v < 10; ++v)
{
myArray.insert(pot);
System.out.println("myArray.at(" + v + ") = " + myArray.at(v));
System.out.println("size = " + myArray.arraySize());
System.out.println("elements = " + myArray.elements());
pot *= 2;
}// End of for loop
System.out.println("myArray.at(10) = " + myArray.at(10));
System.out.println(" ");
// Loops 10 times
for (int v = 0; v < 10; ++v)
{
double value = myArray.remove();
System.out.println("value = " + value);
System.out.println("size = " + myArray.arraySize());
System.out.println("elements = " + myArray.elements() + " ");
}// End of for loop
double value = myArray.remove();
System.out.println("value = " + value);
System.out.println("size = " + myArray.arraySize());
System.out.println("elements = " + myArray.elements());
System.out.println(" ");
// Loops 5 times
for (int i = 0; i < 5; ++i)
{
myArray.insertAt(i, 3 * i);
System.out.println("myArray.at(" + i + ") = " + myArray.at(i));
System.out.println("size = " + myArray.arraySize());
System.out.println("elements = " + myArray.elements() + " ");
}// End of for loop
myArray.printArray();
value = myArray.removeAt(2);
System.out.println();
System.out.println("value = " + value);
System.out.println("size = " + myArray.arraySize());
System.out.println("elements = " + myArray.elements() + " ");
myArray.printArray();
System.out.println();
value = myArray.removeAt(4);
System.out.println("value = " + value);
System.out.println("size = " + myArray.arraySize());
System.out.println("elements = " + myArray.elements() + " ");
} // End of main method
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started