beginning java help...

Semidevil

Diamond Member
Apr 26, 2002
3,017
0
76
so right now, we are learning java, and we are at the part about stacks.

I"m still new to it and don't even know how to create a stack.

here is what I have.

public class MyStack extends Vector {
public MyStack(s1)
s1= new MyStack
}

is this the right format to create a new stack s1??
 

lozina

Lifer
Sep 10, 2001
11,711
8
81
Stack s1 = new Stack() ?

but if you're trying to make a new class behave like a Stack, do something like:

public class MyStack {

private ArrayList list;

public MyStack (int size) {
list = new ArrayList(size);
}

public void push (Object o) {
list.add(o);
}

public Object pop () {
return list.remove(list.size());
}

public Object peek () {
return list.get(list.size());
}
}

I think that oughta work fine
 

Alex

Diamond Member
Oct 26, 1999
6,995
0
0
Originally posted by: Semidevil
so right now, we are learning java, and we are at the part about stacks.

I"m still new to it and don't even know how to create a stack.

here is what I have.

public class MyStack extends Vector {
public MyStack(s1)
s1= new MyStack
}

is this the right format to create a new stack s1??


this is what i'd do:

public class MyStack extends Vector{

//define stack variables here like item_count and an object for the data

public MyStack(){
//this is the constructor put any initialization routines etc here
}

//define stack methods here like push, pop, peek, etc

} //end of stack

then in a separate file have:

MyStack s1 = new MyStack();

(make sure MyStack.class is compiled when you try to run this and it also in the same directory or part of the same package or it won't know what a 'MyStack' is...

hope this helps and if it does thx to Descartes for helping me learn what i know :)
 

manly

Lifer
Jan 25, 2000
12,916
3,687
136
The question is vague, so lozina helped out accordingly.

I do want to point out that Java's Stack incorrectly extends the Vector class. Consult with a good OOP book on the topic of is-a inheritance to find out why this is a mistake.

Do not emulate java.util.Stack in this regard if you're writing your own stack data type.
 

oog

Golden Member
Feb 14, 2002
1,721
0
0
Originally posted by: manly
The question is vague, so lozina helped out accordingly.

I do want to point out that Java's Stack incorrectly extends the Vector class. Consult with a good OOP book on the topic of is-a inheritance to find out why this is a mistake.

Do not emulate java.util.Stack in this regard if you're writing your own stack data type.

I agree. I would not inherit from Vector since I don't consider a Stack to be a type of Vector, or a specialization of a Vector. Whatever data structure you use internally to store the Stack information should be kept internal. Lozina's code does this correctly.
 

Alex

Diamond Member
Oct 26, 1999
6,995
0
0
i guess i wouldnt inherit vector either, i'd either use a linked list or array...
 

oog

Golden Member
Feb 14, 2002
1,721
0
0
Originally posted by: Semidevil


public class MyStack extends Vector {
public MyStack(s1)
s1= new MyStack
}

is this the right format to create a new stack s1??

I'm a little confused by what you're trying to do in your code. You're defining a new class called MyStack, which you're extending from Vector (I don't agree with this inheritance, but that's okay). Then you have a constructor declared in the line:

public MyStack(s1)

The constructor takes one parameter for which you did not supply a data type. You are missing { } around the body of your constructor. In your constructor, you try to assign a new instance of MyStack to s1 (which is also done incorrectly -- you need parentheses after "new MyStack"). This is in the process of trying to create an initial instance of MyStack.

Let's say you got rid of the syntactical errors, you would be left with a MyStack class that has a constructor that looks like this:

public MyStack(MyStack s1) {
s1 = new MyStack();
}

I'm not sure what use that constructor is. You could use it like this, I suppose:

MyStack v1, v2;
v1 = new MyStack(v2);

And both v1 and v2 would be assigned its own instance of MyStack? I'm not even sure that works, but that's my interpretation of the code.

Your original question was: "is this the right format to create a new stack s1??"

Assuming you correctly defined a class called MyStack elsewhere, then to create a new stack and assign it to a variable s1, I would just do this:

MyStack s1 = new MyStack();

<edit>grammatical error</edit>