Question

In: Computer Science

Java Generics (Javas built-in Stack) What are the problems?    class genStck {         Stack stk...

Java Generics (Javas built-in Stack)

What are the problems?

   class genStck {

        Stack stk = new Stack ();

        public void push(E obj) {           

             push(E);        

        }

        public E pop() {       

Object obj = pop();

        }

   }

  

    class Output {

        public static void main(String args[]) {

            genStck <> gs = new genStck ();

            push(36);

            System.out.println(pop());

        }

   }

Solutions

Expert Solution

HI,

Program:

import java.util.*;

//declare class with generic type <E>
class genStck<E> {
   Stack stk = new Stack();

   public void push(E obj) {
       stk.push(obj);
   }

   public E pop() {
       //return popped element
       return (E) stk.pop();
   }
}

//class with main method need to be public
public class Output {

   //main method
   public static void main(String args[]) {
       try {
           //we need to provide the Obect type in '<>'
           //like Integer/Double, String
           genStck<Integer> gs = new genStck<Integer>();
           gs.push(36);
           System.out.println(gs.pop());
       } catch (Exception e) {
           //printing errors if there any
           e.printStackTrace();
       }
   }
}

Output Screenshot:


Related Solutions

Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class Stack{ public Stack(){ // use LinkedList class } public void push(int item){ // push item to stack } public int pop(){ // remove & return top item in Stack } public int peek(){ // return top item in Stack without removing it } public boolean isEmpty(){ // return true if the Stack is empty, otherwise false } public int getElementCount(){ // return current number...
Write a small Java class called StoreAddStuff that uses generics to store two variables of type...
Write a small Java class called StoreAddStuff that uses generics to store two variables of type T passed in during construction, then returns their sum through a non-static method.
Task Generics: GenericStack Class. Java. package Modul02; public class GenericStack<E> { private java.util.ArrayList<E> list = new...
Task Generics: GenericStack Class. Java. package Modul02; public class GenericStack<E> { private java.util.ArrayList<E> list = new java.util.ArrayList<>(); public int size() { return list.size(); } public E peek() { return list.get(size() - 1); } public void push(E o) { list.add(o); } public E pop() { E o = list.get(size() - 1); list.remove(size() - 1); return o; } public boolean isEmpty() { return list.isEmpty(); } @Override public String toString() { return "stack: " + list.toString(); } } package Modul02; public class TestGenericStack...
0. Introduction. In this assignment you will implement a stack as a Java class, using a...
0. Introduction. In this assignment you will implement a stack as a Java class, using a linked list of nodes. Unlike the stack discussed in the lectures, however, your stack will be designed to efficiently handle repeated pushes of the same element. This shows that there are often many different ways to design the same data structure, and that a data structure should be designed for an anticipated pattern of use. 1. Theory. The most obvious way to represent a...
Suppose the interface and the class of stack already implemented, Write application program to ( java)...
Suppose the interface and the class of stack already implemented, Write application program to ( java) 1- insert 100 numbers to the stack                         2- Print the even numbers 3- Print the summation of the odd numbers
In simple Java language algorithm: Implement a static stack class of char. Your class should include...
In simple Java language algorithm: Implement a static stack class of char. Your class should include two constructors. One (no parameters) sets the size of the stack to 10. The other constructor accepts a single parameter specifying the desired size of the stack a push and pop operator an isEmpty and isFull method . Both return Booleans indicating the status of the stack Using the stack class you created in problem 1), write a static method called parse that parses...
Java programming! Implement a static stack class of char. Your class should include two constructors. One...
Java programming! Implement a static stack class of char. Your class should include two constructors. One (no parameters) sets the size of the stack to 10. The other constructor accepts a single parameter specifying the desired size of the stack a push and pop operator an isEmpty and isFull method . Both return Booleans indicating the status of the stack Change this cods according to instructions please: public class Stack { int stackPtr; int data[]; public Stack() //constructor { stackPtr=0;...
Write a java class program to convert from INFIX TO POSTFIX Using stack operations
Write a java class program to convert from INFIX TO POSTFIX Using stack operations
USE GENERICS TO WRITE THE JAVA CODE FOR THIS ASSIGNMENT In this assignment, rewrite two of...
USE GENERICS TO WRITE THE JAVA CODE FOR THIS ASSIGNMENT In this assignment, rewrite two of the following sorting methods (Insertion Sort, Selection Sort, Quick Sort, and Merge Sort) to sort ArrayList of objects using Comaprable interface. (60 points)
Implement our own stack class patterned after Java's Stack class. Start with a generic class that...
Implement our own stack class patterned after Java's Stack class. Start with a generic class that uses an ArrayList for storage of the elements: public class StackBox<E> { ArrayList<E> stack = new ArrayList<E>(); } Implement the following methods in StackBox: boolean empty() Tests if this stack is empty. E push(E item) Pushes an item onto the top of this stack. Returns item pushed. E pop() Removes the object at the top of this stack and returns that object as the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT