Results 1 to 10 of 10

Thread: Stack problem

  1. #1
    Clown is offline Member
    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0

    Default Stack problem

    Hi everyone. I am trying to write a simple method,which prints all of the elements of a stack.

    import java.util.*;

    public class Stackcalculator extends java.util.Stack

    {
    public void show(){
    private LinkedList<Double> stack = new LinkedList<Double>();
    double []b=new double[100];
    int i=0;
    while (stack.empty()==false){
    b[i]=stack.pop();
    System.out.println(""+b[i]);
    i++;
    }
    }
    }

    The problem is that I get an error message saying that "method empty is not recognized" although the method is actually in the java class stack. Any ideas what I am doing wrong? I am using Bluej.

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Your stack variable is not a Stack, it's a LinkedList<Double>. LinkedList has no empty() method.

    -Gary-

  3. #3
    Clown is offline Member
    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0

    Default

    Thanx. Any ideas how i can actually do it?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,571
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Clown View Post
    Thanx. Any ideas how i can actually do it?
    How about reading the API documentation for the LinkedList class and the List interface? Alsways read and study first before you start guessing.

    kind regards,

    Jos

  5. #5
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    It's not at all clear what you're trying to do, but if it's really "print all the elements of a stack" (in which case, what are we trying to do with LinkedList<Double>?) then it would be good to take a look at the documentation for java.util.Stack. Hint: do you know how to print all the elements of a Vector?

    -Gary-

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,571
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by gcalvin View Post
    java.util.Stack
    I personally find that a stack on steroids because it extends the Vector class and inherits all of its methods that are none of a stack's business. Its a typical example of a too strong super class and a too weak sub class. But it indeed is a stack ...

    kind regards,

    Jos

  7. #7
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by JosAH View Post
    I personally find that a stack on steroids because it extends the Vector class and inherits all of its methods that are none of a stack's business. Its a typical example of a too strong super class and a too weak sub class. But it indeed is a stack ...

    kind regards,

    Jos
    I agree, and I don't see what's useful about printing all the elements on a stack in what appears to be a stack-based calculator app (guessing from the class name) but at least it's easy to do when your Stack is a Vector.

    -Gary-

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,571
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by gcalvin View Post
    I agree, and I don't see what's useful about printing all the elements on a stack in what appears to be a stack-based calculator app (guessing from the class name) but at least it's easy to do when your Stack is a Vector.
    The OP would be a good sport if s/he'd use a second stack like this:

    Java Code:
    while (!stack1 is empty) {
       T elem= pop from stack1
       // <--- either print elem here
       push elem on stack2
    }
    while (!stack2 is empty) {
       T elem= pop from stack2
       // <--- or print elem here
       push elem on stack1
    }
    kind regards,

    Jos

  9. #9
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by JosAH View Post
    The OP would be a good sport if s/he'd use a second stack like this:

    Java Code:
    while (!stack1 is empty) {
       T elem= pop from stack1
       // <--- either print elem here
       push elem on stack2
    }
    while (!stack2 is empty) {
       T elem= pop from stack2
       // <--- or print elem here
       push elem on stack1
    }
    kind regards,

    Jos
    True, as long as we're not worried about thread safety (which we probably aren't in this app).

    -Gary-

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,571
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by gcalvin View Post
    True, as long as we're not worried about thread safety (which we probably aren't in this app).
    Multiple threads won't hurt much either because all the surgery is done in the Vector class and those methods are all synchronized ;-)

    kind regards,

    Jos

Similar Threads

  1. recursion and call stack problem
    By OptimusPrime in forum New To Java
    Replies: 4
    Last Post: 12-26-2009, 09:49 PM
  2. Stack problem..pls help
    By Mika in forum New To Java
    Replies: 1
    Last Post: 02-16-2009, 08:10 AM
  3. Problem in Calculator implementation using Stack
    By realahmed8 in forum New To Java
    Replies: 1
    Last Post: 12-19-2008, 11:58 PM
  4. Replies: 1
    Last Post: 11-17-2008, 09:32 AM
  5. Help with heap and stack
    By coco in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 02:21 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •