The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
January 2002

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Seeing Stars

Posted by Matt Gerrans on January 25, 2002 at 10:12 PM

(Doh! Another correction, this time of English: "diamonds," not "triagles" -- could this be why I don't do so well at poker?)

> That's a much more elegant solution Matt, but it'll only handle 5 and 7 stars. I have a feeling that Imran needs to be able to accept any input for the number of stars. Even if he doesn't, and your solution will work for him, how would you have the application display x number of stars, where x is an odd number? I'm sure there's a more elegant and still quite simple solution than mine.
> Hiran

Hiran, Hiran, Hiran. I was only kidding! That's why there was the ;-) at the end (maybe I was supposed to use :-p, or something?). Of course most any sensible instructor would have a hearty chuckle before scibbling a big D- on that little homework assignment.

I actually wrote this method some time ago when I was first learning Java and saw this same homework assignment posted here (I didn't post it for ethical reasons, but I'll shuck my ethical standards for the moment):


public static String getDiamond( char c, int height )
{
StringBuffer diamond = new StringBuffer();
if( height > 0 )
{
// Of course, height must be odd, but if an even number is
// passed, we'll treat it as the odd number just above.
int x = height/2;
for( int i = -x; i <= x; i++ )
{
for( int s = 0; s < Math.abs(i); s++ )
diamond.append( " " );
for( int a = 0; a < (2*x+1) - 2*Math.abs(i); a++ )
diamond.append( c );
if( i < x )
diamond.append( "\n" );
}
}

return diamond.toString();
}

I had mentioned earlier to Chin the idea of doing variations of this assigment (for fun, learning and practice at the time) that yield different grades (for instance the earlier tongue-in-cheek post would be F or D- range and the one above would be B+ or so). I did a couple (as above) and was thinking of using the Java 3D API for the A+ version (a 3D configuration of sparkling, translucent, spinning 3D stars with pulsating colors!), but haven't gotten to that, yet...

By the way, even this simple method (above) can yield some cool patterns if you run it with big numbers (2 or 3 times the width of your display).

Another interesting point, related to another recent thread where Strings were being used and I suggested StringBuffers instead: try refactoring the above method to use String instead of StringBuffer and you will see a very dramatic (and quite percepible) drop in performance. Well, you may not notice any difference for small diamonds like 5 and 7, but try 300 or so and you'll see what I mean.



Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us