Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA PLEASE 13.15 Lab: Simple Arrays Analyzing People\'s Weights Start with the

ID: 3719010 • Letter: J

Question

JAVA PLEASE

13.15 Lab: Simple Arrays

Analyzing People's Weights

Start with the provided template code.

Test 1:
Using a loop, prompt the user to enter five numbers, that are five people's weights.
Store the numbers in an array of doubles.
Then use another loop to output the array's numbers on one line, with each number followed by one space.

Example:

Test 2:
Add code to loop and summing the array's elements to compute the total weight.
Output the result rounded to 2 decimal places with the last digit in column 24.

Test 3:
Add code to compute and output the average of the array's elements,
rounded to 2 decimal places, lined up on the decimal with the total.

Tests 4 - 6:
Add a method definition for a method to find and return the smallest weight in the array. The array will be passed into the method as a parameter.

Call the method and then display the returned value, rounded to 2 decimal places, lined up on the decimal with the total and average.

Example:

import java.util.Scanner;

public class WeightAnalysis {
final static int NUM_WEIGHTS = 5;

public static void main(String[] args) {
  
Scanner keyboard = new Scanner(System.in);
/* Add main method code here. */
  
return;
}

/* Add method definition here */

}

Explanation / Answer

import java.util.Scanner;

public class WeightAnalysis {

final static int NUM_WEIGHTS = 5;

public static void main(String[] args) {

  

Scanner keyboard = new Scanner(System.in);

/* Add main method code here. */

double a[] = new double[NUM_WEIGHTS];

for(int i=0;i<a.length;i++) {

System.out.println("Enter weight "+(i+1)+":");

a[i] = keyboard.nextDouble();

}

double sumWeight = 0,avgWeight = 0;

System.out.println("You entered weights:");

for(int i=0;i<a.length;i++) {

System.out.print(a[i]+" ");

}  

sumWeight=getTotalWeight(a);

avgWeight = sumWeight/a.length;

System.out.println("Total weight: "+sumWeight);

System.out.println("Average weight: "+avgWeight);

System.out.println("Min weight: "+getMinWeight(a));

return;

}

/* Add method definition here */

public static double getMinWeight(double a[]) {

double min = a[0];

for(int i=0;i<a.length;i++) {

if(min>a[i]) {

min = a[i];

}

}

return min;

}

public static double getTotalWeight(double a[]) {

double sum = 0;

for(int i=0;i<a.length;i++) {

sum+=a[i];

}

return sum;

}

}

Output:

Enter weight 1:
96.6
Enter weight 2:
212.2
Enter weight 3:
150.5
Enter weight 4:
120.1
Enter weight 5:
175.0
You entered weights:
96.6 212.2 150.5 120.1 175.0 Total weight: 754.4
Average weight: 150.88
Min weight: 96.6