Java Reference
In-Depth Information
public static double computeAverage( )
{
double total = 0;
for ( int index = 0; index < numberUsed; index++)
total = total + score[index];
if (numberUsed > 0)
{
return (total/numberUsed);
}
else
{
System.out.println(
"ERROR: Trying to average 0 numbers.");
System.out.println("computeAverage returns 0.");
return 0;
}
}
/**
Precondition: numberUsed <= score.length.
The first numberUsed indexed variables of score have values.
Postcondition: Gives screen output showing how much each of the
first numberUsed elements of the array a differ from the average.
*/
public static void showDifference( )
{
double average = computeAverage( );
System.out.println("Average of the " + numberUsed
+ " scores = " + average);
System.out.println("The scores are:");
for ( int index = 0; index < numberUsed; index++)
System.out.println(score[index] +
" differs from average by "
+ (score[index] - average));
}
}
16. public static int max( int ... arg)
{
if (arg.length == 0)
{
System.out.println(
"Fatal Error: maximum of zero values.");
System.exit(0);
}
Search WWH ::




Custom Search