Post

Created by @nathanedwards
 at November 2nd 2023, 2:15:45 am.

Question:

Write a Java program that asks the user to enter the length of an array, and then prompts the user to enter integers to fill the array. The program should then calculate the sum of all the elements in the array and display the result.

You need to declare and initialize an integer array of the specified length, and use a loop to read the user's input and store it in the array. Finally, you should calculate the sum of the elements in the array using a loop and display the result.

Your program should contain the following methods:

  1. public static void main(String[] args): This is the main method where the program execution starts. It should call the other methods to perform the required tasks.
  2. public static int[] createArray(int length): This method accepts an integer length as input and returns a new empty integer array of the specified length.
  3. public static void fillArray(int[] arr): This method prompts the user to enter integers to fill the array arr using a loop.
  4. public static int calculateSum(int[] arr): This method calculates and returns the sum of all the elements in the array arr.

You can assume that the user will only enter valid integer inputs.

Provide your implementation of the program along with the required methods and detailed explanations.

Answer:

import java.util.Scanner;

public class ArraySum {
    
    public static void main(String[] args) {
        // Step 1: Read the length of the array from the user
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the length of the array: ");
        int length = scanner.nextInt();

        // Step 2: Create an array of the specified length
        int[] arr = createArray(length);

        // Step 3: Fill the array with user input
        fillArray(arr);

        // Step 4: Calculate the sum of the array elements
        int sum = calculateSum(arr);

        // Step 5: Display the result
        System.out.println("Sum of the array elements: " + sum);
        
        scanner.close();
    }
    
    public static int[] createArray(int length) {
        return new int[length];
    }
    
    public static void fillArray(int[] arr) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter " + arr.length + " integers:");

        for (int i = 0; i < arr.length; i++) {
            System.out.print("Enter integer #" + (i + 1) + ": ");
            arr[i] = scanner.nextInt();
        }

        scanner.close();
    }
    
    public static int calculateSum(int[] arr) {
        int sum = 0;

        for (int i = 0; i < arr.length; i++) {
            sum += arr[i];
        }

        return sum;
    }
}

Explanation:

  1. We start by importing the Scanner class from the java.util package, which allows us to read user input from the console.

  2. In the main method, we first prompt the user to enter the length of the array and store it in the variable length using the nextInt() method of the Scanner class.

  3. We then call the createArray() method and pass length as an argument to create an empty integer array of the specified length.

  4. Next, we call the fillArray() method and pass the created array as an argument. Inside this method, we prompt the user to enter integers to fill the array using a loop. The entered values are stored in the respective elements of the array.

  5. After that, we call the calculateSum() method and pass the array as an argument. Inside this method, we calculate the sum of all the elements in the array using a loop and store it in the variable sum.

  6. Finally, we display the result by printing the value of sum along with an appropriate message.

Note: It is important to close the Scanner object to avoid resource leakage.