Jump to content

Dammed Diamond


Ste

Recommended Posts

/*

Steven Borchard

Diamond

*/

 

public class TrivialApplication {

 

public static void main(String args[]) {

 

 

 

Edited by Ste

Share this post


Link to post
Share on other sites

I remember doing that project for one of my classes in college.  yet I have no idea how I did it anymore.

461814[/snapback]

 

I had to do something about this in my first year uni programming unit EXAM :)

Share this post


Link to post
Share on other sites

public class Diamond {
      public static void main(String[] args) {
             int size = 8;
             int evenstart = 3;

             // top half
             for (int i = size; i > 0; i -= 2) {
                 // spaces left side
                 for (int j = 0; j < Math.floor(i / 2); j++) {
                     System.out.print("-");
                 }

                 // middle stars
                 for (int j = i; j <= size; j++) {
                     System.out.print("*");
                 }

                 System.out.println();
             }

             // bottom half
             // fix up even numbers
             if (size % 2 == 0) evenstart = 0;
             for (int i = evenstart; i <= size; i += 2) {
                 // spaces left side
                 for (int j = 0; j < Math.floor(i / 2); j++) {
                     System.out.print("-");
                 }

                 // middle stars
                 for (int j = i; j <= size; j++) {
                     System.out.print("*");
                 }

                 System.out.println();
             }
      }
}

 

Bit hacky but works.

 

EDIT: eek, you could also do it the way you were doing it like so...

 

public class Diamond {
      public static void main(String[] args) {
          int size = 9;

          String A="*";
          for(int x=0;x<size;x++){
              for (int i = x; i < size; i++) {
                  System.out.print(" ");
              }

              System.out.println(A);
              A += "**";
          }

          A="*";
          for(int x = 0; x < (size + 1); x++) {
              A += "**";
          }

          for(int x=0;x<(size + 1);x++){
              for (int i = x; i > 0; i--) {
                  System.out.print(" ");
              }

              A = A.substring(2, A.length());
              System.out.println(A);
          }
      }
}

Share this post


Link to post
Share on other sites

Ok its works but the diamond is to big. its is 10 rows to big, but thats ok Ill figure it out latter, though explain to me the use of the last part of the code, I have never encountered anything relating to substring.

 

The next part of my program, is to modify that code so a user can enter an odd number 1 - 19 to specify the numbers of rows.

Edited by Ste

Share this post


Link to post
Share on other sites

Ok its works but the diamond is to big. its is 10 rows to big, but thats ok Ill figure it out latter, though explain to me the use of  the last part of the code, I have never encountered anything relating to substring.

 

The next part of my program, is to modify that code so a user can enter an odd number 1 - 19 to specify the numbers of rows.

462343[/snapback]

 

If you need user input number of rows, notice the 'size' variable? Use that. Since what I have basically done is drawn a top triangle, the middle line and then an upside down button triangle, if size = 2 then you get (2 * 2) + 1 rows. This would mean if the user said they wanted, say, 5 rows you would have to set size = (5 /2) .. which actually equals 1.5, but it'll round down to 1 as it is an int, and as you can see (2*2) + 1 = 5 rows :)

 

substring, gets a portion of the string. You can specift where you want to start and where you want to end. It's basically chopping off the first two stars each time, although you could have very easily chopped off the last two stars by starting from the beginning of the string substring(0, xxx) and returning the portion of the string up until the length() - 2, so substring(0, str.length() - 2);

 

Did any of that make any sense to you, at all? :)

Share this post


Link to post
Share on other sites

I love it when teachers decide you haven't been taught it so you are not allowed to do it that way. Such bullcrap. Thankfully my Uni doesn't care for silly little thing like that, in fact I think they probably like the fact that you have gone away, read some docs, and figured out how to use a new function/whatever!

 

Well anyway, if you aint learnt it yet you will have to go with my first solution... or wait for someone else to come up with a better way than that (no doubt there is one, but hell if I can figure out any other way)

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...