Fix the ArrayStack.java file so you add the required method declared in StackInt
ID: 3820516 • Letter: F
Question
Fix the ArrayStack.java file so you add the required method declared in StackInterface, but not defined ArrayStack yet (so write the function definition), AND fix the isEmpty() method.
In the StackDriver file, where it says to "fill in for Ex. 2.1" put the correct code so an ArrayStack is instantiated, and also where it says "add code to print size of stack", put a System.out.println statement as it specifies
Copy and paste the output to the end of the file, commented out. Also, answer the following questions (also at the end of the file):
1. Was the "UNABLE TO PUSH..." message displayed? If so, why?
2. What happens when you try to pop() from an empty stack?
// ArrayStack.java
import java.util.*;
/**
A class of stacks whose entries are stored in an array.
@author Frank M. Carrano
@author Timothy M. Henry
@version 4.0
UPDATED by C. Lee-Klawender
*/
public class ArrayStack<T> implements StackInterface<T>
{
private T[] stack; // Array of stack entries
private int topIndex; // Index of top entry
private static final int DEFAULT_CAPACITY = 5;
private static final int MAX_CAPACITY = 100;
public ArrayStack()
{
this(DEFAULT_CAPACITY);
} // end default constructor
public ArrayStack(int initialCapacity)
{
if(initialCapacity > MAX_CAPACITY)
initialCapacity = MAX_CAPACITY;
else
if( initialCapacity < DEFAULT_CAPACITY )
initialCapacity = DEFAULT_CAPACITY;
// The cast is safe because the new array contains null entries
@SuppressWarnings("unchecked")
T[] tempStack = (T[])new Object[initialCapacity];
stack = tempStack;
topIndex = -1;
} // end constructor
public boolean push(T newEntry)
{
if( topIndex+1 < stack.length )
{
stack[topIndex + 1] = newEntry;
topIndex++;
return true;
}
return false;
} // end push
public T peek()
{
if (isEmpty()) // UPDATE FOR HW#1
return null;
else
return stack[topIndex];
} // end peek
public T pop()
{
if (isEmpty()) // UPDATE FOR HW#1
return null;
else
{
T top = stack[topIndex];
stack[topIndex] = null;
topIndex--;
return top;
} // end if
} // end pop
// TWO MORE METHODS ARE REQUIRED HERE (PART OF EXERCISE 2.1)
} // end ArrayStack
//StackDriver
/**
A driver that demonstrates the using an implementation of a Stack.
@author Frank M. Carrano
@author Timothy M. Henry
@version 4.0
UPDATED by C. Lee-Klawender
*/
public class StackDriver
{
public static void main(String[] args)
{
System.out.println("Create a stack: ");
StackInterface<String> myStack = new _____________<>(); // FILL IN FOR EX. 2.1
System.out.println("Results of testing the FIRST stack: ");
testStackOperations(myStack);
// Code will be added for Ex. 2.2 here
System.out.println(" Done.");
} // end main
public static void testStackOperations(StackInterface<String> myStack)
{
System.out.println("isEmpty() returns " + myStack.isEmpty());
System.out.println(" Add to stack to get " +
"Joe Jane Jill Jess Jim");
String [] strArray = {"Jim", "Jess", "Jill", "Jane", "Joe", "Jack"};
for( int i=0; i < strArray.length ; ++i )
{
if( myStack.push( strArray[i] ) )
System.out.println("Pushed " + strArray[i] + " successfully");
else
System.out.println("UNABLE TO PUSH " + strArray[i] );
}
System.out.println(" isEmpty() returns " + myStack.isEmpty());
// FOR LAB EXERCISE 2.1, ADD CODE HERE TO GET THE SIZE FROM THE Stack AND DISPLAY IT
System.out.println(" Testing peek and pop:");
while (!myStack.isEmpty())
{
String top = myStack.peek();
System.out.println(" " + top + " is at the top of the stack.");
top = myStack.pop();
System.out.println(top + " is removed from the stack.");
} // end while
System.out.print(" The stack should be empty: ");
System.out.println("isEmpty() returns " + myStack.isEmpty());
System.out.println(" myStack.peek() returns ");
System.out.println(myStack.peek());
System.out.println(" myStack.pop() returns ");
System.out.println(myStack.pop());
System.out.println(" End of Stack Test ");
} // end testStackOperations
} // end Driver
Explanation / Answer
HI, You have not posted StackInterface, so i can not test.
I have added required code for isEmpty method.
Please let me know in case of any issue.
//ArrayStack.java
import java.util.*;
/**
A class of stacks whose entries are stored in an array.
@author Frank M. Carrano
@author Timothy M. Henry
@version 4.0
UPDATED by C. Lee-Klawender
*/
public class ArrayStack<T> implements StackInterface<T>
{
private T[] stack; // Array of stack entries
private int topIndex; // Index of top entry
private static final int DEFAULT_CAPACITY = 5;
private static final int MAX_CAPACITY = 100;
public ArrayStack()
{
this(DEFAULT_CAPACITY);
} // end default constructor
public ArrayStack(int initialCapacity)
{
if(initialCapacity > MAX_CAPACITY)
initialCapacity = MAX_CAPACITY;
else
if( initialCapacity < DEFAULT_CAPACITY )
initialCapacity = DEFAULT_CAPACITY;
// The cast is safe because the new array contains null entries
@SuppressWarnings("unchecked")
T[] tempStack = (T[])new Object[initialCapacity];
stack = tempStack;
topIndex = -1;
} // end constructor
public boolean push(T newEntry)
{
if( topIndex+1 < stack.length )
{
stack[topIndex + 1] = newEntry;
topIndex++;
return true;
}
return false;
} // end push
public T peek()
{
if (isEmpty()) // UPDATE FOR HW#1
return null;
else
return stack[topIndex];
} // end peek
public T pop()
{
if (isEmpty()) // UPDATE FOR HW#1
return null;
else
{
T top = stack[topIndex];
stack[topIndex] = null;
topIndex--;
return top;
} // end if
} // end pop
//TWO MORE METHODS ARE REQUIRED HERE (PART OF EXERCISE 2.1)
public boolean isEmpty(){
return stack.length == topIndex;
}
} // end ArrayStack
//StackDriver
/**
A driver that demonstrates the using an implementation of a Stack.
@author Frank M. Carrano
@author Timothy M. Henry
@version 4.0
UPDATED by C. Lee-Klawender
*/
public class StackDriver
{
public static void main(String[] args)
{
System.out.println("Create a stack: ");
StackInterface<String> myStack = new ArrayStack<>(); // FILL IN FOR EX. 2.1
System.out.println("Results of testing the FIRST stack: ");
testStackOperations(myStack);
// Code will be added for Ex. 2.2 here
System.out.println(" Done.");
} // end main
public static void testStackOperations(StackInterface<String> myStack)
{
System.out.println("isEmpty() returns " + myStack.isEmpty());
System.out.println(" Add to stack to get " +
"Joe Jane Jill Jess Jim");
String [] strArray = {"Jim", "Jess", "Jill", "Jane", "Joe", "Jack"};
for( int i=0; i < strArray.length ; ++i )
{
if( myStack.push( strArray[i] ) )
System.out.println("Pushed " + strArray[i] + " successfully");
else
System.out.println("UNABLE TO PUSH " + strArray[i] );
}
System.out.println(" isEmpty() returns " + myStack.isEmpty());
// FOR LAB EXERCISE 2.1, ADD CODE HERE TO GET THE SIZE FROM THE Stack AND DISPLAY IT
System.out.println(" Testing peek and pop:");
while (!myStack.isEmpty())
{
String top = myStack.peek();
System.out.println(" " + top + " is at the top of the stack.");
top = myStack.pop();
System.out.println(top + " is removed from the stack.");
} // end while
System.out.print(" The stack should be empty: ");
System.out.println("isEmpty() returns " + myStack.isEmpty());
System.out.println(" myStack.peek() returns ");
System.out.println(myStack.peek());
System.out.println(" myStack.pop() returns ");
System.out.println(myStack.pop());
System.out.println(" End of Stack Test ");
} // end testStackOperations
} // end Driver
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.