Solved

MyStack()

Posted on 2004-10-26
308 Views
Last Modified: 2010-03-31
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
0
Question by:Coconut77840
    14 Comments
     
    LVL 92

    Expert Comment

    by:objects
    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
     

    Author Comment

    by:Coconut77840
    Is the ms.insertElementAt() and removeElementAt needed for this program?

    0
     
    LVL 92

    Expert Comment

    by:objects
    not where u are using it no, your stack just needs push() and pop() doesn't it.
    0
     

    Author Comment

    by:Coconut77840
    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
     
    LVL 92

    Expert Comment

    by:objects
    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
     

    Author Comment

    by:Coconut77840
    How do I modify the methods to add/remove elements from the vector?
    0
     

    Author Comment

    by:Coconut77840
    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
     

    Author Comment

    by:Coconut77840
    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
     
    LVL 1

    Expert Comment

    by:senico

    //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
     

    Author Comment

    by:Coconut77840
    The MyStack class is not public
    0
     

    Author Comment

    by:Coconut77840
    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
     

    Author Comment

    by:Coconut77840
    Got it to work.

    Thanks
    0
     
    LVL 92

    Accepted Solution

    by:
    >            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
     

    Author Comment

    by:Coconut77840
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Course: MongoDB Object-Document Mapper for NodeJS

    NodeJS (JavaScript on the server) is awesome, but some developers get confused about NoSQL when it comes to working in Node with MongoDB (NoSQL database). Do you need a better explanation of how to use Node.js with MongoDB? The most popular choice is the Mongoose library.

    Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
    Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    875 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now