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

/** A class of stacks whose entries are stored in a chain of nodes. Implement al

ID: 3760233 • Letter: #

Question

/**
A class of stacks whose entries are stored in a chain of nodes.
Implement all methods in MyStack class
Main Reference : text book or class notes
Do not change or add data fields
*/

package PJ2;

public class MyStack<T> implements StackInterface<T>
{

// Data fields
private Node<T> topNode; // references the first node in the chain
private int numberOfEntries;

public MyStack()
{
private ArrayList<Object> list = new ArrayList<Object>();

^^^^^^^^^^^^^^^^^^^^^^^

illegal start of expression

^^^^^^^^^^^^^^^^^^^^^^^

// no items yet
public boolean isEmpty() {
return list.isEmpty();
} // end default constructor
  
public void push(T newEntry)
{
// add stataments
list.add(newEntry);
} // end push

public T peek()
{
// add stataments
return list.get(size() - 1);
} // end peek

public T pop()
{
Object o = list.get(size() - 1);
list.remove(size() - 1);
return o;
  
} // end pop

public boolean empty()
{
// add stataments
return list.isEmpty();
} // end empty

public int size()
{
// add stataments
return list.size();
} // end isEmpty

public void clear()
{
// add stataments
this.list.clear();
} // end clear

public String toString()
{
// add stataments
return "stack: " + list.toString();
}


/****************************************************
Do not modify: Stack test

Expected output:

   OK: stack is empty
   Push 3 data: 10, 30, 50
   Print stack [50,30,10,]
   OK: stack size is 3
   OK: peek stack top is 50
   OK: stack is not empty
   Pop 2 data: 50, 30
   Print stack [30,10,]
   Print stack [10,]
   OK: stack pop data is 30
   Clear stack
   Print stack []

******************************************************/

public static void main (String args[])
{
   MyStack<Integer> s = new MyStack<Integer>();
   if (s.empty())
System.out.println("OK: stack is empty");
   else
System.out.println("Error: stack is not empty");

   s.push(10);
   s.push(30);
   s.push(50);
System.out.println("Push 3 data: 10, 30, 50");
System.out.println("Print stack " + s);

   if (s.size() == 3)
System.out.println("OK: stack size is 3");
   else
System.out.println("Error: stack size is " + s.size());

   if (s.peek() == 50)
System.out.println("OK: peek stack top is 50");
   else
System.out.println("Error: peek stack top is " + s.size());

   if (!s.empty())
System.out.println("OK: stack is not empty");
   else
System.out.println("Error: stack is empty");

System.out.println("Pop 2 data: 50, 30");
s.pop();
System.out.println("Print stack " + s);
   int data=s.pop();
System.out.println("Print stack " + s);
   if (data == 30)
System.out.println("OK: stack pop data is 30");
   else
System.out.println("Error: stack pop data is " + data);

System.out.println("Clear stack");
s.clear();
System.out.println("Print stack " + s);
}

} // end Stack

Explanation / Answer

Note:

            Requirement not specified clearly, based on the understanding the solution is prepared

without making major changes in the given code.

Program:

package PJ2;

import java.io.*;

import java.util.*;

import java.util.ArrayList.*;

interface StackInterface<T>

{

   // to add new entry in the stack

   public void push(T newEntry);

   // to remove the entry from the stack

   public T pop();

   // to get the to element of the stack

   public T peek();

   // method to check stack i sempty or not

   public boolean isEmpty();

   /** Removes all entries from this stack. */

   public void clear();

} // end StackInterface

public class MyStack<T> implements StackInterface<T>

{

  

    private class Node<T>

    {

    private T item;

    private Node next;

    public Node(T newItem)

    {

      item = newItem;

      next = null;

    }

    public Node(T newItem, Node nextNode)

    {

      item = newItem;

      next = nextNode;

    }

    public void setItem(T newItem)

   {

      item = newItem;

    } // end setItem

    public T getItem()

   {

      return item;

    } // end getItem

    public void setNext(Node nextNode)

   {

      next = nextNode;

    } // end setNext

    public Node getNext()

   {

      return next;

    }

} // end class Node

   // Data fields

   private Node<T> topNode; // references the first node in

   the chain

   private int numberOfEntries;

  

   public MyStack()

   {}

        private ArrayList<T> list = new ArrayList<T>();

   

    // no items yet

        public boolean isEmpty() {

        return list.isEmpty();

   } // end default constructor

   public void push(T newEntry)

   {

      // add stataments

       list.add(newEntry);

   } // end push

   public T peek()

   {

      // add stataments

      return list.get(size() - 1);

   } // end peek

   public T pop()

   {

    T o = list.get(size() - 1);

    list.remove(size() - 1);

    return o;

     

   } // end pop

   public boolean empty()

   {

      // add stataments

      return list.isEmpty();

   } // end empty

   public int size()

   {

      // add stataments

      return list.size();

   } // end isEmpty

   public void clear()

   {

      // add stataments

       this.list.clear();

   } // end clear

   public String toString()

   {

      // add stataments

      return "stack: " + list.toString();

   }

   public static void main (String args[])

   {

    MyStack<Integer> s = new MyStack<Integer>();

    if (s.empty())

            System.out.println("OK: stack is empty");

    else

            System.out.println("Error: stack is not

empty");

    s.push(10);

    s.push(30);

    s.push(50);

        System.out.println("Push 3 data: 10, 30, 50");

        System.out.println("Print stack " + s);

    if (s.size() == 3)

            System.out.println("OK: stack size is 3");

    else

            System.out.println("Error: stack size is " +

            s.size());

    if (s.peek() == 50)

            System.out.println("OK: peek stack top is 50");

    else

            System.out.println("Error: peek stack top is "

+ s.size());

    if (!s.empty())

            System.out.println("OK: stack is not empty");

    else

            System.out.println("Error: stack is empty");

        System.out.println("Pop 2 data: 50, 30");

        s.pop();

        System.out.println("Print stack " + s);

    int data=s.pop();

        System.out.println("Print stack " + s);

    if (data == 30)

            System.out.println("OK: stack pop data is 30");

    else

            System.out.println("Error: stack pop data is "

            + data);

        System.out.println("Clear stack");

        s.clear();

        System.out.println("Print stack " + s);

   }

} // end Stack