Question: JAVA 3 Write a method matchAndUpdate to be outside the ArrayStack class that has two parameters St1 and St2 of type Arraystack with integer data

JAVA 3 Write a method matchAndUpdate to be outside the ArrayStack class that has two parameters St1 and St2 of type Arraystack with integer data elements and a third parameter value of type int. The method is to change the values of st1 if there is a full match between St1 and St2 (all elements in St1 and St2 are the same and in the same order), St1 will change in a way that value will be added to every element in St1. If no full match, the method returns false, otherwise it returns true. Assume St1 and St2 are of the same size. Assume further that you have accessibility to ArrayStack class methods only and not allowed to use arrays. The method prototype is

public boolean static matchAndUpdate(ArrayStack St1, ArrayStack St2, int value);

Ex:

St1:

top

2 5 7 8 9

St2:

2 5 7 8 9

Value is 2

After method call

St1:

top

4 7 9 10 11

St2:

2 5 7 8 9

PLEASE ONLY DO THE METHOD ILL GIVE IMPLEMENTATION

public static boolean matchAndUpdate(ArrayStack St1, ArrayStack St2, int value)

import java.util.NoSuchElementException;

/** Implementation of the interface StackInt using an array. The

* bottom element of the stack is in location 0 in the array.

*/

public class ArrayStack implements StackInt

{

// Data Fields

private E[ ] theData; // array to store stack elements

/** Index of top of the stack. */

private int topOfStack; // index of stack top.

private static final int INITIAL_CAPACITY = 10;

/**

* Construct an empty stack with the default initial capacity.

*/

public ArrayStack()

{

topOfStack = -1; // Initially empty stack.

theData = (E[]) new Object[INITIAL_CAPACITY];

}

/**

* Construct an empty stack with the initial capacity equal to

* parameter cap.

* @param cap The initial capacity of the stack

*/

public ArrayStack(int cap)

{

topOfStack = -1; // Initially empty stack.

if (cap <= 0 )

cap = INITIAL_CAPACITY;

theData = (E[]) new Object[cap];

}

/** Copy constructor Construct a new stack as copy of the existing

* stack passed as parameter.

* @param other The original stack

* copy is made in this object

*/

public ArrayStack (ArrayStack other)

{

theData = (E[]) new Object[other.topOfStack + 1];

for (int i = 0; i < other.topOfStack + 1; i++)

theData[i]= other.theData[i];

topOfStack = other.topOfStack;

}

/**

* Insert a new item on top of the stack.

* @post The new item is the top item on the stack.

* @param obj The item to be inserted

* @return The item that was inserted

*/

@Override

public E push(E obj)

{

if (topOfStack == theData.length - 1) {

reallocate();

}

topOfStack++;

theData[topOfStack] = obj;

return obj;

}

/**

* Remove and return the top item on the stack.

* @pre The stack is not empty.

* @post The top item on the stack has been removed and

* the stack is one item smaller.

* @return The top item on the stack

* @throws NoSuchElementException, if the stack is empty

*/

@Override

public E pop()

{

if (isEmpty()) {

throw new NoSuchElementException();

}

E result = theData[topOfStack];

theData[topOfStack] = null;

topOfStack--;

return result;

}

/**

* Return the top item on the stack

* Pre: The stack is not empty

* Post: The stack remains unchanged

* @return The top item on the stack

* @throws NoSuchElementException, if the stack is empty

*/

@Override

public E peek()

{

if (isEmpty()) {

throw new NoSuchElementException();

}

return theData[topOfStack];

}

/**

* Return true if the stack is empty

* @return True if the stack is empty

*/

@Override

public boolean isEmpty()

{

return (topOfStack == -1);

}

/**

* Method to reallocate the array containing the stack data

* @post The size of the data array has been doubled

* and all of the data has been copied to the new array

*/

private void reallocate() {

E[] temp = (E[]) new Object[2 * theData.length];

System.arraycopy(theData, 0, temp, 0, theData.length);

theData = temp;

}

} //end ArrayStack

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!