Question: 4 . 5 People's weights ( Java ) ( 1 ) Prompt the user to enter five numbers, being five people's weights. Store the numbers

4.5 People's weights (Java)
(1) Prompt the user to enter five numbers, being five people's weights. Store the numbers in an array of doubles. Output the array's numbers on one line, each number followed by one space. (2 pts)
Ex:
Enter weight 1: 236
Enter weight 2: 89.5
Enter weight 3: 142
Enter weight 4: 166.3
Enter weight 5: 93
You entered: 236.089.5142.0166.393.0
(2) Also output the total weight, by summing the array's elements. (1 pt)
(3) Also output the average of the array's elements. (1 pt)
(4) Also output the max array element. (2 pts)
Ex:
Enter weight 1: 236
Enter weight 2: 89.5
Enter weight 3: 142
Enter weight 4: 166.3
Enter weight 5: 93
You entered: 236.089.5142.0166.393.0
Total weight: 726.8
Average weight: 145.35999999999999
Max weight: 236.0 import java.util.Scanner;
public class PeopleWeights {
public static void main(String[] args){
/* Type your code here. */
Scanner input = new Scanner(System.in);
double weights[]= new double[5];
int i =0;
while (i <5){
System.out.println("Enter weight "+(i +1)+":");
weights[i]= input.nextDouble();
i++;
}
outputWeights(weights);
System.out.println(totalWeight(weights));
System.out.println(averageWeight(weights));
System.out.println(maxWeight(weights));
}
public static void outputWeights(double[] arr){
System.out.print("You entered: ");
for (int i =0; i < arr.length; i++){
System.out.print(arr[i]+"");
}
System.out.println("");
}
public static double totalWeight(double[] arr){
double sum =0;
for (int i =0; i < arr.length; i++){
sum = sum + arr[i];
}
return sum;
}
public static double averageWeight(double[] arr){
return totalWeight(arr)/ arr.length;
}
public static double maxWeight(double[] arr){
double temp = arr[0];
for (int i =1; i < arr.length; i++){
if (arr[i]> temp){
temp = arr[i];
}
}
return temp;
}
} Output differs. See highlights below.
Special character legend
Input
236
89.5
142
166.3
93
Your output starts with
Enter weight 1:
Enter weight 2:
Enter weight 3:
Enter weight 4:
Enter weight 5:
You entered: 236.089.5142.0166.393.0
726.8
Expected output starts with
Enter weight 1:
Enter weight 2:
Enter weight 3:
Enter weight 4:
Enter weight 5:
You entered: 236.089.5142.0166.393.0

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!