Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

MyStack()

Posted on 2004-10-26
14
Medium Priority
?
314 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
Comment
Question by:Coconut77840
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 4
14 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12417850
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
ID: 12417865
Is the ms.insertElementAt() and removeElementAt needed for this program?

0
 
LVL 92

Expert Comment

by:objects
ID: 12418036
not where u are using it no, your stack just needs push() and pop() doesn't it.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:Coconut77840
ID: 12418066
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
ID: 12418114
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
ID: 12418162
How do I modify the methods to add/remove elements from the vector?
0
 

Author Comment

by:Coconut77840
ID: 12418236
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
ID: 12418259
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
ID: 12418262

//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
ID: 12418271
The MyStack class is not public
0
 

Author Comment

by:Coconut77840
ID: 12418320
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
ID: 12418348
Got it to work.

Thanks
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 12418366
>            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
ID: 12418414
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

610 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