Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Not entirely sure if those are the correct java files. Complete Programming Proj

ID: 3722775 • Letter: N

Question

Not entirely sure if those are the correct java files.

Complete Programming Project 1, page 194

In the test application, start with a stack size of 5 and create a stack of 10 or 12 elements to verify that the dynamics of the stack are functional. Remove several elements. Print the list before and after the copies to demonstrate the proper functioning of your methods.

---------------------------------

ArrayStack.java

---------------------------------

import java.util.Arrays;

import java.util.EmptyStackException;

/**

A class of stacks whose entries are stored in an array.

@author Frank M. Carrano

@author Timothy M. Henry

@version 4.0

*/

public final class ArrayStack<T> implements StackInterface<T>

{

private T[] stack; // Array of stack entries

private int topIndex; // Index of top entry

private boolean initialized = false;

private static final int DEFAULT_CAPACITY = 50;

private static final int MAX_CAPACITY = 10000;

public ArrayStack()

{

this(DEFAULT_CAPACITY);

} // end default constructor

public ArrayStack(int initialCapacity)

{

checkCapacity(initialCapacity);

  

// The cast is safe because the new array contains null entries

@SuppressWarnings("unchecked")

T[] tempStack = (T[])new Object[initialCapacity];

stack = tempStack;

topIndex = -1;

initialized = true;

} // end constructor

public void push(T newEntry)

{

checkInitialization();

ensureCapacity();

stack[topIndex + 1] = newEntry;

topIndex++;

} // end push

public T peek()

{

checkInitialization();

if (isEmpty())

throw new EmptyStackException();

else

return stack[topIndex];

} // end peek

public T pop()

{

checkInitialization();

if (isEmpty())

throw new EmptyStackException();

else

{

T top = stack[topIndex];

stack[topIndex] = null;

topIndex--;

return top;

} // end if

} // end pop

public boolean isEmpty()

{

return topIndex < 0;

} // end isEmpty

public void clear()

{

checkInitialization();

  

// Remove references to the objects in the stack,

// but do not deallocate the array

while (topIndex > -1)

{

stack[topIndex] = null;

topIndex--;

} // end while

// Assertion: topIndex is -1

} // end clear

// Throws an exception if this object is not initialized.

private void checkInitialization()

{

if (!initialized)

throw new SecurityException ("ArrayStack object is not initialized properly.");

} // end checkInitialization

// Throws an exception if the client requests a capacity that is too large.

private void checkCapacity(int capacity)

{

if (capacity > MAX_CAPACITY)

throw new IllegalStateException("Attempt to create a stack " +

"whose capacity exceeds " +

"allowed maximum.");

} // end checkCapacity

  

// Doubles the size of the array stack if it is full

// Precondition: checkInitialization has been called.

private void ensureCapacity()

{

if (topIndex >= stack.length - 1) // If array is full, double its size

{

int newLength = 2 * stack.length;

checkCapacity(newLength);

stack = Arrays.copyOf(stack, newLength);

} // end if

} // end ensureCapacity

} // end ArrayStack

--------------------------------

Driver.java

--------------------------------

/**

A driver that demonstrates the class ArrayStack.

@author Frank M. Carrano

@author Timothy M. Henry

@version 4.0

*/

public class Driver

