Results 1 to 3 of 3

Thread: Beginners Help Create a Stack

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2002
    Posts
    159

    Beginners Help Create a Stack

    How would i create a stack called thefuture and adds the following strings to it: "Hello", "Java", "will", "be", "replaced", "by", "CSharp"

    i have had a go but im not sure if it's correct. can anyone update it so all the brackets are in the correct place and the language grammer is correct?

    Thanks

    james

    Code:
    public class Stack extends Vector
    
    Stack(thefuture)
    {
    Object push(Object "Hello");
    Object push(Object "Java");	
    Object push(Object "will");	
    Object push(Object "be");	
    Object push(Object "replaced");	
    Object push(Object "by");	
    Object push(Object "CSharp");
    }
    Last edited by NOTSOSURE; May 20th, 2004 at 05:30 AM.

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    I would just create an instance of the stack class then push the objects into the stack.

    Code:
    import java.util.*; 
    
    Stack s =  new Stack(); 
    s.push(new Integer(35)); 
    Integer i  = (Integer) s.pop();
    push takes an object as an arguement so Object push(Object "Hello"); should just be push("Hello") without the return type.

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    You could do somthing like this if you wanted to, passing in an stack to the constructor.
    Code:
     public class X{
    
       private Stack thefuture; 
    
       public X(Stack thefuture){
           this.thefuture = thefuture; 
       }
    
       public push(Object x){
         thefuture.push(x); 
       }
    
       public Object pop(){
         return  thefuture.pop();
      }
     }

Posting Permissions

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



Click Here to Expand Forum to Full Width