Question: Convert the simple Stack class below to a generic class that can work with any class. Currently the code only works with class .Integer .

Convert the simple Stack class below to a generic class that can work with any class. Currently the code only works with class .Integer

.

import java.util.*;

public class Stack

{

private ArrayList myList;

public Stack()

{

myList = new ArrayList();

}

public void push(Integer item)

{

myList.add(item);

}

public Integer pop()

{

Integer retItem = null;

if (!myList.isEmpty())

{

retItem = myList.remove(myList.size() -1);

}

return retItem;

}

public boolean empty()

{

return myList.isEmpty();

}

}

Here is a driver program that exercises the Stack class:

public class StackRunner

{

public static void main(String[] args)

{

Stack stk1 = new Stack();

stk1.push(1);

stk1.push(2);

stk1.push(3);

stk1.push(4);

while (!stk1.empty())

{

System.out.println(stk1.pop());

}

}

}

Modify the Stack and StackRunner classes so they can also create and exercise a stack of type Character

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!