• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

beginning java help...

Semidevil

Diamond Member
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??
 
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
 
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 🙂
 
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.
 
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.
 
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>
 
Back
Top