Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

implement all the method signatures for a stack in MyStack.java for GenericList code below Do not remove the methods you have already defined in GenericList

implement all the method signatures for a stack in MyStack.java for GenericList code below

Do not remove the methods you have already defined in GenericList - Add the methods for the MyStack interface using the methods you have already defined in GenericList - Will use the methods you have already defined in GenericList to emulate a stack. - Do not have to work directly with the array data structure in GenericList. -Will need to modify your toString method so that when you display a stack it looks like the following:

-top, last, ..., second, first, bottom

-Notice that you have the word top and the next item would be the item at the top of the stack. The last word is bottom and the item above would be the first item pushed onto the stack. There should be a space after every comma in the toString output.

Please show the new updated Generic List code. Thank you!

Generic List code

import java.util.Arrays; public class GenericListimplements MyStorage { private T[] arr; private int size; private void newArray() { arr = (T[]) new Object[10]; size = 0; } //to remove repeated code from add and insert method private void expandArray() { T[] arr2; arr2 = (T[]) new Object[(int)(arr.length * 1.2)]; for (int i = 0; i < arr.length; i++) arr2[i] = arr[i]; arr = arr2; } public GenericList() { this.newArray(); }

public int size() { return size; } public void add(T value) { if (size == arr.length) // Is arr full? Then expand by 20% { this.expandArray(); } arr[size] = value; size++; } public T get(int index) throws ArrayIndexOutOfBoundsException { if (index < 0 || index >= this.size) throw new ArrayIndexOutOfBoundsException(); else return arr[index]; }

// cler method - empty the list

public void clear() { this.newArray(); } public void insert(int index, T value) { if(index>=0 && index { if (size == arr.length) // Is arr full? Then expand by 20% { this.expandArray(); } //Open a hole to insert the value for (int i = size; i > index; i--) arr[i] = arr[i - 1]; arr[index] = value; size++; } //throw array here else if (index < 0 || index >= this.size) throw new ArrayIndexOutOfBoundsException(); } public String toString() { String returnValue = ""; if (size != 0) { returnValue = String.valueOf(arr[0]); for (int i = 1; i < size; i++) returnValue = returnValue + ", " + arr[i]; } return returnValue; } //display - display the list public void display() { for (int i = 0; i < size; i++) System.out.println(i + ": " + arr[i]); if ( arr.length == size) System.out.println("List is full "); else System.out.println("List has " + (arr.length - size) + " spaces left "); } public void set(int index, T value) { { //throw new ArrayIndexOutOfBoundsException(); if(index>=0 && index { arr[index] = value; } //throw array here else if (index < 0 || index >= this.size) throw new ArrayIndexOutOfBoundsException(); } }

public void remove(int index) { { //throw new ArrayIndexOutOfBoundsException(); if((index >=0) && (index < size)) { for (int i = index; i < size() - 1; i++) { arr[i] = arr[i + 1]; } --size; } //throw array here else if (index < 0 || index >= this.size) throw new ArrayIndexOutOfBoundsException(); } } public void swap(int index1,int index2){ if((index1 >=0) && (index1 < size)&&(index2 >=0) && (index2 < size)) { T temp = arr[index1]; arr[index1]=arr[index2]; arr[index2]=temp; } else if (index1 < 0 || index1 >= this.size || index2<0 ||index2>= this.size) throw new ArrayIndexOutOfBoundsException(); } }

public interface MyStack { //size - returns the size of the stack //return - the size of the stack as an integer public int size(); //push - push a value on the stack //param - value to add to the top of stack public void push(T value); //pop - return the value at the top of stack //returns - value at top of stack public T pop(); //clear - empty the stack public void clear(); //toString - return a string value that represents the stack //return - String public String toString();

//peek - return value at top of stack but do not remove it. public T peek();

// isEmpty - returns true if stack is empty. Otherwise false. public boolean isEmpty(); }

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

Probabilistic Databases

Authors: Dan Suciu, Dan Olteanu, Christopher Re, Christoph Koch

1st Edition

3031007514, 978-3031007514

More Books

Students also viewed these Databases questions

Question

Discuss all branches of science

Answered: 1 week ago

Question

Why is the System Build Process an iterative process?

Answered: 1 week ago