Grow your CSS skills. Land your dream job.

Struggling with JAVA!

  • # September 5, 2012 at 1:54 pm

    Can someone of u guys plz explain what’s happening in this program, Can someone plz explain it step by step, Thx in advance :)
    class Stack {
    int stck[] = new int[10];
    int tos;
    // Initialize top-of-stack
    Stack() {
    tos = -1;
    }
    // Push an item onto the stack
    void push(int item) {
    if(tos==9)
    System.out.println(“Stack is full.”);
    else
    stck[++tos] = item;
    }
    // Pop an item from the stack
    int pop() {
    if(tos < 0) {
    System.out.println(“Stack underflow.”);
    return 0;
    }
    else
    return stck[tos--];
    }
    }

    class TestStack {
    public static void main(String args[]) {
    Stack mystack1 = new Stack();
    Stack mystack2 = new Stack();

    // push some numbers onto the stack
    for(int i=0; i<10; i++) mystack1.push(i);
    for(int i=10; i<20; i++) mystack2.push(i);
    // pop those numbers off the stack
    System.out.println(“Stack in mystack1:”);
    for(int i=0; i<10; i++)
    System.out.println(mystack1.pop());
    System.out.println(“Stack in mystack2:”);
    for(int i=0; i<10; i++)
    System.out.println(mystack2.pop());
    }
    }

    OUTPUT OF THIS PROGRAM!

    Stack in mystack1:
    9
    8
    7
    6
    5
    4
    3
    2
    1
    0
    Stack in mystack2:
    19
    18
    17
    16
    15
    14
    13
    12
    11
    10

    # September 5, 2012 at 2:22 pm

    Sorry, don’t think you’ll get much help here, this is a JavaScript forum.

    You can try this JAVA forum -> javaprogrammingforums.com

    # September 5, 2012 at 7:04 pm

    As cizmic says, wrong forum. Other people may differ, but I’m not going to answer any more of your java questions after this one. However, a few tips for other forums:

    1) Please format your code correctly and wrap it with code tags. There’s a button in the comment editor to help. Use it.
    2) Please ask more refined questions. At the moment it’s like you’re not even trying. Do you even know what a class is, or where the code starts executing from? All code is by its nature a step by step guide to what it does.
    3) Please write out your questions in full. There is no character limit on this forum (or any forum I frequent). You appear to have spent no time writing or researching your post, so why expect other people to do what you won’t when writing a reply?
    4) Please keep similar posts to a single thread. You’ve started three threads now, two on the exact same code snippet and this one could easily have been added to the previous one if you had written a more sensible title.

    I strongly recommend the following essay:

    http://catb.org./~esr/faqs/smart-questions.html

    Specifically the Before You Ask section:

    http://catb.org./~esr/faqs/smart-questions.html#before

    As for your question:

    1) The program declares, instantiates and initialises two Stack objects.
    2) Initialisation declares and initialises an integer array (st[a]ck) with memory for 10 integers allocated to it.
    It also declares an integer, tos [top-of-stack], and two methods, pop and push.
    3) The Stack constuctor initialises tos, setting its value to -1.
    4) A loop is constucted by using the “for” keyword (a modification of “while”) and
    declaring and initialising an integer named “i” which has 1 added to it each time
    the code within the “for” loop is executed and its value if still less than 10.
    5) The code within the “for” loop executes the push method within the first Stack object,
    which will print “Stack is full” if the object’s tos variable is equal to 9, or add 1 to
    tos and add the integer passed to it to the stck array at the index equal to tos if not.
    6) This is then repeated with i starting at 10 and continuing until it is not less than 20
    with the second Stack object.
    7) The code then prints “Stack in mystack1:”.
    8) Using the same “for” expression as before, the code then prints the return value of the
    first Stack object’s pop method 10 times.
    9) The pop method prints “Stack underflow” if tos is less than 0, then returns 0. If not, then
    it returns the integer at the stck array index equal to tos, then subtracts one from tos.
    Strictly speaking it retrieves the integer, then subtracts one, then returns the integer it
    retrieved.
    10) The code then prints “Stack in mystack2:”.
    11) The process in 9) is repeated for the second Stack object.
    12) The software reaches the end of the code and returns.

    # September 5, 2012 at 7:41 pm

    Looks to me like potential homework answer seeker….

    # September 6, 2012 at 12:08 am

    @kgscott284 I agree.
    @samnizamani This website is good at members helping with code :: http://www.dreamincode.net/ :: but like Ben Walker mentioned, you need to show effort otherwise people will not help you as it seems you are trying to get people to do your work for you. Plus, if you read the comments // its actually telling you what the program is doing.

    I suggest moving this to [Solved]

    @BenWalker, right on brother…

    # September 6, 2012 at 6:11 am

    Marked as Solved

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".