Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By awinston

Thread: need help to create this code

  1. #1
    Vineet. is offline Member
    Join Date
    Jul 2012
    Posts
    1
    Rep Power
    0

    Default need help to create this code

    I need some help making some star patterns. The exact instructions are as follows:

    Create separate programs to produce the following patterns:
    need help to create this code-java-patterns.jpg


    I have to use for loops to get the patterns and I've already got (a.) but I can't seem to get the second. My first pattern's code is this:

    a.)

    int i,j;

    for(i=4 ; i >= 1 ; i--){
    for(j=1 ; j<=i ; j++)
    System.out.print("*");
    System.out.println();
    Last edited by Vineet.; 07-12-2012 at 06:07 PM.

  2. #2
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: need help to create this code

    If you can do it anyway you want, I would suggest using a for-loop.

  3. #3
    FussyCashew is offline Member
    Join Date
    Jun 2012
    Location
    Ohio
    Posts
    10
    Rep Power
    0

    Default Re: need help to create this code

    Quote Originally Posted by Vineet. View Post
    I need some help making some star patterns. The exact instructions are as follows:

    Create separate programs to produce the following patterns:


    a.) ****
    ***
    **
    *
    It is actually quite easy, all you'd need is a loop that prints the string "*" x number of times. For example,

    Java Code:
      // spoon fed solution deleted
    Last edited by Fubarable; 07-11-2012 at 06:23 PM. Reason: spoon fed solution deleted

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

    Default Re: need help to create this code

    Quote Originally Posted by FussyCashew View Post
    It is actually quite easy, all you'd need is a loop that prints the string "*" x number of times. For example,

    Java Code:
      // spoon fed solution deleted
    Please don't spoon-feed solutions as that doesn't help the original poster learn to learn Java. Yes it's easier to answer this way, but they don't get the better understanding that occurs when they try to solve it first themselves first.

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

    Default Re: need help to create this code

    Quote Originally Posted by Vineet. View Post
    I need some help making some star patterns. The exact instructions are as follows:

    Create separate programs to produce the following patterns:


    a.) ****
    ***
    **
    *
    Next time, please at least show us what you've tried. This sort of post is nothing more than a homework dump and is not appreciated on this forum.

  6. #6
    FussyCashew is offline Member
    Join Date
    Jun 2012
    Location
    Ohio
    Posts
    10
    Rep Power
    0

    Default Re: need help to create this code

    Quote Originally Posted by Fubarable View Post
    Please don't spoon-feed solutions as that doesn't help the original poster learn to learn Java. Yes it's easier to answer this way, but they don't get the better understanding that occurs when they try to solve it first themselves first.
    Alright, thanks for letting me know. I'm new to this forum, and giving java help, sorry.

  7. #7
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: need help to create this code

    Quote Originally Posted by Fubarable View Post
    Please don't spoon-feed solutions as that doesn't help the original poster learn to learn Java. Yes it's easier to answer this way, but they don't get the better understanding that occurs when they try to solve it first themselves first.
    Not to mention the solution didn't work.
    Fubarable likes this.

  8. #8
    FussyCashew is offline Member
    Join Date
    Jun 2012
    Location
    Ohio
    Posts
    10
    Rep Power
    0

    Default Re: need help to create this code

    Quote Originally Posted by awinston View Post
    Not to mention the solution didn't work.
    Did it not? That's odd... Perhaps String.concat() is giving issues as it usually does.

  9. #9
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: need help to create this code

    Well for 1, String.concat() simply returns a new String with the argument at the end. The original String remains unchanged. In order to change the original String, you would have to do something like:

    Java Code:
    pat = pat.concat("*");
    This works as well and it's easier for me:

    Java Code:
    pat += "*";
    But the underlying problem with you solution is that it printed this:

    Java Code:
    *
    **
    ***
    ****
    which is the reverse of what OP is asking for.
    Last edited by awinston; 07-11-2012 at 07:03 PM. Reason: Grammar

  10. #10
    FussyCashew is offline Member
    Join Date
    Jun 2012
    Location
    Ohio
    Posts
    10
    Rep Power
    0

    Default Re: need help to create this code

    Quote Originally Posted by awinston View Post
    Well for 1, String.concat() simply returns a new String with the argument at the end. The original String remains unchanged. In order to change the original String, you would have to do something like:

    Java Code:
    pat = pat.concat("*");
    This works as well and it's easier for me:

    Java Code:
    pat += "*";
    But the underlying problem with you solution is that it printed this:

    Java Code:
    *
    **
    ***
    ****
    which is the reverse of what OP is asking for.
    Oh silly me, drop concat of course, I usually use += myself. Yeah, I just realized that it was backwards, but without spoonfeeding (I hope) wouldn't you just use -= on a string with * added x number of times?

  11. #11
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: need help to create this code

    Quote Originally Posted by FussyCashew View Post
    Oh silly me, drop concat of course, I usually use += myself. Yeah, I just realized that it was backwards, but without spoonfeeding (I hope) wouldn't you just use -= on a string with * added x number of times?
    Yeah, that sounds right!

Similar Threads

  1. code to create a folder in java
    By radhika123 in forum New To Java
    Replies: 7
    Last Post: 07-21-2011, 12:21 PM
  2. Create interface from my code
    By Lyricid in forum AWT / Swing
    Replies: 1
    Last Post: 11-18-2009, 06:39 PM
  3. Trying to create a code for queue, complex stuff...
    By Mikey_S in forum Threads and Synchronization
    Replies: 3
    Last Post: 09-28-2009, 10:13 PM
  4. How to create directory through Java Code
    By Java Tip in forum java.io
    Replies: 1
    Last Post: 04-14-2009, 04:34 PM
  5. Creating a java gui to create xml code
    By Jman in forum New To Java
    Replies: 3
    Last Post: 04-27-2008, 07:56 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
  •