Generic Java Stack
Hi here the question the interviewer asked me-
package jp.co.worksap.intern;
/**
*The Stack class represents a last-in-first-out(LIFO) stack of objects
*And this class can look at the object which has the highest (lowest or so) value.
*So every object onto the stack must be comparable to each other.
*@param<E.
*/
public interface ISortableStack<E extends Comparable<E>>{
/**
*Pushes an item onto the top of this stack.
*If the Element e is null, throws NullPointerException.
*
*
@param e
*@throws NullPointerException
*/
public void push(E e);
/**
*Removes the object at the top of this stack and returns that object as the value of this function.
*If the stack is empty, throws EmptyStackException.
*@return
*@throws EmptyStackException
*/
public E pop();
/**
*Locks at the object which has the middle value among the all objects without removing it from the stack.
*Returns the object which has the value of following order <code>(size()/2)+1</code>
*<pre>
*e.g.
*When the stack has the following values (1, 2, 5, 4, 2, 6)
*this method returns 4 and doesn't remove the object.
*</pre>
*
*If the stack is empty, throws EmptyStackException.
*@return
*@throws EmptyStackException
*/
public E peekMidElement();
/**
*Looks at the object which has the highest value among the all objects without removing it from the stack.
*Returns the object which has the value of the following order <code>size()</code>
*<pre>
*e.g.
*When the stack has the following values (1,2,5,4,2,6)
*this method returns 6 and doesn't remove that object
*</pre>
*
*If the stack is empty, throws EmptyStackException.
*@return
*@throws EmptyStackException
*/
public E peekHighestElement();
/**
*Looks at the object which has the lowest value among the all objects without removing it from the stack.
*Returns the object which has the value of the following order <code>1</code>
*<pre>
*e.g.
*When the stack has the following values (1,2,5,4,2,6)
*this method returns 1 and doesn't remove the object.
*</pre>
*
*If the stack is empty, throws EmptyStackException.
*@return
*@throws EmptyStackException
*/
public E peekLowestElement();
/**
*Returns the number of objects in this stack.
*@return
*/
public int size();
}
Now i have implemented the Interface in Eclipse as per the statement shown below code
public class MyStackClass<E extends Comparable<E>> implements ISortableStack<E>{
private int A;
private Node first;
private class Node{
private E e;
private Node next;
}
public MyStackClass() {
first = null;
A = 0;
}
private ArrayList<E> listOne = new ArrayList<E>();
public void push(E e) {
// TODO Auto-generated method stub
Node oldfirst = first;
first = new Node();
first.e = e;
first.next = oldfirst;
A++;
}
@Override
public E pop() {
// TODO Auto-generated method stub
if (isEmpty()) throw new RuntimeException("Stack underflow");
E e = first.e; // save e to return
first = first.next; // delete first node
A--;
return e; // return the saved e
}
@Override
public E peekMidElement() {
// TODO Auto-generated method stub
if(listOne.size() <= 0){
throw new EmptyStackException();
}
return listOne.get(listOne.size()/2);
}
@Override
public E peekHighestElement() {
// TODO Auto-generated method stub
if(listOne.size() <= 0){
throw new EmptyStackException();
}
return listOne.get(listOne.size() - 1);
}
@Override
public E peekLowestElement() {
// TODO Auto-generated method stub
if(listOne.size() <= 0){
throw new EmptyStackException();
}
return listOne.get(0);
}
@Override
public int size() {
// TODO Auto-generated method stub
return A;
}
public boolean isEmpty() {
return first == null;
}
}
Now when i call main method the ISortableStack array list is unable to instantiate shown below.It throws error at ISortableStack as:Cannot make a reference to a non static type . and is not able to instatiate the ISORTABLESTACK.
import java.io.*;
public class DisplayStack {
E ch;
public static void main(String[] args) {
// TODO Auto-generated method stub
ISortableStack<Character> s = new ISortableStack<Character>(); //Cannot instantiate ISORTABLESTACK
char character;
DisplayStack demo = new DisplayStack();
while ((character = (char)System.in.read())!= '\n') //Cannot make reference to a non static type
if (!s.full())
s.push(character);
while (!s.empty())
System.out.print(s.pop());
System.out.println();
}
}
Please help me to get this code work.
View Answers
August 25, 2011 at 8:17 PM
Please help me in this one...
Ads
Related Tutorials/Questions & Answers:
Generic Java Stack
Generic Java Stack Hi here the question the interviewer asked me-
package jp.co.worksap.intern;
/**
*The
Stack class represents a last-in-first-out(LIFO)
stack of objects
*And this class can look at the object
stack in java/
stack in
java/
java code for
stack push pop and display operation?
thanks
Advertisements
Java collection Stack example
Java collection
Stack example How to use
Stack class in
java collection?
The
Stack class uses the First In Last Out(FILO) mechanism... :- -1
Description:- The above example demonstrates you the
Stack class in
java
Stack
Stack How to implement a
stack using classes in
java?
Here is an example that implements a
Stack class based on arrays.
public class
Stack {
private int top;
private int[] storage;
Stack(int
Java Stack Example
Java Stack Example
In this section you will learn about
Stack class in
java, how to use
Stack in
java. Before going into details we should know what... the item in the
stack and how far it is from top
of
stack
Example : A
Java code
Java Stack
Java Stack
The
Stack class works on the principle
last-in-first-out (LIFO)
stack of objects...
java stack click on the link:
http:/www.roseindia.net/
java/beginners/
stack
Java Stack Example
Java Stack Example
In this section we will read about how you can provide... and there is no more element is to be
removed i.e. the
stack is empty.
In
Java Stack...
the
stack collection in the
Java programming. In this example you will see how
Implementing a Stack in Java
Implementing a
Stack in
Java
In this section, you will learn how to implement a
stack in
Java. A
Stack is like a bucket in which you can put elements one-by-one
What are Java full stack technologies?
What are
Java full
stack technologies? Hi,
What are all...?
What are
Java full
stack technologies?
Is
Java still popular in 2018... full
stack.
What are
Java full
stack technologies?
Here is are the list of
Java
Java-Generic Interface
Java-
Generic Interface Hi Friends,
The current connector... and publish it to
java program
-Now we are getting request from other modules in our... replace C connector by
java program which can receive xml messages and parse
calculator in java with stack
calculator in
java with stack i want calcultor with interface in
java and in interface there is button called postfix ,,, when the user enter opertions and numbers first check if is vaild or not then convert to postfix
Stack Overflow - Java Tutorials
Stack Overflow in
Java
A
stack is the part of the memory. The local automatic variable is created on
this
stack and method arguments are passed. When a process starts, it get a
default
stack size which is fixed for each process. 
Use a tree stack to sort number in java?
Use a tree
stack to sort number in
java? The Question is :
Three stacks can be used to sort a list of numbers. Assuming
stack in holds the input list of numbers,
stack out is to hold the output list after sorting the numbers
Use a tree stack to sort number in java?
Use a tree
stack to sort number in
java? The Question is :
Three stacks can be used to sort a list of numbers. Assuming
stack in holds the input list of numbers,
stack out is to hold the output list after sorting the numbers
Use a tree stack to sort number in java?
Use a tree
stack to sort number in
java? The Question is :
Three stacks can be used to sort a list of numbers. Assuming
stack in holds the input list of numbers,
stack out is to hold the output list after sorting the numbers
Use a tree stack to sort number in java?
Use a tree
stack to sort number in
java? The Question is :
Three stacks can be used to sort a list of numbers. Assuming
stack in holds the input list of numbers,
stack out is to hold the output list after sorting the numbers
Use a tree stack to sort number in java?
Use a tree
stack to sort number in
java? The Question is :
Three stacks can be used to sort a list of numbers. Assuming
stack in holds the input list of numbers,
stack out is to hold the output list after sorting the numbers
Use a tree stack to sort number in java?
Use a tree
stack to sort number in
java? The Question is :
Three stacks can be used to sort a list of numbers. Assuming
stack in holds the input list of numbers,
stack out is to hold the output list after sorting the numbers
Example of Java Stack Program
Example of
Java Stack Program
Stack is like a bucket we you can enter objects and retrieve
it. Here in the example describes the methods to prepare an
example of
Java stack
Java get Stack Trace
Java get
Stack Trace
In this section, you will learn how to obtain the
stack trace... to print out the exception
stack trace to catch the error instead
of using
Java get Stack Trace as String
Java get
Stack Trace as String
In this section, you will learn how to get the
stack trace...(printWriter)- This method prints the
throwable exception and its
stack trace
Java generic
.style1 {
color: #000080;
}
Java generic
Java 1.5 provides number of new features to the
Java Language including
"
Java Generics". Using generics makes
Array stack
Array stack Write a
stack class ArrayStack.java implements PureStack interface that reads in strings from standard input and prints them in reverse order
Array stack
Array stack Write a
stack class ArrayStack.java implements PureStack interface that reads in strings from standard input and prints them in reverse order
Which gives better performace Stack vs Arraylist
Which gives better performace
Stack vs Arraylist I want to implemet a
generic Stack implementation. I am confused whether to use java.util.Stack class or to simulate ArrayList as
Stack?
I am very much concerned about performance