[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 320
  • Last Modified:

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
0
Coconut77840
Asked:
Coconut77840
  • 9
  • 4
1 Solution
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

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

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 9
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now