MyStack()

I am to write a Java class MyStack which extends Object and contains (is composed of) a java.util.Vector instance.  Define methods push(Object x) and pop() which add x to the top of a MyStack and remove and return the top element, respectively.

Now, that's what I have gotten as of yet:

public class  Mystack;
import java.util.Vector;
public static void main(String[] args){

    MyStack ms = new MyStack();


    ms.push("A");
    ms.push("B");
    ms.push("C");
    System.out.println(ms.pop());
    System.out.println(ms.pop());
    //ms.insertElementAt(  );  // Not sure whether this is usefull
    //ms.removeElementAt(  ); // same comment as above

Am I on the right track? What am I missing?

Thanks
Coconut77840Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
yes, you've just got your syntax a bit wrong.
Should be more like the following, and your next job would be to write the push() and pop() methods.

import java.util.Vector;

public class  Mystack
{
  public static void main(String[] args){

    MyStack ms = new MyStack();


    ms.push("A");
    ms.push("B");
    ms.push("C");
    System.out.println(ms.pop());
    System.out.println(ms.pop());
    //ms.insertElementAt(  );  // Not sure whether this is usefull
    //ms.removeElementAt(  ); // same comment as above
  }
}
0
Coconut77840Author Commented:
Is the ms.insertElementAt() and removeElementAt needed for this program?

0
objectsCommented:
not where u are using it no, your stack just needs push() and pop() doesn't it.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Coconut77840Author Commented:
What am I doing wrong here?

<code>

import java.util.*;

public class Hw5Pr3{
//       push() Method

        public void push(Object value) {
          int top;    // <-- not sure whether this should be defined as INT
              Object data;  // <--- Object ??
              data[top] = value;
          top++;
      }

//       pop()

        public Object pop()  {
          top--;
          return data[top];
       }

    public static void main(String[] args){

    MyStack ms = new MyStack();


    ms.push("A");
    ms.push("B");
    ms.push("C");
    System.out.println(ms.pop());
    System.out.println(ms.pop());
    System.out.println(ms.pop());


    }

}

</code>
0
objectsCommented:
firstly looks like the var 'data' should be a member variable, and shouyld be a Vector

public class Hw5Pr3{
   private Vector data = new Vector();
...



then modify your methods to add/remove elements from that vector.
0
Coconut77840Author Commented:
How do I modify the methods to add/remove elements from the vector?
0
Coconut77840Author Commented:
import java.util.*;

public class Hw5Pr2{
         private Vector data = new Vector();

//       push() Method

          public void push(Object value) {
            int top = 0;
            data(top) = value;
            top++;
        }

//       pop()

          public Object pop()  {
            top--;
            return data(top);
         }

    public static void main(String[] args){

    MyStack ms = new MyStack();


    ms.push("A");
    ms.push("B");
    ms.push("C");
    System.out.println(ms.pop());
    System.out.println(ms.pop());
    System.out.println(ms.pop());


    }

}


I am getting the following errors:

Hw5Pr2.java:10: cannot resolve symbol
symbol  : method data (int)
location: class Hw5Pr2
            data(top) = value;
            ^
Hw5Pr2.java:17: cannot resolve symbol
symbol  : variable top
location: class Hw5Pr2
            top--;
            ^
Hw5Pr2.java:18: cannot resolve symbol
symbol  : variable top
location: class Hw5Pr2
            return data(top);
                        ^
Hw5Pr2.java:23: cannot resolve symbol
symbol  : class MyStack
location: class Hw5Pr2
    MyStack ms = new MyStack();
    ^
Hw5Pr2.java:23: cannot resolve symbol
symbol  : class MyStack
location: class Hw5Pr2
    MyStack ms = new MyStack();
                     ^
5 errors

0
Coconut77840Author Commented:
Updated code:

import java.util.*;

public class MyStack{
         private Vector data = new Vector();

//       push() Method

          public void push(Object value) {
            int top = 0;
            data(top) = value;
            top++;
        }

//       pop()

          public Object pop()  {
            top--;
            return data(top);
         }
}
public class Hw5Pr2 {
    public static void main(String[] args){

    MyStack ms = new MyStack();


    ms.push("A");
    ms.push("B");
    ms.push("C");
    System.out.println(ms.pop());
    System.out.println(ms.pop());
    System.out.println(ms.pop());


    }

}
0
senicoCommented:

//try this one

import java.util.Vector;

public class  Mystack
{
  public static void main(String[] args)
  {
 
    //declare vector MyStack
    MyStack ms = new MyStack();

    //methods push()  ---> ms.addElement(Object x);
 
    //method pop()  ---> ms.removeElement(Object x);

   /*insert object into vector MyStack*/
    ms.addElement("A");
    ms.addElement("B");
    ms.addElement("C");

    /*Display vector MyStack*/
    System.out.println(ms);

    /*remove object into vector MyStack*/
    ms.removeElement("A");
    ms.removeElement("B");
    ms.removeElement("C");

    /*Display vector MyStack*/
     System.out.println(ms);
 
  }
}

you can refer more on this link:
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Vector.html

Good Luck...
0
Coconut77840Author Commented:
The MyStack class is not public
0
Coconut77840Author Commented:
I am down to 3 errors:

Hw5Pr6.java:24: cannot resolve symbol
symbol  : method data (int)
location: class MyStack
            data(top) = value;
            ^
Hw5Pr6.java:31: cannot resolve symbol
symbol  : variable top
location: class MyStack
            top--;
            ^
Hw5Pr6.java:32: cannot resolve symbol
symbol  : variable top
location: class MyStack
            return data(top);
                        ^
3 errors

Thanks
0
Coconut77840Author Commented:
Got it to work.

Thanks
0
objectsCommented:
>            data(top) = value;

should be:

data.add(value);

>            top--;

remove this line altogether

> return data(top);

should be:

return data.remove(data.size()-1);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Coconut77840Author Commented:
Thanks objects.

Mine also worked fine with the same output as yours.

// Here is the code I wrote:

<code>
import java.util.*;
public class Hw5Pr2 {

    public static void main(String[] args){

    MyStack ms = new MyStack();


    ms.push("A");
    ms.push("B");
    ms.push("C");
    System.out.println(ms.pop());
    System.out.println(ms.pop());
    System.out.println(ms.pop());
    }
}
class MyStack {
        // private Vector data = new Vector();
        private Object[] data = new Object[3];
         private int top = 0;
         public boolean empty() {
                 return top == 0;
         }
//       push() Method

          public void push(Object value) {
            data[top] = value;
            top++;
        }

//       pop()

          public Object pop()  {
            top--;

    }
}

</code>
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.