{

public static void main(String[] args)

{

testStackOperations();

System.out.println(" Done.");

} // end main

public static void testStackOperations()

{

System.out.println("Create a stack: ");

StackInterface<String> myStack = new ArrayStack<>();

System.out.println("isEmpty() returns " + myStack.isEmpty());

System.out.println(" Add to stack to get " +

"Joe Jane Jill Jess Jim");

myStack.push("Jim");

myStack.push("Jess");

myStack.push("Jill");

myStack.push("Jane");

myStack.push("Joe");

System.out.println(" isEmpty() returns " + myStack.isEmpty());

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(" Add to stack to get " +

"Jim Jess Joe ");

myStack.push("Joe");

myStack.push("Jess");

myStack.push("Jim");

System.out.println(" Testing clear:");

myStack.clear();

System.out.println("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());

} // end testStackOperations

} // end Driver

/*

Create a stack:

isEmpty() returns true

Add to stack to get

Joe Jane Jill Jess Jim

isEmpty() returns false

Testing peek and pop:

Joe is at the top of the stack.

Joe is removed from the stack.

Jane is at the top of the stack.

Jane is removed from the stack.

Jill is at the top of the stack.

Jill is removed from the stack.

Jess is at the top of the stack.

Jess is removed from the stack.

Jim is at the top of the stack.

Jim is removed from the stack.

The stack should be empty: isEmpty() returns true

Add to stack to get

Jim Jess Joe

Testing clear:

The stack should be empty:

isEmpty() returns true

myStack.peek() returns

Exception in thread "main" java.util.EmptyStackException

at ArrayStack.peek(ArrayStack.java:46)

at Driver.testStackOperations(Driver.java:58)

at Driver.main(Driver.java:12)

*/

----------------------------

StackInterface.java

----------------------------

/**
An interface for the ADT stack.
@author Frank M. Carrano
@author Timothy M. Henry
@version 4.0
*/
public interface StackInterface<T>
{
/** Adds a new entry to the top of this stack.
@param newEntry An object to be added to the stack. */
public void push(T newEntry);
  
/** Removes and returns this stack's top entry.
@return The object at the top of the stack.
@throws EmptyStackException if the stack is empty before the operation. */
public T pop();
  
/** Retrieves this stack's top entry.
@return The object at the top of the stack.
@throws EmptyStackException if the stack is empty. */
public T peek();
  
/** Detects whether this stack is empty.
@return True if the stack is empty. */
public boolean isEmpty();
  
/** Removes all entries from this stack. */
public void clear();
} // end StackInterface

Explanation / Answer

This program seems correct .

Some changes are done in Driver Class to check dynamic resize .

Driver.java

/**
*
* A driver that demonstrates the class ArrayStack.
*
*
*
* @author Frank M. Carrano
*
* @author Timothy M. Henry
*
* @version 4.0
*
*/

public class Driver

{

   public static void main(String[] args)

   {

       testStackOperations();

       System.out.println(" Done.");

   } // end main

   public static void testStackOperations()

   {

       // create a stack with capcity 5
       System.out.println("Create a stack: ");

       StackInterface<String> myStack = new ArrayStack<>(5);

       System.out.println("isEmpty() returns " + myStack.isEmpty());

       System.out.println(" Adding 10 elements to satck   ");

       // Add 10 elements

       myStack.push("Jim");

       myStack.push("Jess");

       myStack.push("Jill");

       myStack.push("Jane");

       myStack.push("Joe");

       myStack.push("Kim");

       myStack.push("Kom");

       myStack.push("King");

       myStack.push("Kobra");

       myStack.push("Kid");

       System.out.println(" isEmpty() returns " + myStack.isEmpty());

       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(" Add to stack to get " +

               "Jim Jess Joe ");

       myStack.push("Joe");

       myStack.push("Jess");

       myStack.push("Jim");

       System.out.println(" Testing clear:");

       myStack.clear();

       System.out.println("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());

   } // end testStackOperations

} // end Driver

ArrayStack.java

import java.util.Arrays;

import java.util.EmptyStackException;

/**
*
* A class of stacks whose entries are stored in an array.
*
* @author Frank M. Carrano
*
* @author Timothy M. Henry
*
* @version 4.0
*
*/

public final class ArrayStack<T> implements StackInterface<T>

{

   private T[] stack; // Array of stack entries

   private int topIndex; // Index of top entry

   private boolean initialized = false;

   private static final int DEFAULT_CAPACITY = 50;

   private static final int MAX_CAPACITY = 10000;

   public ArrayStack()

   {

       this(DEFAULT_CAPACITY);

   } // end default constructor

   public ArrayStack(int initialCapacity)

   {

       checkCapacity(initialCapacity);

       // The cast is safe because the new array contains null entries

       @SuppressWarnings("unchecked")

       T[] tempStack = (T[]) new Object[initialCapacity];

       stack = tempStack;

       topIndex = -1;

       initialized = true;

   } // end constructor

