Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java 1.Modify the clear() method so that all the elements in the array are reset to null a.Identify the complexity O() of the clear() method

Java

1.Modify the clear() method so that all the elements in the array are reset to null

a.Identify the complexity O() of the clear() method before and after your changes. Explain.

2.Add a new method growStack()a.The method should initialize a new array. The size of the new array should be the current size plus the default sizeb.

Update the stackArray reference to point to the new arrayc.

Identify the complexity of the growStack() method. Explain.

ArrayStack Code-

public class ArrayStack implements StackADT {

private E stackArray[]; // Array holding stack private static final int DEFAULT_SIZE = 10; private int maxSize; // Maximum size of stack private int top; // First free position at top

// Constructors @SuppressWarnings("unchecked") // Generic array allocation public ArrayStack(int size) { maxSize = size; top = 0; stackArray = (E[]) new Object[size]; // Create stackArray }

public ArrayStack() { this(DEFAULT_SIZE); }

// Modify this method // so that it all elements are null public void clear() { top = 0; } // Reinitialize stack

// Push "element" onto stack // Changed return type of method to void public void push(E element) { // modify this method // if (top >= maxSize) // move this to a private method called isFull() return false; // call growStack() instead // no changes after this stackArray[top++] = element; // return true; // you may remove this line altogether }

// Remove and return top element public E pop() { if (top == 0) return null; return stackArray[--top]; }

public E peek() { // Return top element if (top == 0) return null; return stackArray[top - 1]; }

public int length() { return top; } // Return stack size

public boolean isEmpty() { return top == 0; } // Tell if the stack is empty // add new method growStack() // growStack() that returns void // 1. create a new local array, call it newStack (see the constructor for an example) // 2. set the size of the new array to stackArray.length + DEFAULT_SIZE // 3. copy all the elements from stackArray to newStack (for loop is perfect) // 4. set stackArray to reference the newStack }

ArrayStackTester Code-

public class ArrayStackTester {

public static void main(String[] args) { // TODO Auto-generated method stub

// ArrayStack myStack = new ArrayStack(); AnotherStack myStack = new AnotherStack(2); try { myStack.peek(); } catch (EmptyCollectionException e) { // TODO Auto-generated catch block //e.printStackTrace(); System.out.println(e.getMessage()); } System.out.println("Push number 4"); myStack.push(4); System.out.println("Test peek [4]. Method returned: " + myStack.peek());

// Test new item at top after a second push System.out.println("Push number 7"); myStack.push(7); System.out.println("Test peek [7]: " + myStack.peek()); System.out.println("Push number 3");

myStack.push(3); }

}

StackADT Interface Code -

public interface StackADT { public void push(T element); public T pop(); public T peek(); public boolean isEmpty(); public int length(); public String toString();

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database Management Systems

Authors: Mark L. Gillenson

3rd Edition

978-1119907466

More Books

Students also viewed these Databases questions

Question

1. Identify outcomes (e.g., quality, accidents).

Answered: 1 week ago