Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Unable to get the idea

  1. #1
    gozuhair is offline Member
    Join Date
    Jul 2011
    Posts
    39
    Rep Power
    0

    Default Unable to get the idea

    Hi All

    I am new in Java and reading one of the java book and stuck on one program, the program is below

    Java Code:
    Forty students were asked to rate the quality of the food in the student cafeteria on a
    scale of 1 to 10 (where 1 means awful and 10 means excellent). Place the 40 responses
    in an integer array, and summarize the results of the poll.
    Java Code:
     // Fig. 7.8: StudentPoll.java
    2       // Poll analysis program.
    3
             public class StudentPoll
    5       {
    6        public static void main( String args[] )
    7        {
    8        // array of survey responses
    9        int responses[] = { 1, 2, 6, 4, 8, 5, 9, 7, 8, 10, 1, 6, 3, 8, 6,
    10     10, 3, 8, 2, 7, 6, 5, 7, 6, 8, 6, 7, 5, 6, 6, 5, 6, 7, 5, 6,
    11      4, 8, 6, 8, 10 };
    12      int frequency[] = new int[ 11 ]; // array of frequency counters
    13
    14      // for each answer, select responses element and use that value
    15     // as frequency index to determine element to increment
    16     for ( int answer = 0; answer < responses.length; answer++ )
    17     ++frequency[ responses[ answer ] ];
    18
    19     System.out.printf( "%s%10s", "Rating", "Frequency" );
    20
    21     // output each array element's value
    22     for ( int rating = 1; rating < frequency.length; rating++ )
    23    System.out.printf( "%d%10d", rating, frequency[ rating ] );
    24    } // end main
    25     } // end class StudentPoll
    Can somebody explain me the line no 17. Thank you in advance

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: Unable to get the idea

    for ( int answer = 0; answer < responses.length; answer++ )
    You need to read the tutorial or your text about how for loops work. This is fairly standard in many computer languages.
    Go to this site and Find "for statement" Trail: Learning the Java Language: Table of Contents (The Java™ Tutorials)

  3. #3
    gozuhair is offline Member
    Join Date
    Jul 2011
    Posts
    39
    Rep Power
    0

    Default Re: Unable to get the idea

    Thanks for the reply but i am not talking abount loop statement, I am talking abount the below code after loop statement.

    ++frequency[ responses[ answer ] ];

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Unable to get the idea

    His issue is with this statement:
    Java Code:
    ++frequency[ responses[ answer ] ];
    My suggestion is that you split this up into several lines to better understand it.

    Java Code:
    int thisAnswer = responses[answer];
    ++frequency[thisAnswer];
    Note that

    Java Code:
    ++frequency[thisAnswer];
    is the same as


    Java Code:
    frequency[thisAnswer] = frequency[thisAnswer] + 1;
    Norm likes this.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: Unable to get the idea

    Dyslexia, or need new glasses.

  6. #6
    gozuhair is offline Member
    Join Date
    Jul 2011
    Posts
    39
    Rep Power
    0

    Default Re: Unable to get the idea

    Thanks for the reply, could you please explain it in more details, actually, I am always afraid from array topic and this time I dont want to compromise it.
    If you can provide me an example then I will really appreciate you.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Unable to get the idea

    Consider using a debugger such as one that is available from NetBeans or Eclipse (or likely whatever IDE you're using) and step through the code to see what happens to the frequency array at that line as this can tell you a lot.

  8. #8
    gozuhair is offline Member
    Join Date
    Jul 2011
    Posts
    39
    Rep Power
    0

    Default Re: Unable to get the idea

    I am using Eclipse but i dont know how debugger works, will try to see the values in debugger and then let you the exact situation. Thank you very much

Similar Threads

  1. I have no idea what I'm doing any more...
    By kaizen9001 in forum New To Java
    Replies: 1
    Last Post: 03-26-2011, 05:10 PM
  2. Idea
    By Engineer in forum New To Java
    Replies: 1
    Last Post: 09-19-2010, 07:02 PM
  3. need an Idea ...
    By Mekonom in forum AWT / Swing
    Replies: 43
    Last Post: 11-29-2009, 03:26 PM
  4. I have an idea !!
    By HosHos in forum New To Java
    Replies: 1
    Last Post: 08-12-2009, 06:38 AM
  5. i couid not get the idea:
    By sivasayanth in forum New To Java
    Replies: 2
    Last Post: 01-18-2008, 05:52 PM

Posting Permissions

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