Based on what you know about Java Generics, convert the IntStack into a generic SimpleStack that can be used to store objects of any class type.
Consider the existing IntStack given below:
import java.util.ArrayList;
import java.util.EmptyStackException;
import java.util.List;
public class IntStack {
private List<Integer> stack ;
public IntStack () {
stack = new ArrayList<>();
}
public boolean empty () {
return stack.size() == 0 ;
}
// push and pop operations
public void push ( Integer obj ) {
stack.add(obj);
}
public Integer pop () {
if (stack.size() <= 0) {
throw new EmptyStackException();
}
return stack.remove(stack.size() - 1);
}
public Integer peek () {
if (stack.size() == 0) {
return null;
}
return stack.get(stack.size() - 1);
}
public boolean hasNext() {
return ( stack.size() > 0 );
}
@Override
public String toString() { return stack.toString(); }
public static void main(String [] args) {
IntStack istack = new IntStack();
istack.push(1);
istack.push(2);
System.out.println(istack);
System.out.println(istack.pop());
System.out.println(istack);
}
}
Based on what you know about Java Generics, convert the IntStack into a generic SimpleStack that can be used to store objects of any class type.
![](/static/compass_v2/shared-icons/check-mark.png)
Step by step
Solved in 4 steps with 4 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Concepts of Database Management](https://www.bartleby.com/isbn_cover_images/9781337093422/9781337093422_smallCoverImage.gif)
![Prelude to Programming](https://www.bartleby.com/isbn_cover_images/9780133750423/9780133750423_smallCoverImage.jpg)
![Sc Business Data Communications and Networking, T…](https://www.bartleby.com/isbn_cover_images/9781119368830/9781119368830_smallCoverImage.gif)