-2

I have my stack, with its corresponding methods, in class A. Now in class B, I want to create a new stack, calling the methods from class A. What I want to do, specifically, is peek at the elements of the stack in class A and then add the ones I like to the new stack in class B. Can someone help me out? I'm quite new at this! P.S. I do want to know specifically how to do this across classes; I can figure out how to do it in the same class, but I'm trying to broaden my understanding of calling methods across classes. Thank you!

EDIT: Someone asked for the code I refer to: newStack.push(oldStack.(StackOne).peek());

I don't have that right, but here are the elements: StackOne is the name of my first class, in which I've created the first stack, and oldStack is in that class. I want to peek at these elements from StackTwo (my second class). If I like an element, I would push it from oldStack to newStack (ultimately resulting in something similar to what I've posted above, but that doesn't work).

editor
  • 3
  • 2
  • 1
    include the code for the classes you made in here – Multithreader Sep 04 '16 at 04:37
  • It's unclear where you are stuck. You can call `public` methods from any other class, and you can call `protected` methods from subclasses or other classes in the same package. – 4castle Sep 04 '16 at 04:42
  • I advise reading class vs object. Also, static vs non-static methods. It seems the concept is not clear for you yet. – Multithreader Sep 04 '16 at 04:46
  • Are there any getter methods for the `oldStack` in your `StackOne` class? We need to see what methods you have already created and what access modifiers you have used. `peek` and `push` are not methods you have written, and it's not what we're asking for. – 4castle Sep 04 '16 at 04:51
  • I have created my own peek method in Class A; I created all stack methods from scratch for practice. Looking at what I posted above -- newStack.push(oldStack.(StackOne).peek()); -- what exactly is wrong there? I've seen methods called across classes in which a stack that is peeked at in one class can be pushed to a stack in another class, so I know it can't be that difficult. – editor Sep 04 '16 at 04:51
  • If you've done proper object-oriented programming, the `oldStack` of `StackOne` should not be directly accessible from `StackTwo`. We can show you the wrong way to do things, or you could show us your class structure and we can show you the right way to do things. – 4castle Sep 04 '16 at 04:53
  • Show me the wrong way then, please. If you can't answer the question, please let someone talk who can. I do pass one parameter, which is the stack itself. What I want to access is the methods from the first class. Thanks! – editor Sep 04 '16 at 04:54
  • `this.newStack.push(new StackOne().oldStack.peek())` would be the wrong way to do it. – 4castle Sep 04 '16 at 04:55
  • And seriously: this is absolutely basic stuff - you get that from reading books and stuff. The only reason why people spend their time explaining stuff to you is ... because it is Sunday, and some people have too much time. The normal reaction to such questions would be quick downvotes and close requests! Really: never ever ask people to "show me the wrong way please". – GhostCat Sep 04 '16 at 04:59
  • Btw, don't use the `java.util.Stack` class in real production code. Use an `ArrayList` instead as your backing structure. `Stack` extends from `Vector`, and `Vector` is [all but deprecated](http://stackoverflow.com/q/1792134/5743988). – 4castle Sep 04 '16 at 05:07

2 Answers2

0

Here is a class that incapsulates a stack inside of it.

MyStack.java

import java.util.Stack;

public class MyStack<T> {
    private Stack<T> stack;

    public MyStack() {
        stack = new Stack<T>();
    }

    public T peek() {
        return stack.peek();
    }

    public void push(T item) {
        stack.push(item);
    }
}

Main.java

public class Main {
    public static void main(String[] args) {
        MyStack<String> myStackA = new MyStack<String>();
        MyStack<String> myStackB = new MyStack<String>();

        myStackA.push("Hello");
        myStackA.push("Hi");

        myStackB.push(myStackA.peek());

        System.out.println(myStackA.peek()); //prints Hi
        System.out.println(myStackB.peek()); //prints Hi
    }
}
Multithreader
  • 878
  • 6
  • 14
  • Very helpful, Multithreader! Now what if I wanted to create each stack in its own class instead of both in the main? Could I still access peek from one class and push to a stack in another class? – editor Sep 04 '16 at 05:12
  • Here is something to learn the difference between class and object. https://www.youtube.com/watch?v=MeP1CztNMdo – Multithreader Sep 04 '16 at 05:43
  • Thank you! I do understand the high-level concept; it's how to translate that to code that I am having difficulty as a beginner. – editor Sep 04 '16 at 14:08
0

I have made one program using which you can understand how to get/peek value from on stack located in one class(in my case A) to store it into other stack located at other class(in my case B)..

import java.util.*;
class A{
    public Stack st;
        public A(){
        st = new Stack();
    }
    public void pushA(Integer a){
    st.push(new Integer(a));
    }
    public Integer popA(){
    return (Integer)st.pop();
    }
    public Integer peekA(){
    return (Integer)st.peek();
    }
}
class B{
    public Stack st;
    public B(){
        st = new Stack();
    }
    public void pushB(Integer b){
    st.push(b);
    }
    public Integer popB(){
    return (Integer)st.pop();
    }
    public Integer peekB(){
    return (Integer)st.peek();
    }
}
class Main{
    public static void main(String ar[]){
    A stackOfA = new A();
    stackOfA.pushA(12);
    stackOfA.pushA(34);
    stackOfA.pushA(66);
    B stackOfB = new B();
    stackOfB.pushB(stackOfA.peekA());
    System.out.println("A:"+stackOfA.st);
    System.out.println("B:"+stackOfB.st);
    }
}

Output:

A:[12, 34, 66]
B:[66]
M.Usman
  • 2,049
  • 22
  • 25
  • That is awesome; thank you! When I get enough reputation points, I will come back and upvote this. ;-) – editor Sep 04 '16 at 05:28