Modify the Stack class as follows:

  • Read all of the directions before starting on this project.
  • Add a constructor with no arguments that creates an array of size 64.
  • Modify Push so that if the array is full, the stack size is doubled. To do this, create a new array, copy the existing items from the old array to the new array, and then replace the old array with the new one.
  • Add a property called Size. The get accessor will retrieve the size of the array. The set accessor will create a new array with the specified size, copy the items from the original array, and replace the old array with the new one. The new size must be larger than the old size. Otherwise throw an InvalidOperationException.
  • Include a private method called Resize that is used by Push and the set accessor.
  • You can include a class containing a Main method in the same file as the Stack class for testing purposes. However, remove it before turning in the program. I will supply my own test program.
  • Include javadoc comments for everything. Change the author tag to au- thors and add yourself to the list. Keep existing comments or update them as appropriate.

Starter Code

stack.cs

using System;

/**
* An array implementation of a generic last-in first-out (LIFO)
* data structure.
*/
public class Stack< T > {
T[] contents;
int top;

/**
* Creates an empty stack of maximum size n.
*/
public Stack(int n) {
contents = new T[n];
top = -1;
}

/**
* Indicates whether the stack is empty.
*/
public bool Empty {
get => top == -1;
}

/**
* Adds an item to the top of the stack.
* @param The item to add.
* @throws InvalidOperationException if the stack is full.
*/
public void Push(T item) {
if (top == contents.Length - 1) {
throw new InvalidOperationException("Stack Overflow");
}
else {
contents[++top] = item;
}
}

/**
* Removes and returns the item from the top of the stack.
* @returns The top item.
* @throws InvalidOperationException if the stack is empty.
*/
public T Pop() {
if (top == -1) {
throw new InvalidOperationException("Empty Stack");
}
else {
return contents[top--];
}
}
}
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.