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

Implement MyStack using inheritance. MyStack is implemented using composition. D

ID: 647682 • Letter: I

Question

Implement MyStack using inheritance. MyStack is implemented using composition. Define a new Stack class that extends ArrayList.

Write a test program that prompts the user to enter five strings and displays them in reverse order.

This is what I have so far:

import java.util.ArrayList;

public class MyStack {

      

       private ArrayList<Object> list = new ArrayList<>;

      

       public boolean isEmplty(){

              return list.isEmpty();

       }

      

       public int getSize() {

              return list.size();

       }

      

       public Object peek(){

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

       }

      

       public Object pop(){

              Object o = list.get(getSize()-1);

              list.remove(getSize() -1);

              return o;

       }

      

       public void push(Object o) {

              list.add(o);

       }

      

       @Override

       public String toString(){

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

       }

      

}

Explanation / Answer

import java.util.ArrayList; public class MyStack { private ArrayList list = new ArrayList(); public boolean isEmpty() { return list.isEmpty(); } public int getSize() { return list.size(); } public Object peek() { return list.get(getSize() - 1); } public Object pop() { Object o = list.get(getSize() - 1); list.remove(getSize() - 1); return o; } public void push(Object o) { list.add(o); } @Override /** Override the toString in the Object class */ public String toString() { return "stack: " + list.toString(); } }
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