Printing rows of stars...more robust and revisited
I took the program I wrote to print rows of stars in the correct pattern
one step further. It now handles any number of rows as entered on the
command line. I'm posting the code here along with two sample runs, one
for odd and one for even as the results are different for both.
What I'm hoping for is some constructive (not destructive) criticism on
what I can do to make the program
a) more object oriented
b) more readable
c) more maintainable
I have my own ideas as to how these goals can be accomplished but I
would like to hear from some of the more experienced Java programmers so
that I can benefit from their experience.
Here is my code
public class ShowMe {
public static void main(String [] args) {
final int first = 1;
int last = 0;
try {
last = Integer.parseInt(args[0]);
} catch (NumberFormatException e)
{
System.out.println("Sorry, I can't handle that number");
System.exit(1);
}
final int total = first + last;
final int medium = total / 2;
String allStars = "";
for (int starCount=1;starCount<total+1;starCount++)
allStars = allStars.concat("*");
for (int countRows=1;countRows<total;countRows++) {
int countStars = countRows;
if (countRows > medium) {
countStars = total - countRows;
}
String newStar = allStars.substring(0, countStars);
System.out.println(newStar);
}
}
}
Here is the first run. Input is an even number:
$ java ShowMe 12
*
**
***
****
*****
******
******
*****
****
***
**
*
Here is the second run. Input in an odd number
$ java ShowMe 15
*
**
***
****
*****
******
*******
********
*******
******
*****
****
***
**
*
--
There are 10 types of people in this world. Those who understand binary
and those who don't