Type-1: 

Here's a Java program to find the average of elements in a floating-point array, along with an explanation of each step:


public class ArrayAverage
public static void main(String[] args)
// Step 1: Define a floating-point array with some elements 
double[] numbers = { 10.5, 20.3, 30.7, 40.2, 50.9 };
// Step 2: Initialize variables to store the sum and count of elements 
double sum = 0.0
int count = numbers.length; 
// Step 3: Calculate the sum of all elements in the array 
// Using a for loop to iterate through each element in the array 
for (double number : numbers) {
 sum += number; 
 } 
// Step 4: Calculate the average by dividing the sum by the number of elements double average = sum / count;
// Step 5: Print the average of the elements
System.out.println("The average of the elements in the array is: " + average);
 } 
}

Explanation of each step:

  1. We start by defining a floating-point array named numbers. In this example, we have an array with elements {10.5, 20.3, 30.7, 40.2, 50.9}. You can modify this array to have any set of floating-point elements.
  2. Next, we initialize two variables: sum to store the sum of all elements, and count to store the number of elements in the array. Since we know the number of elements in the array, we set count to the length of the numbers array (numbers.length).
  3. We use a for-each loop to iterate through each element of the numbers array. The loop variable number will hold the value of the current element.
  4. Inside the loop, we add the current element (represented by number) to the sum variable. This step is repeated for each element in the array, effectively calculating the sum of all elements.
  5. After the loop has finished, we have the sum of all elements in the array. To find the average, we divide the sum by the number of elements (count).
  6. Finally, we print the average of the elements using System.out.println().


When you run this program, it will output:

The average of the elements in the array is: 30.32

This is because the average of {10.5, 20.3, 30.7, 40.2, 50.9} is approximately 30.32. You can modify the array elements to find the average with different sets of floating-point numbers.

Type-2:

 Another way to find the average of elements in a floating-point array is by using a traditional for loop. Here's the Java program with the traditional for loop:


public class ArrayAverage
public static void main(String[] args) {
// Step 1: Define a floating-point array with some elements 
double[] numbers = { 10.5, 20.3, 30.7, 40.2, 50.9 };
// Step 2: Initialize variables to store the sum and count of elements 
double sum = 0.0; int count = numbers.length; 
// Step 3: Calculate the sum of all elements in the array using a traditional for loop 
for (int i = 0; i < count; i++) { 
 sum += numbers[i]; 
 }
// Step 4: Calculate the average by dividing the sum by the number of elements
double average = sum / count; 
// Step 5: Print the average of the elements 
 System.out.println("The average of the elements in the array is: " + average); 
 } }

Explanation of the alternative method:

  1. We start by defining a floating-point array named numbers, just like in the previous example.
  2. Next, we initialize two variables: sum to store the sum of all elements, and count to store the number of elements in the array. As before, we set count to the length of the numbers array (numbers.length).
  3. We use a traditional for loop to iterate through the array. The loop variable i starts from 0 and runs until i < count, where count gives the total number of elements in the array.
  4. Inside the loop, we add the current element (represented by numbers[i]) to the sum variable. This step is repeated for each element in the array, effectively calculating the sum of all elements.
  5. After the loop has finished, we have the sum of all elements in the array. To find the average, we divide the sum by the number of elements (count).
  6. Finally, we print the average of the elements using System.out.println().


When you run this program, it will produce the same output:

The average of the elements in the array is: 30.32

Both the for-each loop and traditional for loop achieve the same result of calculating the average of elements in the array. You can use either method based on your preference or specific use case.

Type-3:

 Here's another way to find the average of elements in a floating-point array using Java Streams:


import java.util.Arrays; 
public class ArrayAverage
public static void main(String[] args)
// Step 1: Define a floating-point array with some elements 
double[] numbers = { 10.5, 20.3, 30.7, 40.2, 50.9 }; 
// Step 2: Use Java Streams to calculate the average of elements 
double average = Arrays.stream(numbers).average().orElse(0.0);
// Step 3: Print the average of the elements
System.out.println("The average of the elements in the array is: " + average);
 } 
}

Explanation of the alternative method:

  1. We start by defining a floating-point array named numbers, just like in the previous examples.
  2. We use Java Streams to calculate the average of elements in the array. The Arrays.stream(numbers) converts the array numbers into a Stream of double values. Then, we use the average() method of the Stream to get an OptionalDouble, which may contain the average of the elements if the array is non-empty. We use the orElse(0.0) method to provide a default value of 0.0 if the array is empty or if the average is not present.
  3. Finally, we print the average of the elements using System.out.println().


When you run this program, it will produce the same output as before:

The average of the elements in the array is: 30.32

Using Java Streams provides a concise and expressive way to find the average of elements in an array. It simplifies the code and is particularly useful when working with large datasets or more complex operations on arrays.