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

The question is: Write a generic MyStack class ( class MyStack<T> ) to implement

ID: 3672593 • Letter: T

Question

The question is: Write a generic MyStack class ( class MyStack<T> ) to implement a stack using an ArrayList.

Data Member:

-An ArrayList<T> object

Instance Methods:

-public MyStack()     : zero-parameter constructor: instantiate an empty stack

-public MyStack(MyStack<T>) : copy constructor

-public boolean isEmpty():

-public void push(T):

-public T pop() :

-public T lookUp():

So far, this is what I have. I'm missing the copy constructor and I'm not sure if what I've done is correct.

import java.util.ArrayList;

public class MyStack<T>

{
   private ArrayList<T> stack;

   public MyStack()
   {
       stack = new ArrayList<T>();
   }

  
   public MyStack (MyStack<T> oldStack)
{

   }


   public boolean isEmpty()
   {
       return size()==0;

   }

   public void push (T value)
   {
       stack.add(value);

   }


   public T pop()
   {
       if (size() != 0)
       {
           return stack.remove(size()-1);
       }
       else
       {
           System.out.println("Empty list, cannot list.");
           return null;
       }

   }

   public T lookUp()
   {
       if (size() != 0)
       {
           return stack.get(size()-1);
       }
       else
       {
           System.out.println("Empty list, cannot look up.");
           return null;
       }
   }

   public int size()
   {
       return stack.size();
   }
}

Explanation / Answer

Hi, I have implement oldStack copy constructor.

The only way to get all elements of oldStack is pop(). so first pop all elements one by one and add it to stack.

Then restore oldstack

import java.util.ArrayList;
public class MyStack<T>
{
    private ArrayList<T> stack;
  
    public MyStack()
    {
        stack = new ArrayList<T>();
    }
  
    public MyStack (MyStack<T> oldStack)
    {
        int size = oldStack.size();
        if(size == 0)   // if oldStack is empty, then initialize stack with empty
            stack = new ArrayList<T>();
        else{
            //pop all elements one by one and add it in stak
            while(oldStack.size() > 0){
                stack.add(oldStack.size()-1, oldStack.pop());
              
            }
          
            //now oldStack gis empty, so iterate through stack from begining and add it to oldStack
          
            int i=0;
            while(i < size){
                // restoring oldStack
                oldStack.push(stack.get(i));
            }
        }
    }

    public boolean isEmpty()
    {
        return size()==0;
    }
    public void push (T value)
    {
        stack.add(value);
    }

    public T pop()
    {
        if (size() != 0)
        {
            return stack.remove(size()-1);
        }
        else
        {
            System.out.println("Empty list, cannot list.");
            return null;
        }
    }
    public T lookUp()
    {
        if (size() != 0)
        {
            return stack.get(size()-1);
        }
        else
        {
            System.out.println("Empty list, cannot look up.");
            return null;
        }
    }
    public int size()
    {
        return stack.size();
    }
}

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