aspose file tools*
The moose likes Java in General and the fly likes stack program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "stack program" Watch "stack program" New topic
Author

stack program

donnie devis
Greenhorn

Joined: Aug 24, 2011
Posts: 4
i want help to build a java program on the following interface
please its very urgent...



fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

Welcome to the ranch. We're glad you're here, but we really don't work this way. Nothing here is "urgent". We don't do homework for you. overly broad questions tend to be ignored.

Technically, you have not even ASKED a question.

You may want to read our FAQ on how to ask questions here.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
donnie devis
Greenhorn

Joined: Aug 24, 2011
Posts: 4
im sorry for my misconduct....
but one thing can you explain me what does these line of code mean


...

can a stack program be sortable.... if yes then the logic of LIFO is nowhere....
and what does that E stand for???
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19785
    
  20

And whoever wrote that interface doesn't know that method documentation should come before the method, not after.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

Yes, I agree, the "sortable" part of the name doesn't seem to make much sense. I don't see any methods which necessarily cause anything to be sorted.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19785
    
  20

Donny Davis wrote:im sorry for my misconduct....
but one thing can you explain me what does these line of code mean


...

can a stack program be sortable.... if yes then the logic of LIFO is nowhere....
and what does that E stand for???

The E is the generic element type. If you would have an ISortableStack<String> it would only be able to handle Strings, and an ISortableStack<Integer> would only be able to handle Integers.
The stack doesn't need to be sortable - its elements should be. If you read more closely, the extends Comparable<E> applies to E. Therefore, you can create an ISortableStack<String> (because String implements Comparable<String>), but not an ISortableStack<InputStream>, or even an ISortableStack<java.sql.Timestap>. InputStream doesn't even implement Comparable, and java.sql.Timestamp does not implement Comparable<java.sql.Timestamp> but Comparable<java.util.Date> instead (through inheritance).

As for the LIFO logic - ISortableStacks can only be modified by using pop() and push(), making it behave like a regular LIFO stack. The sortable only applies to the read-only methods peekMidElement(), peekHighestElement() and peekLowestElement().
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4490
    
    8

The E indicates a generic type. Oracle has quite a detailed tutorial on using generics, which is quite a big topic. The short answer is that E stands for some type that is unknown at the moment but will be specified when the class is created. That way you can have a stack of Strings, or a stack of anything else you want.

In terms of sorting: it looks to me as if it's supposed to behave like a common stack, but with convenience methods for finding the highest and lowest values.

Edit - or what Rob said.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

Donny Davis wrote:im sorry for my misconduct....

Not a big deal... As you see, when you asked a specific question, you got lots of help.

Welcome to the Ranch!
donnie devis
Greenhorn

Joined: Aug 24, 2011
Posts: 4
hey guys thankx for your replies
so i will make a class named stack....
it will implement an interface named Isortable stack
ok
im planning to use an array
for the code <<E extends comparable<<E>> i will write code for comparing integers and that too in methods for the highest n the lowest ones
and pop and push will just insert and remove elements from the stack.....
peekmidelement will return the middle element

i hope im right in my idea
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Remember the Capital Letters in real life. And if you want sorting, the generic type is

<E extends Comparable<? super E>>

That allows for a supertype of E having implements Comparable<Foo> in its code, rather than E itself.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19785
    
  20

Like the java.sql.Timestamp class I mentioned earlier.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

Rob Spoor wrote:And whoever wrote that interface doesn't know that method documentation should come before the method, not after.


Assuming the documentation is misplaced in that way, the docs for peekMidElements are incorrect. Copied and pasted from peekHighestElement but not modified to indicate the actual requirement.
donnie devis
Greenhorn

Joined: Aug 24, 2011
Posts: 4
ya the documentation was misplaced ......

anyways thank you guys
i coded the program.... n it is working fine.

thanks a lot
srikanth sharma
Greenhorn

Joined: Aug 31, 2011
Posts: 1

Can you please mail your program

my mail id is [deleted]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Welcome to the Ranch srikanth sharma.

We do not like discussions off the forum, so I have deleted your e-mail address.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
You should only send people your program about ten minutes before it is due to be handed in, and if they are at the same college. Then they will get 0 because of plagiarism . . .



. . . but you will get sent down for letting them plagiarise you
parth doshi
Greenhorn

Joined: Aug 31, 2012
Posts: 1
I am trying to do exactly the same but for queue

my interface looks like this:

public interface ExamPeekableQueue<E extends Comparable<E>>
{
public void enqueue(E e);

public E dequeue();

public int size();

}


I now want to create a class. Here is what I tried but I need some help to complete the code. Please help me. I need to code this asap

public class ExamPeekableQueueImpl implements ExamPeekableQueue
{

@Override
public void enqueue(Comparable e)
{
// TODO Auto-generated method stub

}

@Override
public Comparable dequeue()
{
// TODO Auto-generated method stub
return null;
}


@Override
public int size()
{
// TODO Auto-generated method stub
return 0;
}

public static void main(String args[])
{
ExamPeekableQueueImpl e=new ExamPeekableQueueImpl();
List<Integer> list=new ArrayList();
list.add(1);
list.add(2);
list.add(3);

for(int i=0;i<list.size();i++)
e.enqueue(new Integer(5));

System.out.println(e.size());
}


How do i write the code for enqueue, deque functions...please just point me to a solution and i'll try to code the same
Vishal Shaw
Ranch Hand

Joined: Aug 09, 2012
Posts: 179
parth doshi wrote:I am trying to do exactly the same but for queue

my interface looks like this:

public interface ExamPeekableQueue<E extends Comparable<E>>
{
public void enqueue(E e);

public E dequeue();

public int size();

}


I now want to create a class. Here is what I tried but I need some help to complete the code. Please help me. I need to code this asap

public class ExamPeekableQueueImpl implements ExamPeekableQueue
{

@Override
public void enqueue(Comparable e)
{
// TODO Auto-generated method stub

}

@Override
public Comparable dequeue()
{
// TODO Auto-generated method stub
return null;
}


@Override
public int size()
{
// TODO Auto-generated method stub
return 0;
}

public static void main(String args[])
{
ExamPeekableQueueImpl e=new ExamPeekableQueueImpl();
List<Integer> list=new ArrayList();
list.add(1);
list.add(2);
list.add(3);

for(int i=0;i<list.size();i++)
e.enqueue(new Integer(5));

System.out.println(e.size());
}


How do i write the code for enqueue, deque functions...please just point me to a solution and i'll try to code the same


Welcome to ranch.

As for your post , fred already said that we do not do homework for you. I would suggest you to read your Datastructure well. The logic of a queue is very realistic, I am sure you'd enjoy it. I am also posting a link for you


Programming is about thinking, NOT coding
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: stack program