Results 1 to 8 of 8
  1. #1
    Join Date
    Jun 2008
    Posts
    54
    Rep Power
    0

    Default Stacks, lists...

    Hey,
    I have another problem again...
    This time I have to do stacks. This is a list, in which I can only put elements at the end fo the list and only get elemts from the end. So I have to write a programm which contains three main parts:

    public class Stack

    double[] array;
    int number;
    boolean yes;
    boolean no;

    public void length(int length)
    //gives out the length of the array;

    public boolean pop(int track)
    //if the list is not full, put in the number and return yes;
    //if the list is full, don't put the number into the list and return no;

    public void push(int track)
    //gives out the last element of the list, if list is not empty;

    so that's what I have to do, I tried it and it would be nice if anyone could have a look over it and maybe help me to get it work, because i don't know exactly what to write in the public static void method()


    public class Stack
    {
    double[] array;
    int number;
    boolean yes;
    boolean no;

    public void length(int length)
    {
    this.array = new double[length];

    for(int i = 0; i < array.length; i++)
    {
    array[i] = Integer.MIN_VALUE;
    }
    }

    public boolean pop(int track)
    {


    if(array[track] < array.length)
    {
    while(array[track] != Integer.MIN_VALUE)
    {
    track++;
    }
    array[track] = number;

    return yes;

    }

    else
    {
    return no;
    }
    }

    public void push(int track)
    {


    while(array[track] != number)
    {
    track++;
    }

    System.out.println(array[track]);
    }

    public static void main(String[] args)
    {
    Stack app = new Stack();
    app.length(2);

    }

    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,343
    Rep Power
    25

    Default

    exactly what to write in the public static void
    Is the the main() method you are refering to?
    I'd add some code to test the three methods to make sure they worked properly. Push a couple of item, test the length then pop them and see if they are returned in the correct order.
    public void length(int length)
    //gives out the length of the array;
    This method definition does not do what it's comment describes.
    public int length() {
    would make more sense.
    Use the constructor to set the length of the array.

    The comments for push and pop look like they are reversed.

  3. #3
    Join Date
    Jun 2008
    Posts
    54
    Rep Power
    0

    Default

    ok, thanks for your help, i changed the void to int... but what do u mean with

    "pop and push" are reversed.... or how could i changed it... my public static void main(String[] args) is my main method, but i don't know exactly how i shall proof the programm.... how i have to put the numbers in...

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,343
    Rep Power
    25

    Default

    push usually means to put something on the stack
    pop to take it off & return it

  5. #5
    Join Date
    Jun 2008
    Posts
    54
    Rep Power
    0

    Default

    Oh, I see.... i changed the push and pop methods.... but know... how can i proof, if it is actually working, i thought about puttung some number in it... but i don't know exactly how to do it... do i have to put them into the main method, like:
    app.length(2);
    app.push(0, 5);
    app.push(1, 33);
    app.push(2, 65);
    app.pop(0, 0);

    but i think it is not completely right, because i am getting an error message:

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
    at Stack.push(Stack.java:24)
    at Stack.main(Stack.java:60)

    I just don't know exactly what it means... especially the Array⁄ndexOutOfBoundsException...

    can u maybe help me with this stuff....

    little_polarbear

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,343
    Rep Power
    25

    Default

    what it means... especially the Array⁄ndexOutOfBoundsException
    If you have an array the contains 3 objects and you try to look at the element at index 3, or at index 100 or at index -1 that index is OutOfBounds. The bounds for a 3 element array is 0-2.
    The error message says that the array has less than 3 elements as the index 2 is past the end of the array.(out of bounds)

    You methods are incorrectlyl designed.
    push should take an argument and return true|false depending on if the stack is full
    pop should return a value and not have any arguments

  7. #7
    Join Date
    Jun 2008
    Posts
    54
    Rep Power
    0

    Default

    ok, i changed it, but there is still sth going wrong and i am not completely sure if i changed it right...


    public class Stack
    {
    double[] array;
    int number;
    boolean yes;
    boolean no;

    public int length(int length)
    {
    this.array = new double[length];

    for(int i = 0; i < array.length; i++)
    {
    array[i] = Integer.MIN_VALUE;
    }
    return length;
    }

    public boolean push(int track, int number)
    {


    if(array[track] < array.length)
    {
    while(array[track] != Integer.MIN_VALUE)
    {
    track++;
    }
    array[track] = number;

    return yes;

    }

    else
    {
    return no;
    }
    }

    public void pop(int track, int number)
    {


    while(array[track] != number)
    {
    track++;
    }

    System.out.println(array[track]);
    }

    public static void main(String[] args)
    {
    Stack app = new Stack();
    app.length(9);
    app.push(0, 5);
    app.push(1, 33);
    app.push(2, 65);
    app.pop(2, 0);

    }

    }


    little_polarbear

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,343
    Rep Power
    25

    Default

    still sth going wrong
    You need to show what you mean.
    You need to think what each method is supposed to do before you try to write code for it. Can you describe what each of the methods is supposed to do?
    Last edited by Norm; 08-02-2008 at 02:02 PM.

Similar Threads

  1. Stacks
    By Zosden in forum Advanced Java
    Replies: 15
    Last Post: 05-05-2008, 08:16 AM
  2. 2 dimensional Lists
    By gapper in forum New To Java
    Replies: 4
    Last Post: 01-20-2008, 09:01 AM
  3. Using Stacks
    By ravian in forum New To Java
    Replies: 7
    Last Post: 11-28-2007, 09:53 AM
  4. Replies: 1
    Last Post: 08-07-2007, 07:05 AM
  5. Tudu Lists 2.0
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 07-11-2007, 03:32 PM

Posting Permissions

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