Results 1 to 16 of 16
  1. #1
    coding is offline Member
    Join Date
    Jan 2011
    Posts
    40
    Rep Power
    0

    Default Can someone go into depth in explaining this code?

    I want to know why it does what it does and what each i and j means and why it print what it prints. I get the simple io and prompt but the variables and etc. I need help clarifying.
    import jpb.*;
    public class {


    public static void main(String[] args) {
    for (int i=1;i<=row ;i++) {
    for (int j=1;j<=i;j++) {

    System.out.print("*");
    }
    System.out.println("");
    }

    }

    }
    Last edited by coding; 03-08-2011 at 01:36 AM.

  2. #2
    eXistenZ is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default Is this a job application?

    I have seen something like this on a first-cut job application response. It was multiple choice: what does the output look like?

    The inner loop is a print but the outer loop is a println (print line). So it prints a line of *'s and then it makes a number of rows of those stars. So the available answers were:

    *
    **
    ***
    or
    ***
    ***
    ***
    or
    ***
    **
    *
    and so forth. You can work it out.

    Anyway, that's where I have seen something similar.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    A lot of this stuff is basics(such as what i and j are) I would love to help you out, but I have a question, are you teaching yourself, or are you taking classes? If you are taking classes, don't be afraid to ask your teacher for more help. i and j are used in this for counters. The basic for loop is like this
    Java Code:
    for(declaration; condition; step)
    first you declare a variable, then you set up a condition, and then you step somehow.

    so in the code
    Java Code:
    for(int i = 0; i < 5; i++)
    Whatever is in the code block will be executed 5 times(0, 1, 2, 3, 4) It goes until the condition is false. In this case, when i is 5, since 5 is not less than 5.

    Now answer this question for me
    Java Code:
    for(int i = 0; i < 10; i++){
      for(int j = 0; j < 3; i++)
    how many times does this loop?

    You could also do
    Java Code:
    int i;
    for(i = 0; i < 5; i++){
    But it's quicker to declare and initialize the variable(not quicker for the computer, but less code to write)
    declaring the variable before the loop can be useful at times. Say you want to use the variable again outside the loop, it works
    Java Code:
    int i;
    for(i = 0; i < 5; i++){
      System.out.println(i)
      if(i % 4 == 0){
        break;
      }
    }
    System.out.println("The loop broke at " + i);
    If you declare the variable in the loop you can't use it outside the loop. Understanding this and realizing when you need to use one or the other is important.

    Now, if you have more questions, I will gladly help out. However, with basics, if you are in class you might want to talk to your teacher.

    Also answer existenz's question.
    Last edited by sunde887; 03-08-2011 at 02:28 AM.

  4. #4
    coding is offline Member
    Join Date
    Jan 2011
    Posts
    40
    Rep Power
    0

    Default

    Oh thanks that helps a little. I'm taking a class but it is really fast paced and I have other classes to and getting help becomes a hassle but do get help if it doesn't interfere with my schedule since I do multiple things a day.

    It's not for a job application.

    Also I'm teaching myself as much as possible on my spare time so I won't be behind.

    Also I'm starting to get the I part, but what does the j part have to do with it? I is the amount it starts with right and how many rows it creates.
    In the code it ask to print out a star in quotes but how does the code now to duplicate and repeat it?

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    the j is a separate loop.

    I need you to answer the following question
    Java Code:
    for(int i = 0;i < 10; i++){
      for(int j = 0; j < 3; j++)
    How many times does this loop?

    The reason why you have a j is because i can't be used again since it's already been declared and initialized. Regardless if it's for an app, he gave you some choices, decide which one the output should be...without compiling and running first.

  6. #6
    coding is offline Member
    Join Date
    Jan 2011
    Posts
    40
    Rep Power
    0

    Default

    It would loop 10 times right. Or up to 10. So 9?

  7. #7
    coding is offline Member
    Join Date
    Jan 2011
    Posts
    40
    Rep Power
    0

    Default

    I'll be going to sleep and checking this thread in the morning. The reason I do this is because I was sick majority of the semester and now getting better and don't want to lose anymore time.

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    no, 10 times would be
    Java Code:
    for(int i = 0; i < 10; i++)
    0,1,2,3,4,5,6,7,8,9

    but if you have a loop inside of a loop it does for each i it produces the j loop
    Java Code:
    for(int i = 0; i < 10; i++){
      for(int j = 0; j < 3; j++)
    so for each i(0,1,2,3,4,5,6,7,8,9) it would do the entire j loop(0,1,2). Does this clear up how many times it loops?

    If necessary compile and run the following code
    Java Code:
    public class Printing{
      public static void main(String[] args){
        for(int i = 0; i < 10; i++){
          for(int j = 0; j < 3; j++){
            System.out.println(j + " ");
          }
        }
      }
    }

  9. #9
    coding is offline Member
    Join Date
    Jan 2011
    Posts
    40
    Rep Power
    0

    Default

    Yah that clears it up.

  10. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You should definitely see about getting more help from your teacher, however; I am more than willing to assist you(I will not give you answers though) with whatever you need.

  11. #11
    coding is offline Member
    Join Date
    Jan 2011
    Posts
    40
    Rep Power
    0

    Default

    Thanks, so far no one has gave me flat out answers. They just tell me what I did wrong and guide me in the right direction. It has really helped.

    I'm going to sleep and going to rest my head.

    But this has me stumped.

    How should I tackle this?

    Making an input of this

    ***
    *****
    ****
    ***

  12. #12
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    This isn't too much of a challenge, but it requires you to think like a programmer(which you may not be able to do yet) You need somehow to loop up, and then down, like this
    Java Code:
    loop
      loop
        print star
    loop
      loop
        print star

  13. #13
    coding is offline Member
    Join Date
    Jan 2011
    Posts
    40
    Rep Power
    0

    Default

    K, how long have you been using java?

  14. #14
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I haven't been using Java that long to be perfectly honest. I started learning by reading How to design programs(which is a great book, and available for free online at htdp.org) 3 times about a year ago. As far as java I started in the beginning of January, however; I am very motivated and spend a lot of time reading and helping people on these forums so I have seen a lot of similar problems.

    I also practiced at a site which got me a lot of practice with problems like you are doing.

    CodingBat Java

  15. #15
    coding is offline Member
    Join Date
    Jan 2011
    Posts
    40
    Rep Power
    0

    Default

    Wow, that's amazing. You are a fast learning and seem so professional and skilled. You could have fooled me with me thinking you had years of experience.

  16. #16
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Haha. I only really know the basics, threading, complex generics, regular expressions and a lot of other stuff I still only have a basic understanding of. If you are done in this thread please mark it solved with the thread tools. If you have more questions feel free to ask

Similar Threads

  1. Bit depth of a boolean
    By subith86 in forum New To Java
    Replies: 1
    Last Post: 01-23-2011, 08:39 AM
  2. Explaining Exceptions
    By sunde887 in forum New To Java
    Replies: 5
    Last Post: 01-22-2011, 08:00 AM
  3. Help explaining the difference.
    By Romally in forum New To Java
    Replies: 1
    Last Post: 11-14-2010, 02:40 PM
  4. the depth of SWT controls
    By Alarmmy in forum SWT / JFace
    Replies: 0
    Last Post: 07-13-2009, 09:51 AM
  5. Replies: 4
    Last Post: 01-01-2009, 12:03 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
  •