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

Thread: Program with for loops help

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Program with for loops help

    Hey guys, I'm new to Java, and we're learning the For Loop in class, I don't understand it at all, and I need to create this object using for loops and nested for loops:
    ___*___
    __***__
    _*****_
    *******

    The object has 3 _'s then an asterix, then another 3. The next line has 2 _'s and 3 asterixs, and then 2 _'s, and so on till the last line which is 7 asterixs.

    If I could have help with writing the code, it would be greatly appreciated, .


  2. #2
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Program with for loops help

    [CODE]wahh i have school today if i had a long time for this right now i ill try to do this...

    anyway try to study this first ..

    same problem but slightly easy rather than that (helloworld922 gave this to me)

    public class LoopSample {
     
        public static void main(String[] args) {
     
            int firstLoop,
                secondLoop;
     
            for (firstLoop = 3; firstLoop > 0; firstLoop--) {
     
                for (secondLoop = firstLoop; secondLoop > 0; secondLoop--){
     
                    System.out.print(">");
                }
     
                System.out.println("");
            }
        }
    }

    you can see this

    >>>
    >>
    >

    the firstloop will generate the count.(together with the next lines). the second loop will generate the character's display according to the count...

    try to study its algorithm you'll find it out...
    Last edited by chronoz13; October 4th, 2009 at 08:55 PM.

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Program with for loops help

    The code's going to look somewhat different than the one chronoz gave, but it's the same general idea. It all comes down to what's the pattern? Once you can figure out a simple algorithm, it's not too hard to code.

    It looks like with each new line the number of asterisks increase by 2 from the previous line, and the number of _'s decrease by 2, and stops when the line is all asterisks. I'm also assuming that the line width is always going to be odd. So, a simple little algorithm would be this:

    1. Input how wide of a box you want = width (must be odd number)
    2. Have numberAsterisks = 1
    3. While numberAsterisks < width:
    {
         4. numberUnderscores = width - numberAsterisks
         5. Draw out numberUnderscores/2 underscores
         6. Draw out numberAsterisks asterisks
         7. Increase numberAsterisks by 2
    }

    That's the whole algorithm, now it's your turn to turn this into code

  4. #4
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Program with for loops help

    wait, I can see some spaces. is it also in the part of the loop?
    Last edited by chronoz13; October 5th, 2009 at 05:07 AM.

  5. #5
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Program with for loops help

    oh sorry.. there's no spaces.. i was confused if asterisk ,, its a charcter that will appear on a 'top'
    i thought is was spaces.. sory,,

  6. #6
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Program with for loops help

    im already here please help!

       for (int x = 1; x <= 7; x = x + 2) {
     
                for (int j = x; j > 0; j--) {
     
                    System.out.print("*");
                }
     
                System.out.println("");
            }
        }
    }

    output

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

    im stuck with askterisks.. i dont know where should i start
    Last edited by chronoz13; October 5th, 2009 at 06:16 AM.

  7. #7
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Program with for loops help

    oh my God ! im here... cant believe it ..

     
             for (int x = 1; x <= 7; x = x + 2) {
     
                for (int p = x; p <= 5; p++) {
     
                    System.out.print("_");
                }
                for (int j = x; j > 0; j--) {
     
                    System.out.print("*");
                }
     
                for (int k = x; k <= 5; k++) {
     
                    System.out.print("_");
                }
                System.out.println("");
            }

    output:

    _____*_____
    ___***___
    _*****_
    *******
    Last edited by chronoz13; October 5th, 2009 at 07:11 AM.

  8. #8
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Program with for loops help

    DONE DONE DONE!! i MADE it... but i wont deletete my first two post i want someone to check it... for me to know my first mistakes... (please dont think i'm spamming...)

    yehey!! idid it!! what an ease!!

    here's the final CODE!!

            for (int x = 1; x <= 7; x = x + 2) {
     
                for (int p = x; p <= 5; p = p + 2) {
     
                    System.out.print("_");
                }
     
                for (int j = x; j > 0; j--) {
     
                    System.out.print("*");
                }
     
                for (int k = x; k <= 5; k = k + 2) {
     
                    System.out.print("_");
                }
                System.out.println("");
            }
     
        }

    OUTPUT:

    ___*___
    __***__
    _*****_
    *******

  9. #9
    Junior Member
    Join Date
    Oct 2009
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Program with for loops help

    woo hoo thanks guys, i still have no idea what the hell i'm doing, but at least i got the answer to my challenge problem!

  10. #10
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Program with for loops help

    That's not the purpose of us helping you. We're here to help you learn, not just blindly give you the answer.

    I must say, though, nice work chronoz13.

  11. #11
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Program with for loops help

    oh sori helloworld.. i understand what you mean.. its just that... i also want to figure it out..
    anyway ixjaybeexee, I started also like that.. i want other people to make the program for me.(coz i dont know where to start).. but that's not where it ends.. i have to study it. and understand how it works.... so if i ever encounter similiar difficulties, I already have the guts and some knowledge to deal with it....
    but if you can try to figure it out..try it first by your own.. but if theres no more way out... then ask for help...

    anyway im happy that we statisfy you... hehe happy coding...!
    Last edited by chronoz13; October 6th, 2009 at 07:05 AM.

  12. #12
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Program with for loops help

    ahmm helloworld. can you show the code for this.. i want to study its algorithm and to compare its flow with the one that i made

    1. Input how wide of a box you want = width (must be odd number)
    2. Have numberAsterisks = 1
    3. While numberAsterisks < width:
    {
         4. numberUnderscores = width - numberAsterisks
         5. Draw out numberUnderscores/2 underscores
         6. Draw out numberAsterisks asterisks
         7. Increase numberAsterisks by 2
    }

  13. #13
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Program with for loops help

    Haha, it's ok chronoz. You're turning out to be quite the programmer

    Haha, I actually just found a slight problem in the algorithm i posted. I forgot to draw the end underscores Here's the revised algorithm:

    1. Input how wide of a box you want = width (must be odd number)
    2. Have numberAsterisks = 1
    3. While numberAsterisks < width:
    {
         4. numberUnderscores = width - numberAsterisks
         5. Draw out numberUnderscores/2 underscores
         6. Draw out numberAsterisks asterisks
         7. Draw out numberUnderscores/2 underscores
         8. Increase numberAsterisks by 2
         9. Draw out new line
    }

    I haven't actually checked if this code is correct, but here it is:

    public static void drawPattern(int width)
    {
         if (width % 2 == 0)
         {
              System.out.println("width must be odd!");
              return;
         }
         int numberAsterisks = 1;
         while (numberAsterisks < width)
         {
              int numberUnderscores = width - numberAsterisks;
              for (int i = 0; i < numberUnderscores/2; i++)
              {
                   System.out.print("_");
              }
              for (int i = 0; i < numberAsterisks; i++)
              {
                   System.out.print("*");
              }
              for(int i = 0; i < numberUnderscores/2; i++)
              {
                   System.out.print("_");
              }
              numberAsterisks += 2;
              System.out.println("\n");
         }
    }

  14. The Following User Says Thank You to helloworld922 For This Useful Post:

    chronoz13 (October 6th, 2009)

  15. #14
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Program with for loops help

    so thats it wahahahah I was having a very hard time "what will I do in numbers 5.) and 6.)"

    "huh".. Im really extracting the step-by-step logic that you posted from 1 - 7 whahahah so thats it

    5.) and 6.) whahahaha

    anyway tnx for this and that helloworld...wahahahha

  16. #15
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Program with for loops help

    Whoa that's a bit harder... pre-defined method haha., im not yet running that program but i notice that ...
    it has a little bit logical similarities with mine...

    the main loop will generate the next lines .("\n") and the rest of the loops that is NESTED with the main loop will generate the corresponding characters...(JSon taught and explained me this...shhhh)

    ahmm helloworld.. is it possible to make this program in a fully nested loop.. as in loop inside a loop inside a loop inside a loop and so on.. until it reaches the main loop. My eyes is starting to get white thinking of that

    waheheheh ...

  17. #16
    Junior Member
    Join Date
    Oct 2009
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Program with for loops help

    I'm only on my second chapter in Computer Science class, so I really don't know a whole lot

  18. #17
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Program with for loops help

    Hehe, yeah, i don't know why i decided to put it inside a method, but I was just going through the algorithm for any general width.

    I'm kind of confused by what you mean by fully nested loop. I have 3 for loops in series nested inside of a while loop. Did you mean like i did with the other question you had drawing "<"?

  19. #18
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Program with for loops help

    exactly ... as in loop inside a loop inside a loop that will generate the same out put...


    or just a single loop.... hahaa... i cant think of it... ei wait regardig with your method im not yet testing it... in the condition for the remainder what value it should return?
    Last edited by chronoz13; October 6th, 2009 at 08:35 PM.

  20. #19
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Program with for loops help

    I don't think this is possible in just 1 for loop. It's really not useful to have a loop inside a loop inside a loop here... remember, each inner loop contributes to the overall run-time. My code as-is is O(n^2), where as having what you're proposing would be O(n^3).

    My method doesn't return any value because I declared it void. However, you can still use the return statement even if the method returns nothing (note: only works for the void return type)

  21. #20
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Program with for loops help

    ahh so its ok if i remove the return statement... and helloworld regarding with the big 'O' notation ... i dont have any idea about ...

    and regarding with the drawPattern that can generate by a single loop...
    i would like to share my code..

    this was done by my friend...

     public static void main(String[] args) {
     
            int c = 5;
     
            for (int a = 1; a <= c; a++) {
     
                System.out.print("*");
     
                if (a == 5) {
     
                    System.out.println("");
                    a = 0;
                    c--;
                }
            }

    we were coding this in 'C' syntax when we where in school a while ago and it generate this output

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

    but when i apply the logic in 'java'' syntax, i dont know why it happened like this..

    *****
    ****
    the the 3,2,1's part is not printing...

  22. #21
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Program with for loops help

    Link: Big O notation

    Basically, that's the time it takes your algorithm to run with respect to some value n (usually how many times through you have to go). Big O is very useful when n is huge, but not so useful when n is small. However, the difference in times when n is small is negligible.

    umm, that code shouldn't work in either C or Java. It's because you're always comparing to 5 in the if statement instead of C.

     public int main() {
     
            int c = 5;
     
            for (int a = 1; a <= c; a++) {
     
                printf("*");
     
                if (a == [b]c[/b]) {
     
                    printf("\n");
                    a = 0;
                    c--;
                }
            }

    I really have the feeling that somewhere in this i've messed up my C syntax, but you get the idea

    the converse code in Java would be this:

     public static void main(String[] args) {
     
            int c = 5;
     
            for (int a = 1; a <= c; a++) {
     
                System.out.print("*");
     
                if (a == [b]c[/b]) {
     
                    System.out.println("");
                    a = 0;
                    c--;
                }
            }

  23. #22
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Program with for loops help

    tnx helloworld... for correcting the code for me... anyway .. its a bit confusing for me trying to understand the logic of big 'O' ..


    ahh , i want to compare the value of 'a' into 5 (that's the logic) but my mistake is.. i compared it in a numerical value(literally)... so instead the of comparing the value of 'a' to 'c' which is decrementing,
    the value of condtion in 'if' stucks in 5, although the value of 'c' decrements..
    Last edited by chronoz13; October 8th, 2009 at 09:22 AM.

  24. #23
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Program with for loops help

    It's ok if you don't understand big-O for now. I'm sure sometime in the future you will take a CS algorithm class, and they will go over all the gooey bits of big-O notation.

    I think for now, it's sufficient to say this:

    Big-O notation describes how fast a function's runtime increases. Obviously, we want runtime of an algorithm to be as short as possible, so the best Big-O performance is O(1). It's not always possible to get O(1) performance (actually, it's very rare), so we can shoot for O(n), O(log(n)), O(n*log(n)),O(n^2).... Any function of n that's limit at infinity is infinity ( O(1) is an exception here, it's limit is 1).

    Mathematically speaking, here's a general breakdown of big-O terms I have seen and there runtime performance relative to each other:

    O(1) < O(log(log(n))) < O(log(n)) < O(n) < O(n log(log(n)) < O(n log(n)) < O(n^2) < O(n^3) < O(n^4) < O(2^n) < O(Fib(n)) < O(n!) < O(2^(2^n))

    The most common ones are O(1), O(log(n)), O(n), O(n log(n)), O(n^2), and O(n^3)

    You'll also find some crazy ones, like the performance of shell sort: O(n^(5/4)), or even crazier O((log(n))^(6+ε)) for the AKS primality test.

  25. #24
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Program with for loops help



    what the.! anyway... ill keep that.. !! heheheheh

Similar Threads

  1. need help with loops plz
    By Kilowog in forum Loops & Control Statements
    Replies: 4
    Last Post: September 28th, 2009, 08:11 AM
  2. Homework - using 'IF' for 'For Loops'
    By Enzo in forum Loops & Control Statements
    Replies: 5
    Last Post: September 16th, 2009, 10:52 AM
  3. How to Use different Java loops
    By JavaPF in forum Java Programming Tutorials
    Replies: 1
    Last Post: August 28th, 2009, 03:10 AM
  4. Replies: 11
    Last Post: April 29th, 2009, 03:12 AM
  5. [SOLVED] Fixing of bug in number guessing game
    By big_c in forum Loops & Control Statements
    Replies: 6
    Last Post: April 16th, 2009, 02:15 AM