Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

stack program

 
donnie devis
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want help to build a java program on the following interface
please its very urgent...



 
fred rosenberger
lowercase baba
Bartender
Posts: 12202
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
donnie devis
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And whoever wrote that interface doesn't know that method documentation should come before the method, not after.
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12202
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50273
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like the java.sql.Timestamp class I mentioned earlier.
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya the documentation was misplaced ......

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

thanks a lot
 
srikanth sharma
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Can you please mail your program

my mail id is [deleted]
 
Campbell Ritchie
Sheriff
Pie
Posts: 50273
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50273
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic