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

Thread: asterisk forming diamond

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default asterisk forming diamond

    public class Diamond {
                  public static void main(String args[]){
     
                      int n,i,j,k;
               do { 
     
                n = (int)(3);
     
                }while(n % 2 == 0);
     
     
               for (i = 1; i <= n; i++){
     
                       for (k = n; k > i; k--)
     
                         System.out.print(" ");
     
     
     
                       for (j =1; j <= i; j++)
     
                   System.out.print("*" + " ");
     
                 System.out.println();
     
                       }         
     
               for (i = n; i > 0; i--){
     
                      for (k = n; k > i; k--)
     
                         System.out.print(" ");
     
     
     
                       for (j =1; j <= i; j++)
     
                   System.out.print("*" + " ");
     
                 System.out.println();
     
                       } 
          }
     
          }
    ..there should be only one line for e asterisk..


  2. #2
    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: asterisk forming diamond

    Take a look at the similar threads listed at the bottom.

    help with loop to print an equliateral asterisk triangle

    Does this help? This question has been answered quite a few times recently.

    P.S - Please dont post the same thread twice.
    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

  3. #3
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    491
    Thanks
    8
    Thanked 47 Times in 45 Posts

    Default Re: asterisk forming diamond

    What's "Object Oriented Programming" about a class that does everything in its main(...) method?

    db

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,669
    Thanks
    25
    Thanked 327 Times in 304 Posts

    Default Re: asterisk forming diamond

    Quote Originally Posted by Darryl.Burke View Post
    What's "Object Oriented Programming" about a class that does everything in its main(...) method?

    db
    Thats how students are taught when they first learn JAVA. Usually when they are learning the basic syntax and the teacher/professor doesn't want to go indepth with the whole Object Oriented side of it.

    "The system" is messed up for sure. I disagree with how JAVA is taught, getting students to create classes that are exact copies of JAVA Provided classes (like we see a lot on this forum) defeats the purpose of JAVA. As does handcuffing the students when they are trying more complex algorithms. The stupidest thing of all is that my college won't let me take the beginners JAVA class until I finish C. I've asked about testing out, but they said if I tested out I don't get the credit for the course...
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. #5
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: asterisk forming diamond

    ..the output should look like this
    *
    * *
    * * *
    * *
    *
    ..but then the output of this program is

    *
    * *
    * * *
    * * *
    * *
    *
    ..sorry this should be in loops..thank you..pleas help me..

  6. #6
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: asterisk forming diamond

    ..in diamond formation..

  7. #7
    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: asterisk forming diamond

    Quote Originally Posted by aussiemcgr View Post
    Thats how students are taught when they first learn JAVA. Usually when they are learning the basic syntax and the teacher/professor doesn't want to go indepth with the whole Object Oriented side of it.

    "The system" is messed up for sure. I disagree with how JAVA is taught, getting students to create classes that are exact copies of JAVA Provided classes (like we see a lot on this forum) defeats the purpose of JAVA. As does handcuffing the students when they are trying more complex algorithms. The stupidest thing of all is that my college won't let me take the beginners JAVA class until I finish C. I've asked about testing out, but they said if I tested out I don't get the credit for the course...
    I think a student needs to understand the basics before getting into the Object Oriented philosophy. For simple programs, it's much easier for new students to learn imperative programming before starting to worry about functional or Object Oriented programming. Granted, the amount of time being spent on imperative programming may be a bit excessive depending on who your professor is.

    I disagree that it defeats the purpose of Java to have students write classes which are already implemented in Java, I think a computer scientist should understand how the tools they are using work. While implementing your own sorting algorithm every single time may not be practical, knowing these basic algorithms will help you develop larger algorithms and systems which perform much better than a simple "intuitive" solution.

    Anyways, back on topic

    The reason you're diamond is drawing three stars in the middle is because you're starting to draw stars before you decrement to 2 (to take into account that 3 stars have already been drawn by the first loop).

    Try running your code as if you were the computer (get a piece of scratch paper to keep track of the output and variable values).

Similar Threads

  1. help with loop to print an equliateral asterisk triangle
    By everyone0 in forum Loops & Control Statements
    Replies: 13
    Last Post: October 11th, 2012, 12:37 PM
  2. Is it possible to print a spade, club, heart, or diamond in java?
    By BC2210 in forum Java Theory & Questions
    Replies: 1
    Last Post: October 2nd, 2009, 08:35 PM

Tags for this Thread