   public void push(T newEntry)

   {

       checkInitialization();

       ensureCapacity();

       stack[topIndex + 1] = newEntry;

       topIndex++;

   } // end push

   public T peek()

   {

       checkInitialization();

       if (isEmpty())

           throw new EmptyStackException();

       else

           return stack[topIndex];

   } // end peek

   public T pop()

   {

       checkInitialization();

       if (isEmpty())

           throw new EmptyStackException();

       else

       {

           T top = stack[topIndex];

           stack[topIndex] = null;

           topIndex--;

           return top;

       } // end if

   } // end pop

   public boolean isEmpty()

   {

       return topIndex < 0;

   } // end isEmpty

   public void clear()

   {

       checkInitialization();

       // Remove references to the objects in the stack,

       // but do not deallocate the array

       while (topIndex > -1)

       {

           stack[topIndex] = null;

           topIndex--;

       } // end while

       // Assertion: topIndex is -1

   } // end clear

   // Throws an exception if this object is not initialized.

   private void checkInitialization()

   {

       if (!initialized)

           throw new SecurityException("ArrayStack object is not initialized properly.");

   } // end checkInitialization

   // Throws an exception if the client requests a capacity that is too large.

   private void checkCapacity(int capacity)

   {

       if (capacity > MAX_CAPACITY)

           throw new IllegalStateException("Attempt to create a stack " +

                   "whose capacity exceeds " +

                   "allowed maximum.");

   } // end checkCapacity

   // Doubles the size of the array stack if it is full

   // Precondition: checkInitialization has been called.

   private void ensureCapacity()

   {

       if (topIndex >= stack.length - 1) // If array is full, double its size

       {

           int newLength = 2 * stack.length;

           checkCapacity(newLength);

           stack = Arrays.copyOf(stack, newLength);

       } // end if

   } // end ensureCapacity

} // end ArrayStack

StackInterface.java

/**
* An interface for the ADT stack.
*
* @author Frank M. Carrano
* @author Timothy M. Henry
* @version 4.0
*/
public interface StackInterface<T> {
   /**
   * Adds a new entry to the top of this stack.
   *
   * @param newEntry
   *            An object to be added to the stack.
   */
   public void push(T newEntry);

   /**
   * Removes and returns this stack's top entry.
   *
   * @return The object at the top of the stack.
   * @throws EmptyStackException
   *             if the stack is empty before the operation.
   */
   public T pop();

   /**
   * Retrieves this stack's top entry.
   *
   * @return The object at the top of the stack.
   * @throws EmptyStackException
   *             if the stack is empty.
   */
   public T peek();

   /**
   * Detects whether this stack is empty.
   *
   * @return True if the stack is empty.
   */
   public boolean isEmpty();

   /** Removes all entries from this stack. */
   public void clear();
} // end StackInterface

output

Create a stack:
isEmpty() returns true

Adding 10 elements to satck


isEmpty() returns false

Testing peek and pop:

Kid is at the top of the stack.
Kid is removed from the stack.

Kobra is at the top of the stack.
Kobra is removed from the stack.

King is at the top of the stack.
King is removed from the stack.

Kom is at the top of the stack.
Kom is removed from the stack.

Kim is at the top of the stack.
Kim is removed from the stack.

Joe is at the top of the stack.
Joe is removed from the stack.

Jane is at the top of the stack.
Jane is removed from the stack.

Jill is at the top of the stack.
Jill is removed from the stack.

Jess is at the top of the stack.
Jess is removed from the stack.

Jim is at the top of the stack.
Jim is removed from the stack.

The stack should be empty: isEmpty() returns true

Add to stack to get
Jim Jess Joe


Testing clear:
The stack should be empty:

isEmpty() returns true

myStack.peek() returns
Exception in thread "main" java.util.EmptyStackException
   at stack.ArrayStack.peek(ArrayStack.java:89)
   at stack.Driver.testStackOperations(Driver.java:108)
   at stack.Driver.main(Driver.java:25)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote