Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: What would be the output of this?

  1. #1
    Member
    Join Date
    Feb 2011
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default What would be the output of this?

    This was a homework assignment given to me and I got the answer (listed later), but I was wondering if anyone could check it over and see if i'm correct:
    a)
    //Assume that you can store and retrieve variables of type int on stack
     
    item1 = 1;
    item2 = 0;
    item3 = 4;
    stack.push(item2); // push puts one item on top of stack
    stack.push(item1);
    stack.push(item1 + item3);
    item2 = stack.top; //retrieves node info at top of stack
    stack.push(item3*item3);
    stack.push(item2);
    stack.push(3);
    item1 = stack.top();
    stack.pop(); // pop() removes top item from stack
    System.out.println(item1 + " " + item2 + " " + item3);
    while(!stack.isEmpty()){
       item1 = stack.top();
       stack.pop();
       System.out.println(item1);
    }
    My result is:
    3 5 4
    5
    16
    5
    1
    0

    b)
    item1 = 4;
    item3 = 0;
    item2 = item1+1;
    stack.push(item2);
    stack.push(item2+1);
    stack.push(item1);
    item2 = stack.top();
    stack.pop();
    item1 = item2+1;
    stack.push(item1);
    stack.push(item3);
    while(!stack.isEmpty()){
    item3 = stack.top();
    stack.pop();
    System.out.println(item3);
    }
    System.out.println(item1 + " " + item2 + " " + item3);
    My result is:
    0
    5
    6
    5
    5 4 5


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: What would be the output of this?

    What happened when you wrote a small program that runs this?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Oct 2011
    Posts
    53
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: What would be the output of this?

    They look both correct. Not too hard with pen and paper (and of course I could not find any of them ).

Similar Threads

  1. Why is my output not right?
    By Bryan229 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 16th, 2011, 07:17 AM
  2. [SOLVED] Can't get output HELP
    By moodycrab3 in forum Object Oriented Programming
    Replies: 8
    Last Post: February 7th, 2011, 03:07 AM
  3. Help with output
    By jch02140 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 8th, 2011, 01:09 PM
  4. Why am I getting this output?
    By ptabatt in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 6th, 2010, 07:36 PM
  5. need help with output.
    By VictorCampudoni in forum What's Wrong With My Code?
    Replies: 0
    Last Post: June 24th, 2010, 11:25 PM