A return type goes immediately before the method name
A method name goes immediately before the formal parameter/argument list
A formal parameter list is a comma-separated list of "type & name" pair(s) in parentheses
(VERY MUCH LIKE) variable declarations except for comma , (ie, not semi-colon ;)
When there are no parameters, must include empty ()
//----------------------------------------------------------
// swap the values of a[i] and a[j]
public static void swap (int a[], int i, int j){
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
//----------------------------------------------------------
// mix up the array contents
public static void shuffle(int a[]){
for(int i=0;i<20;i++){
swap( a
, (int)(Math.random()*a.length)
, (int)(Math.random()*a.length)
);
}
}
//----------------------------------------------------------
// return average of all array contents
public static double avg(int a[]){
double sum = 0;
for(int i=0;i<a.length;i++){
sum = sum + a[i];
}
return sum/a.length ;
}
//----------------------------------------------------------
public static void main(String args[]){
int arr1[] = { -10, -20, -30, -40, -50, -60, -70, -80, -90};
int arr2[] = { 19, 7, 22, 33, 88, 9, 0, 13, 44, 0, -808};
swap( arr1, 5, 7 );
shuffle( arr2 );
double arr2Avg = avg( arr2 );
System.out.printf("\n Avg of arr1 values is %f\n", avg( arr2 ) );
}
swap ( arr1, 5, 7 );
shuffle ( arr2 );
double arr2Avg = avg ( arr2 );
System.out.printf("\n Avg of arr1 values is %f\n", avg ( arr2 ) );