public class Add { // get numbers from the command line, right justify each, // show the sum and the average public static void main( String args[] ) { int sum = 0 ; int i ; for ( i = 0 ; i < args.length ; i++ ) { String s = args[ i ]; sum += Integer.parseInt( s ); while( s.length() < 20 ) { s = " " + s ; } System.out.println( s ); } System.out.println("--------------------"); String sumStr = " " + sum ; while( sumStr.length() < 15 ) { sumStr = " " + sumStr ; } System.out.println( "total" + sumStr ); String average = " " + ( sum / args.length ) ; while( average.length() < 13 ) { average = " " + average ; } System.out.println( "average" + average ); } }Look at the while loops. The same task is essentially done three times! In all cases, the while loop right justifies a string. Here is the same program with a custom rightJustify() method.
public class Add { private static String rightJustify( String s , int newLength ) { while( s.length() < newLength ) { s = " " + s ; } return s ; } // get numbers from the command line, right justify each, // show the sum and the average public static void main( String args[] ) { int sum = 0 ; int i ; for ( i = 0 ; i < args.length ; i++ ) { String s = args[ i ]; sum += Integer.parseInt( s ); System.out.println( rightJustify( s , 20 ) ); } System.out.println("--------------------"); String sumStr = " " + sum ; System.out.println( "total" + rightJustify( sumStr , 15 ) ); String average = " " + ( sum / args.length ) ; System.out.println( "average" + rightJustify( average , 13 ) ); } }My main() method is much simpler now.
The rightJustify() method takes two parameters. A string and an int. The method returns a string.
By looking at my main() method now, I see three cases where I get a number, format it and print it. I'm going to introduce one more method:
public class Add { private static String rightJustify( String s , int newLength ) { while( s.length() < newLength ) { s = " " + s ; } return s ; } private static void justifyAndPrint( String leadingText , int number , int totalLength ) { // convert the number to text String s = "" + number ; System.out.println( leadingText + rightJustify( s , totalLength - leadingText.length() ) ); } // get numbers from the command line, right justify each, // show the sum and the average public static void main( String args[] ) { int sum = 0 ; int i ; for ( i = 0 ; i < args.length ; i++ ) { String s = args[ i ]; int num = Integer.parseInt( s ); sum += num ; justifyAndPrint( "" , num , 20 ); } System.out.println("--------------------"); justifyAndPrint( "total" , sum , 20 ); justifyAndPrint( "average" , sum / args.length , 20 ); } }I think main() is even simpler now.
One more thing! Look at how "20" crops up over and over ...
public class Add { private static String rightJustify( String s , int newLength ) { while( s.length() < newLength ) { s = " " + s ; } return s ; } private static void justifyAndPrint( String leadingText , int number ) { // convert the number to text String s = "" + number ; System.out.println( leadingText + rightJustify( s , 20 - leadingText.length() ) ); } // get numbers from the command line, right justify each, // show the sum and the average public static void main( String args[] ) { int sum = 0 ; int i ; for ( i = 0 ; i < args.length ; i++ ) { String s = args[ i ]; int num = Integer.parseInt( s ); sum += num ; justifyAndPrint( "" , num ); } System.out.println("--------------------"); justifyAndPrint( "total" , sum ); justifyAndPrint( "average" , sum / args.length ); } }Looking good!
See page 44 in Just Java 1.2, or page 95 in Just Java 2 (sixth edition) for more information on methods.
And for a change of pace, you can visit my permaculture site.