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(); } }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.