Results 1 to 6 of 6

Thread: Head First Java

  1. #1
    mackenzie1235 is offline Member
    Join Date
    Dec 2013
    Posts
    12
    Rep Power
    0

    Default Head First Java

    Hi all, I have a question about some puzzle in the book of Head First Java:

    Java Code:
    public class Mix4 {
        int counter = 0;
        public static void main (String [] args) {
            int count = 0;
            Mix4 [] m4a = new Mix4[20];
            int x = 0;
            while (........) {
               m4a[x] = new Mix4();
               m4a[x].counter = m4a[x].counter + 1;
               count = count + 1;
               count = count + m4a.maybeNew(x);
               x = x + 1;
          }
           System.out.println(count + " "+ m4a[1].counter);
       }
    
       public int maybeNew(int index) {
           if (......) {
             Mix4 m4 = new Mix4();
             m4.counter = m4.counter + 1;
             return 1;
          }
          return 0;
        }
    }
    The question is to find possible candidates and output for the while and if statements. Possible candidates are (x < 9 / index <5) and ( x < 20 / index <5).

    My question is not about the possible answers, but about the reasoning behind this class Mix4.
    Am I correct on the following:

    An integer variable counter is created and set to zero. Then a next variable count is created and set to zero. Then an array m4a is created. Then the variable x is created and set to zero. Then a while statement (can be x < 9 or x < 20 doesn't matter) and subsequently 9 or 20 m4a's are created based on the value of x. Then the dot operator is used with "counter" on the array of m4a. Is counter a standard API package or something? Then the m4a[x].counter is assigned to m4a[x].counter + 1.

    Does this mean that m4a[0].counter is essentially 0, but when x turns 1 the m4a[1].counter is also 1?

    Then the variable count is assigned to "count + 1" and subsequently to "count + m4a[x].maybeNew(x)". Then the variable x is assigned to "x + 1".

    Then a system.out.println is stated with the variable "count" and "m4a[1].counter".

    Then a method is created returning an integer named maybeNew with the parameters integer index.
    Then an if statement (index < 5). Then Mix4 m4 is assigned to new Mix4(). I don't understand that? What is Mix4 m4? It's not an array is it? How can it be assigned to a new Mix4 without parameters? Then m4.counter is assigned to m4.counter + 1. Then the method should return 1 except when equal or above 5 then return 0.

    To what is the variable "index" referring?

    Thanks in advance!

    Gr. Mackenzie
    Last edited by mackenzie1235; 01-09-2014 at 01:31 PM.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Head First Java

    No, counter is a field in the Mix4 class, which is what the elements of the array are.

    As to what is index referring to, it is referring to what you pass that method (or, more correctly, the value that you pass) when you call it, i.e. x in this example.

  3. #3
    mackenzie1235 is offline Member
    Join Date
    Dec 2013
    Posts
    12
    Rep Power
    0

    Default Re: Head First Java

    But I still don't understand what m4a[x].counter. What's the function of that?

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Head First Java

    m4a is array.
    x is an integer and, in that construct, references the Mix4 instance stored as an array element at that index in the array (zero based).
    and counter is the field in that instance of Mix4.

  5. #5
    mackenzie1235 is offline Member
    Join Date
    Dec 2013
    Posts
    12
    Rep Power
    0

    Default Re: Head First Java

    Sorry, but what exactly is a field?

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Head First Java

    That "int counter" declaration in the class definition outside of any method.

Similar Threads

  1. Noob can't get his head around the basics of java
    By ripperuk in forum New To Java
    Replies: 20
    Last Post: 07-13-2013, 08:15 PM
  2. Head First Java book-this() question
    By silverglade in forum New To Java
    Replies: 7
    Last Post: 05-07-2011, 12:46 AM
  3. Head First Java Book question
    By silverglade in forum New To Java
    Replies: 4
    Last Post: 05-02-2011, 09:23 PM
  4. Java Project Head, Kochi
    By enroutehuma in forum Jobs Offered
    Replies: 1
    Last Post: 11-09-2009, 06:44 PM
  5. Monitor Disc Head Using Java
    By zorroforce in forum New To Java
    Replies: 4
    Last Post: 09-18-2009, 07:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •