Is there a better way of getting the following output: ?

T

Tom Leylan

Hi Ivan:

You are getting closer. I think you've recognized a sequence but I don't
think you're seeing the pattern. It's a formula where the star length
decreases by two at each step until there is one star at which point the
star length increases by two. At the same time the blank length adjusts by
the same value in the opposite direction. Interestingly you don't have to
keep track of the number of blanks only the stars because you can calculate
blanks at any time by subtracting stars from total rows.

I originally wrote my version as two procedures in a dummy class but I've
subsequently rewrote it as a static class which can be used as follows:

Stars.ProcessX( "*", ".", 5 );
Stars.ProcessX( "X", ".", 8 );

It even self-corrects in the second example. I check to see if the number
of rows is odd and if it isn't I add one to it.

One other idea (I know you're learning this) but you want to encapsulate
whatever you can to offer reuse. You are making a lot of calls to
System.out.print() for instance. It doesn't matter how many call you make
but that they are scattered throughout the code and that make it difficult
to change if the next thing you want to do is draw graphics instead. I put
all mine in a private method named StarsOut(). It is using
System.out.print() but the mechanism for all output is there and there
alone.

Seemingly I could add other processes to my Stars class that could output
other shapes as well. You would end up having to rewrite Main().

One step at a time though :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top