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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 50

Thread: Help in making an output for this Graphic (newbie)

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    35
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Help in making an output for this Graphic (newbie)

    I'm newbie =)

    I've made the code for the 1st and 2nd output but, my problem is i dont know how to do the codes for the 3rd and 4th. can anyone help me please? thanks so much my Friends!!!

    1.
    *
    **
    ***
    ****
    *****
     
    import java.io.*;
     
    public class Asterisk1
     
    {
          public static void main(String jpg[])
       {
            int n=5;
       for(int x=1; x<=n; x++)
          {
         for(int y=1; y<=x; y++)
       {
           System.out.print("*");
       }
           System.out.println();
          }
       }
    }
     
     
     
    2.
    *****
    ****
    ***
    **
    *
     
    import java.io.*;
     
    public class Ast1
     
    {
          public static void main(String jpg[])
       {
            int n=5;
       for(int x=1; x<=n; x++)
          {
         for(int y=5; y>=x; y--)
       {
           System.out.print("*");
       }
           System.out.println();
          }
       }
    }
     
     
     
    the problem are these
     
    3.
    *****
     ****
      ***
       **
        *
     
     
     
    4.
        *
       **
      ***
     ****
    *****
    Last edited by javarum; June 27th, 2011 at 05:40 PM. Reason: for tagging


  2. #2
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Help in making an output for this Graphic (newbie)

    When posting code, please use the highlight tags to retain the correct formatting: [highlight=java].. your code here ..[/highlight]

    You probably also need to include your output examples inside the highlight tags, because examples 3 & 4 are identical to 2 & 1 as posted. Posts here by default remove multiple spaces, unless put inside the tags.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Help in making an output for this Graphic (newbie)

    Draw the designs you want to print, look at where the spaces are and where the *s are.
    Design code that will output the required number of spaces and the number of *s on each line.

    Your current code just outputs *s, now you need some leading spaces before the *s.

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Help in making an output for this Graphic (newbie)

    Search the forums or view the 'Similar Threads' box at the bottom of this thread.. This question has been answered 100 times before
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  5. #5
    Member
    Join Date
    Jun 2011
    Posts
    35
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help in making an output for this Graphic (newbie)

    1.
    *
    **
    ***
    ****
    *****
    import java.io.*;
    public class Asterisk1
    {
    public static void
    main(String jpg[])
    {
    int n=5;
    for(int x=1; x<=n; x+
    +)
    {
    for(int y=1; y<=x; y+
    +)
    {
    System.out.print("*")
    ;
    }
    System.out.println();
    }
    }
    }
    2.
    *****
    ****
    ***
    **
    *
    import java.io.*;
    public class Ast1
    {
    public static void
    main(String jpg[])
    {
    int n=5;
    for(int x=1; x<=n; x+
    +)
    {
    for(int y=5; y>=x;
    y--)
    {
    System.out.print("*")
    ;
    }
    System.out.println();
    }
    }
    }
    the problem are these
    3.
    *****
     ****
      ***
       **
        *
    4.
        *
       **
      ***
     ****
    *****
    im posting via mobile. if 1st and 2nd combined with the 3rd and 4th
    output it will just give you
    a diamond shape.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Help in making an output for this Graphic (newbie)

    Have you solved it now? Your post doesn't really say?

  7. #7
    Member
    Join Date
    Jun 2011
    Posts
    35
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help in making an output for this Graphic (newbie)

    hi sir norms! sadly, not yet. hai... pls.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Help in making an output for this Graphic (newbie)

    What work have you done on the problem?

  9. #9
    Member
    Join Date
    Jun 2011
    Posts
    35
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help in making an output for this Graphic (newbie)

    ah just changing values of x and y and playing on increment and decrement. but fails me. do i need to add another system.out.print(); ? so that if a statement is true or false it will print an empty space? thanks for your time sir.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Help in making an output for this Graphic (newbie)

    Have you looked at the problem of what is being printed line by line?
    What goes on the first line? Some spaces and some *s
    What goes on the second line? A different number of spaces and *s

    How does the number of spaces and asterisks change from one line to the next?
    Is there a relationship between the line number and the number of spaces and the number of *s printed on that line?

  11. The Following User Says Thank You to Norm For This Useful Post:

    javarum (June 27th, 2011)

  12. #11
    Member
    Join Date
    Jun 2011
    Posts
    35
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help in making an output for this Graphic (newbie)

    if you look at the 1st and 2nd output, mirrored view. thats what i need to do. i know you are trying to push me to make it for myself coz its too easy and i realy wanted to. but, the problem is its too hard for me. :-( im in my 2nd day of learning java.

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Help in making an output for this Graphic (newbie)

    Part of your problem could be using the wrong variable names and no comments.
    Here is the second assignment with comments and new names:
     
          int nbrRows=5;
          // Print on nbrRows
          for(int row=1; row<=nbrRows; row++)        {
            // Print some stuff on this row
            // Start with *s 
            // Fill the first row and then one less for each row after
            for(int column=nbrRows; column >= row; column--)    {
              System.out.print("*");  // Print * in columns  on this row
            }  // end for(column)
     
            System.out.println();  // No more on this row, move to next row
          } // end for(row)

    Go back to post#10 and answer the questions I asked there.

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

    javarum (June 27th, 2011)

  15. #13
    Member
    Join Date
    Jun 2011
    Posts
    35
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help in making an output for this Graphic (newbie)

    wow! you just make that so fast?! great! sir norms. my instructor might want the script to be as almost the same as what ive posted above... hmmm. i cant explain exactly since im not that good in english. hihi

  16. #14
    Member
    Join Date
    Jun 2011
    Posts
    35
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help in making an output for this Graphic (newbie)

    Quote Originally Posted by Norm View Post
    Have you looked at the problem of what is being printed line by line?
    What goes on the first line? Some spaces and some *s
    What goes on the second line? A different number of spaces and *s

    How does the number of spaces and asterisks change from one line to the next?
    Is there a relationship between the line number and the number of spaces and the number of *s printed on that line?
    Number of spaces and *'s change every new line. and i dont know what is the exactly statement or new line command for printing an empty space. i don't know how to make it print 4 spaces and then 1 * and in second line print 3 spaces and then 2 *'s.

  17. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Help in making an output for this Graphic (newbie)

    System.out.print(" "); // prints a single/empty space
    how to make it print 4 spaces
    Look at the code you posted already. It prints a different number of *s on each line. Hint: it takes a loop

  18. #16
    Member
    Join Date
    Jun 2011
    Posts
    35
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help in making an output for this Graphic (newbie)

    Quote Originally Posted by Norm View Post
    System.out.print(" "); // prints a single/empty space

    Look at the code you posted already. It prints a different number of *s on each line. Hint: it takes a loop

    Yes ir. i tried to add this already but nothing happens. ehehe! i dont know where to put it exactly. =(

  19. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Help in making an output for this Graphic (newbie)

    Do the project a small bit at a time.
    Write down on paper the steps to print out the first line. See post #10.
    What do you need to print for the first line? Some spaces and some *s.
    Two parts:
    One print x spaces: what is the code for that?
    Two print y *s: what is the code for that?

    How do you print a space? I showed you in post#15
    Put that in a loop that executes x times to get x spaces.
    Do the same for the *s.
    Last edited by Norm; June 27th, 2011 at 02:52 PM.

  20. #18
    Member
    Join Date
    Jun 2011
    Posts
    35
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help in making an output for this Graphic (newbie)

    Quote Originally Posted by Norm View Post
    Do the project a small bit at a time.
    Write down on paper the steps to print out the first line. See post #10.
    What do you need to print for the first line? Some spaces and some *s.
    Two parts:
    One print x spaces: what is the code for that?
    Two print y *s: what is the code for that?

    How do you print a space? I showed you in post#15
    Put that in a loop that executes x times to get x spaces.
    Do the same for the *s.
    hmmm.. like for the statement of the x is false it will print space?
    and then if the statement of y is true it wil print *s?
    ohhh... =( need to get food im hungry.. my brain is not working.

    Sir why arent you giving me the exact answer to this? you really want me to solve it? ehehe! woooshh... hard. hehe. thanks sir. (getting food)

  21. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Help in making an output for this Graphic (newbie)

    No. x and y are numbers. like 4 and 1 then 3 and 2 etc
    you really want me to solve it?
    Answer me this question: Do you want to learn programming?
    If so, you'll need to learn to think like a computer operates.

  22. #20
    Member
    Join Date
    Jun 2011
    Posts
    35
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help in making an output for this Graphic (newbie)

    Quote Originally Posted by Norm View Post
    No. x and y are numbers. like 4 and 1 then 3 and 2 etc

    Answer me this question: Do you want to learn programming?
    If so, you'll need to learn to think like a computer operates.
    i really do!

  23. #21
    Member
    Join Date
    Jun 2011
    Posts
    35
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help in making an output for this Graphic (newbie)

    another hint pls hehe

  24. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Help in making an output for this Graphic (newbie)

    First do the steps I asked you to do in post#17
    What do you need to print for the first line? Some spaces and some *s.
    Two parts:
    One print x spaces: what is the code for that?
    Two print y *s: what is the code for that?

    How do you print a space? I showed you in post#15
    Put that in a loop that executes x times to get x spaces.
    Do the same for the *s.

  25. #23
    Member
    Join Date
    Jun 2011
    Posts
    35
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help in making an output for this Graphic (newbie)

    as what you thaught me, this is how to print a space > System.out.print(" ");

    what do you mean bout this Sir?
    One print x spaces: what is the code for that? <<< for(int x=1; x<=n; x++) System.out.print(" "); ???
    Two print y *s: what is the code for that?

  26. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Help in making an output for this Graphic (newbie)

    What happens when you compile and execute your code?
    print y *s: what is the code for that?
    y is an integer, like 4. If you print 4 *s the printout would be: ****
    You already have the code for that in your first two programs.

  27. #25
    Member
    Join Date
    Jun 2011
    Posts
    35
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help in making an output for this Graphic (newbie)

    another hint pls. ^_^

    its hard to print spaces before the *s.

Page 1 of 2 12 LastLast

Similar Threads

  1. Java graphic coordinates out of bounds?
    By jnmcneel in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 24th, 2011, 03:28 PM
  2. how to adjust the size on a graphic component
    By Khoatic in forum AWT / Java Swing
    Replies: 8
    Last Post: November 19th, 2010, 10:28 PM
  3. Graphic angle
    By CoffeeBeans in forum Java Theory & Questions
    Replies: 1
    Last Post: August 22nd, 2010, 11:28 AM
  4. Graphic Environment Abstract Methods
    By striko_514 in forum Java Theory & Questions
    Replies: 2
    Last Post: July 5th, 2010, 01:01 AM
  5. Output problem (newbie)
    By Asido in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 8th, 2010, 12:19 PM