Suppose you have the following assignment:
In other words, I want to type
java Add 345 2 88888888 1000 55 55 55
and see
345
2
88888888
1000
55
55
55
--------------------
total 88890400
average 12698628
Let's start with the bare minimum for any program, no matter what it does.
public class Add
{
public static void main( String args[] )
{
}
}
Now, lets make it add two numbers and print the result:
public class Add
{
public static void main( String args[] )
{
int firstVal = Integer.parseInt( args[0] );
int secondVal = Integer.parseInt( args[1] );
int sum = firstVal + secondVal ;
System.out.println( "the sum is: " + sum );
}
}
So now I type
java Add 345 2 88888888 1000 55 55 55
and see
the sum is: 347
Now lets make the program add any number of values. Since I'm going to do a similar activity over and over, I think a loop is called for.
To determine how many times I go through the loop, I ask args for its length.
public class Add
{
public static void main( String args[] )
{
int sum = 0 ;
for ( int i = 0 ; i < args.length ; i++ )
{
sum += Integer.parseInt( args[ i ] );
}
System.out.println( "the sum is: " + sum );
}
}
So now I type
java Add 345 2 88888888 1000 55 55 55
and see
the sum is: 88890400
Now we'll add the averaging thing ...
public class Add
{
public static void main( String args[] )
{
int sum = 0 ;
for ( int i = 0 ; i < args.length ; i++ )
{
sum += Integer.parseInt( args[ i ] );
}
System.out.println( "the sum is: " + sum );
System.out.println( "the average is: " + ( sum / args.length ) );
}
}
Now I type
java Add 345 2 88888888 1000 55 55 55
and see
the sum is: 88890400
the average is: 12698628
What's left? printing all the numbers out and making everything look pretty. I'll print the numbers out first:
public class Add
{
public static void main( String args[] )
{
int sum = 0 ;
for ( int i = 0 ; i < args.length ; i++ )
{
System.out.println( args[ i ] );
sum += Integer.parseInt( args[ i ] );
}
System.out.println( "the sum is: " + sum );
System.out.println( "the average is: " + ( sum / args.length ) );
}
}
I type
java Add 345 2 88888888 1000 55 55 55
and see
345
2
88888888
1000
55
55
55
the sum is: 88890400
the average is: 12698628
I need everything to be right justified. This means that each line will need to be the same length with leading spaces making it so.
By looking at the assignment specs, the example has twenty hyphens. So each line needs to be twenty characters long.
The first line contains "345". This is a string with a length of three. 20 - 3 = 17. I'll need 17 spaces. For the next line I'll need
19 spaces. 12 spaces for the next ... Let's put these notes into our code:
public class Add
{
public static void main( String args[] )
{
int sum = 0 ;
for ( int i = 0 ; i < args.length ; i++ )
{
xxx
String arg = args[ i ];
int numSpaces = 20 - arg.length();
xxx print numSpaces spaces
xxx
System.out.println( args[ i ] );
sum += Integer.parseInt( args[ i ] );
}
System.out.println( "the sum is: " + sum );
System.out.println( "the average is: " + ( sum / args.length ) );
}
}
I'm thinking it would be great if there was a method somewhere that I could give a number and it would give me a string of that many spaces.
I looked around and couldn't find one. So maybe we should make one.
public class Add
{
static String spaces( int numSpaces )
{
String returnVal = "" ;
for( int i = 0 ; i < numSpaces ; i++ )
{
returnVal += " ";
}
return returnVal ;
}
public static void main( String args[] )
{
int sum = 0 ;
for ( int i = 0 ; i < args.length ; i++ )
{
String arg = args[ i ];
int numSpaces = 20 - arg.length();
System.out.print( spaces( numSpaces ) );
System.out.println( args[ i ] );
sum += Integer.parseInt( args[ i ] );
}
System.out.println( "the sum is: " + sum );
System.out.println( "the average is: " + ( sum / args.length ) );
}
}
Did this do the trick? I type
java Add 345 2 88888888 1000 55 55 55
and see
345
2
88888888
1000
55
55
55
the sum is: 88890400
the average is: 12698628
Getting closer!We need to add the line of twenty hyphens and reformat the results. Adding the line is easy. Reformatting the results can be tricky. We'll comment out the "average" line while trying things with the "total" line.
public class Add
{
static String spaces( int numSpaces )
{
String returnVal = "" ;
for( int i = 0 ; i < numSpaces ; i++ )
{
returnVal += " ";
}
return returnVal ;
}
public static void main( String args[] )
{
int sum = 0 ;
for ( int i = 0 ; i < args.length ; i++ )
{
String arg = args[ i ];
int numSpaces = 20 - arg.length();
System.out.print( spaces( numSpaces ) );
System.out.println( args[ i ] );
sum += Integer.parseInt( args[ i ] );
}
System.out.println("--------------------");
String total = "" + sum ;
System.out.println( spaces( 20 - total.length() ) + total );
//System.out.println( "the average is: " + ( sum / args.length ) );
}
}
I type
java Add 345 2 88888888 1000 55 55 55
and see
345
2
88888888
1000
55
55
55
--------------------
88890400
To put the word "total" in, my "spaces" will be shorter by the length of the word "total" - five.
public class Add
{
static String spaces( int numSpaces )
{
String returnVal = "" ;
for( int i = 0 ; i < numSpaces ; i++ )
{
returnVal += " ";
}
return returnVal ;
}
public static void main( String args[] )
{
int sum = 0 ;
for ( int i = 0 ; i < args.length ; i++ )
{
String arg = args[ i ];
int numSpaces = 20 - arg.length();
System.out.print( spaces( numSpaces ) );
System.out.println( args[ i ] );
sum += Integer.parseInt( args[ i ] );
}
System.out.println("--------------------");
String total = "" + sum ;
System.out.println( "total" + spaces( 15 - total.length() ) + total );
//System.out.println( "the average is: " + ( sum / args.length ) );
}
}
I type
java Add 345 2 88888888 1000 55 55 55
and see
345
2
88888888
1000
55
55
55
--------------------
total 88890400
Almost done! I uncomment the "average" line and make similar changes ...
public class Add
{
static String spaces( int numSpaces )
{
String returnVal = "" ;
for( int i = 0 ; i < numSpaces ; i++ )
{
returnVal += " ";
}
return returnVal ;
}
public static void main( String args[] )
{
int sum = 0 ;
for ( int i = 0 ; i < args.length ; i++ )
{
String arg = args[ i ];
int numSpaces = 20 - arg.length();
System.out.print( spaces( numSpaces ) );
System.out.println( args[ i ] );
sum += Integer.parseInt( args[ i ] );
}
System.out.println("--------------------");
String total = "" + sum ;
System.out.println( "total" + spaces( 15 - total.length() ) + total );
String average = "" + ( sum / args.length ) ;
System.out.println( "average" + spaces( 13 - average.length() ) + average );
}
}
I type
java Add 345 2 88888888 1000 55 55 55
and see
345
2
88888888
1000
55
55
55
--------------------
total 88890400
average 12698628
Done!The program could use a little cleaning up, but the important thing is that it works. A problem that seemed a little too big was overcome.
And for a change of pace, you can visit my permaculture site.