Results 1 to 8 of 8
  1. #1
    aamster is offline Member
    Join Date
    Oct 2009
    Posts
    8
    Rep Power
    0

    Default intricate star pattern

    hi, I need to make a pattern that looks like the one attached. I can get the stars on the left side fine, but i can't seem to get the stars on the right or the pound symbols. I tried splitting up each portion of stars ( the upper left, lower left, upper right, lower right) but i just can't seem to get the right side. If you want, you can look at my code that I have attached as well.

    I would greatly appreciate anyone who attempts to help me. You must do it using a nested for loop, and if you can, no methods.

    Thanks for your help,
    Adam
    Attached Files Attached Files

  2. #2
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    I'm partially confused how you can do the left half, but not the right half, when they are mirror images. If you have code that does the left half, perform the mirror of it for the right half.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  3. #3
    aamster is offline Member
    Join Date
    Oct 2009
    Posts
    8
    Rep Power
    0

    Default

    well, codesaway, would you mind looking at my code and showing me where i went wrong??

  4. #4
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    Could you describe the pattern to me, instead?

    1) Describe the number of stars required for a row
    2) Describe the number of blanks for a row

    That's where you need to begin. By describing the pattern, you can then convert it to code.

    P.S. Why is the class file saved in a .doc, instead of .java?
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  5. #5
    aamster is offline Member
    Join Date
    Oct 2009
    Posts
    8
    Rep Power
    0

    Default

    codesaway, if you just look at the star pattern.doc file i uploaded, i think you'll have a better idea of the pattern.

  6. #6
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    I do understand the pattern, and I have already written code that outputs it. What I'm trying to do is to get you to think like a programmer. You said that you were having problems, so I started with the most important question, how many stars are on a row?

    If you can tell me, for a given row number, how many stars there are, then you are done. Since the pattern is a mirror, with the '#" in the middle, the number of stars before the "#" is the same as the number of stars after. Also, the number of blanks are directly related to the number of stars. So, in essence, the number of stars for a given row is the ONLY question that matters, because using that knowledge, you can output the pattern, as desired.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  7. #7
    aamster is offline Member
    Join Date
    Oct 2009
    Posts
    8
    Rep Power
    0

    Default

    do I need two separate for loops to create the left pyramid and right pyramid, respectively? or is it all in the same for loop? I see it goes like this
    line 1: 15 stars
    2: 7 stars # 7 stars
    3: 6 stars space # space 6 stars
    4: 5 stars space space # space space 5 stars
    5: 4 stars space space space # space space space 4 stars
    etc, etc.

    I don't know how to produce the mirror image.

  8. #8
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    How I tackled it is I broke it up into pieces. Other than the first and last row, a pattern is used, stars blanks # blanks stars.

    So, I had a variable, starsCount, which I used to store the number of stars for the row. I then had a variable, blanksCount, which stores the number of blanks for the row. These values were for the left half, since the right half is a mirror.

    Then, you loop. First you output <starsCount> number of stars, then <blanksCount> number of blanks. You add in the "#". Then, you do the same in reverse (since it's a mirror).

    That's how I tackled the problem. This method ensures self-documenting code, since the loops, and variable names tell you what the output should be. My professors always told me that if you couldn't tell the output by reading just the code, then you did something wrong already.
    Last edited by CodesAway; 10-11-2009 at 06:55 AM.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

Similar Threads

  1. Star pattern help?
    By GeeKunMow in forum New To Java
    Replies: 13
    Last Post: 07-21-2011, 05:59 AM
  2. Need Help with A star
    By 256mxr in forum New To Java
    Replies: 3
    Last Post: 10-11-2009, 02:50 PM
  3. Recursive Star Pattern HELP PLEASE!!
    By ITnewbie in forum New To Java
    Replies: 1
    Last Post: 09-25-2009, 09:55 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
  •