Results 1 to 8 of 8
Thread: Stacks, lists...
- 07-31-2008, 03:41 PM #1
Member
- Join Date
- Jun 2008
- Posts
- 54
- Rep Power
- 0
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);
}
}
- 07-31-2008, 04:37 PM #2
Is the the main() method you are refering to?exactly what to write in the public static void
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.
This method definition does not do what it's comment describes.public void length(int length)
//gives out the length of the array;
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.
- 07-31-2008, 06:28 PM #3
Member
- Join Date
- Jun 2008
- Posts
- 54
- Rep Power
- 0
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...
- 07-31-2008, 06:44 PM #4
push usually means to put something on the stack
pop to take it off & return it
- 08-01-2008, 03:31 PM #5
Member
- Join Date
- Jun 2008
- Posts
- 54
- Rep Power
- 0
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
- 08-01-2008, 03:49 PM #6
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.what it means... especially the Array⁄ndexOutOfBoundsException
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
- 08-02-2008, 11:48 AM #7
Member
- Join Date
- Jun 2008
- Posts
- 54
- Rep Power
- 0
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
- 08-02-2008, 01:59 PM #8
Similar Threads
-
Stacks
By Zosden in forum Advanced JavaReplies: 15Last Post: 05-05-2008, 08:16 AM -
2 dimensional Lists
By gapper in forum New To JavaReplies: 4Last Post: 01-20-2008, 09:01 AM -
Using Stacks
By ravian in forum New To JavaReplies: 7Last Post: 11-28-2007, 09:53 AM -
I want to be able to do this with stacks and queues as well as with vectors
By carl in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:05 AM -
Tudu Lists 2.0
By JavaBean in forum Java SoftwareReplies: 0Last Post: 07-11-2007, 03:32 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks