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

Thread: Why create eclipse this output ?

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Why create eclipse this output ?

    Hi friends,

    For my homework I must try to guess the possible output but its very difficult.

    This is the code
    package mix5;
     
    public class Mix5 {
     
     
    	int counter = 0;
     
    	public static void main(String[] args) {
     
    		int count = 0;
    		Mix5[] m5a = new Mix5[20];
    		int x = 0;
    		while ( x < 9) {
    			m5a [x] = new Mix5();
    			m5a[x].counter = m5a[x].counter + 1;
    			count = count + 1;
    			count = count + m5a [x].maybeNew(x);
    			x = x + 1;
    		}
    		System.out.println( count + " " 
    				 			+ m5a [1].counter);
    	}
    	public int maybeNew (int index) {
    		if (index < 5 ){
    			Mix5 m5 = new Mix5();
    			m5.counter = m5.counter + 1;
    			return 1;
    		}
    		return 0;
    	}
     
    }

    The output is "14 1"
    But how create eclipse this output? I tried and tried but it's not working for me. Can anybody explain this output?
    (Head First Java pg 90)
    Thanks

    Regards Manzara


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Why create eclipse this output ?

    Try adding lots of println statements that print out the values of variables as they are changed.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    manzara (March 28th, 2013)

  4. #3
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Why create eclipse this output ?

    Quote Originally Posted by manzara View Post
    how create eclipse this output?
    Do you mean "How do i create this output in eclipse?" I'm not sure exactly what your question is. This exercise in Head First Java is meant to show you how to follow objects and how they change value. I dont have the book in front of me so i cant really give you more than that. Do you have a more specific question than that? The current output just shows the value of count and the counter for the SECOND item in the array (arrays are zero based).
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  5. The Following User Says Thank You to Chris.Brown.SPE For This Useful Post:

    manzara (March 28th, 2013)

  6. #4
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Why create eclipse this output ?



    @Norm
    Thank you for quick feedback.
    I attached the page from the java book.
    I know how to use the println statement but do not know where I can put the code into
    Could you please (learn) show me where I can put the println statements in the code ?

    The problem with this task is that I really do not know what the code(s) are doing. The book say nothing about .count I do not know what the return code is doing..
    I really want to learn java but the book is flying from one subject to the other. that irritates me

  7. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Exclamation Re: Why create eclipse this output ?



    @Norm

    Thank you for your quick response. I know the println statement.
    Could you (learn) show me where I can put the println statment into my code. I want to see how Eclipse is creating that answer 14 1 step by step.
    I really want to learn Java but the HeadFrist Java book is flying from one subject to the other.
    The book have not mentioned once about the . count and suddenly I see in my practice assignment a. count


    @Chris.Brown.SPE
    Thank you for your quick response too. I attached the page of the book

  8. #6
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Why create eclipse this output ?

    I replied to this with a few images but I don't see my other replys

  9. #7
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Why create eclipse this output ?

    I dont see any other replys either. If you are having issues with images you can always host them on imgur.com and link them.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  10. The Following User Says Thank You to Chris.Brown.SPE For This Useful Post:

    manzara (March 28th, 2013)

  11. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Why create eclipse this output ?

    The posts were moderated (probably because of the images). Everyone should be able to see them now.
    An example of using println:
    	m5.counter = m5.counter + 1;    //<<<<<<<< Existing statement
            System.out.println("index="+index + ", m5.counter="+m5.counter);
    If you don't understand my answer, don't ignore it, ask a question.

  12. The Following User Says Thank You to Norm For This Useful Post:

    manzara (March 28th, 2013)

  13. #9
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Why create eclipse this output ?

    The example in the book wants you to follow the code given each of the candidate blocks and determine on your own what the output would be. They want you to understand how and when different objects are being updated. Why are you getting the particular answer? Take a look at the topic of the chapter and pages prior to the example. It will prolly give you some insight into what they are trying to demonstrate. Is there a particular question you have about the example? Or just confused of how it works?
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  14. The Following User Says Thank You to Chris.Brown.SPE For This Useful Post:

    manzara (April 3rd, 2013)

  15. #10
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Why create eclipse this output ?

    Hi Chris,

    I read every page of the book. And I'm confused because I do not understand what how a few code works. I use a lot of the DeBug mode to learn. But sometimes the codes are very difficult.

Similar Threads

  1. How to create Media Player Application on eclipse: Helios?
    By Dart_18 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 10th, 2012, 08:31 AM
  2. Replies: 1
    Last Post: October 20th, 2012, 12:21 PM
  3. Read txt file into array and create new output.
    By Margaret_Girl87 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 19th, 2012, 12:08 PM
  4. Replies: 1
    Last Post: December 30th, 2011, 03:29 AM