<h2>
Answer:</h2><h2>
</h2>
//import the Scanner class
import java.util.Scanner;
//begin class definition
public class AverageWeight{
//declare the main method
public static void main(String []args){
//declare the names array
String [] names = new String [3];
//declare the weights array
double [] weights = new double [3];
//Create an object of the Scanner class
Scanner input = new Scanner(System.in);
//create a loop to ask for the names and weights.
for(int i = 0; i< names.length; i++){
System.out.println("Enter name " + (i+1));
names[i] = input.next();
System.out.println("Enter weight " + (i+1));
weights[i] = input.nextDouble();
}
//find the sum of the weights
double sum = 0.0;
for(int j = 0; j< weights.length; j++){
sum += weights[j];
}
//find the average
double average = sum / weights.length;
//print out the average of the weights
System.out.printf("%s%f \n", "The average of the weights is ", average);
//print out the elements of the names array
System.out.println("Names : ");
System.out.print("{ ");
for(int i = 0; i< names.length; i++){
System.out.print(names[i] + " ");
}
System.out.print(" }");
//print out the elements of the weights array
System.out.println();
System.out.println();
System.out.println("Weights : ");
System.out.print("{ ");
for(int i = 0; i< weights.length; i++){
System.out.print(weights[i] + " ");
}
System.out.print(" }");
} //end of main method
} //end of class definition
<h2>Sample Output</h2>
>> Enter name 1
Peter
>> Enter weight 1
12.0
>> Enter name 2
Joe
>> Enter weight 2
23.4
>> Enter name 3
Paul
>> Enter weight 3
23.9
The average of the weights is 19.766667
Names :
{ Peter Joe Paul }
Weights :
{ 12.0 23.4 23.9 }
<h2>
Explanation:</h2>
The code contains comments explaining important lines.
The source code file has been attached to this response.
A sample output has also been provided.