Problem

(Implement MyStackusing inheritance) In Listing 1, MyStack is implemented using compositio...

(Implement MyStackusing inheritance) In Listing 1, MyStack is implemented using composition. Define a new stack class that extends ArrayList.

Draw the UML diagram for the classes and then implement MyStack. Write a test program that prompts the user to enter five strings and displays them in reverse order.

LISTING 1 MyStack.java

1 import java.util.ArrayList;

2

3 public class MyStack {

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

5

6 public boolean isEmpty() {

7 return list.isEmpty();

8 }

9

10 public int getSize() {

11 return list.size();

12 }

13

14 public Object peek() {

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

16 }

17

18 public Object pop() {

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

20 list.remove(getSize() - 1);

21 return o;

22 }

23

24 public void push(Object o) {

25 list.add(o);

26 }

27

28 @Override

29 public String toString() {

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

31 }

32 }

Step-by-Step Solution

Request Professional Solution

Request Solution!

We need at least 10 more requests to produce the solution.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT