Array Ops

import java.util.Random;
import java.util.Scanner;

public class ArrayOps {

    private final int[] array; // Field marked as final

    /**
     * Constructor for the ArrayOps class. Dynamically allocates an array of the
     * given size and populates it with random integers in the range [-size,
     * size].
     *
     * @param size The size of the array to be created.
     */
    public ArrayOps(int size) {
        array = new int[size];
        Random rand = new Random();
        for (int i = 0; i < size; i++) {
            array[i] = rand.nextInt(2 * size + 1) - size;
        }
    }

    /**
     * Finds and returns the minimum value in the array.
     *
     * @return The minimum value in the array.
     */
    public int getMin() {
        int min = array[0];
        for (int value : array) {
            if (value < min) {
                min = value;
            }
        }
        return min;
    }

    /**
     * Finds and returns the maximum value in the array.
     *
     * @return The maximum value in the array.
     */
    public int getMax() {
        int max = array[0];
        for (int value : array) {
            if (value > max) {
                max = value;
            }
        }
        return max;
    }

    /**
     * Calculates and returns the average value of the array.
     *
     * @return The average value of the array as a double.
     */
    public double getAverage() {
        int sum = 0;
        for (int value : array) {
            sum += value;
        }
        return (double) sum / array.length;
    }

    /**
     * Returns a string representation of the array.
     *
     * @return A string containing the array elements.
     */
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("\n[ ");
        for (int i = 0; i < array.length; i++) {
            sb.append(array[i]);
            if (i < array.length - 1) {
                sb.append(", ");
            }
            if ((i + 1) % 10 == 0) {
                sb.append("\n  "); // each 10 numbers in new line
            }
        }
        sb.append(" ]");
        return sb.toString();
    }

    /**
     * The main driver class to test the ArrayOps class.
     */
    public static void main(String[] args) {
        // Use try-with-resources to automatically close the Scanner
        try (Scanner scan = new Scanner(System.in)) {
            boolean continuePlaying = true;
            System.out.println("--------------Welcome--------------");
            while (continuePlaying) {
                // Prompt the user for the sample size
                int size = 0;
                boolean validSize = false;
                while (!validSize) {
                    System.out.print("Enter the size of the array: ");
                    String input = scan.nextLine().trim(); // Robust input handling
                    try {
                        size = Integer.parseInt(input);
                        if (size > 0) {
                            validSize = true;
                        } else {
                            System.out.println("Size must be a positive integer. Please try again.");
                        }
                    } catch (NumberFormatException e) {
                        System.out.println("Invalid input. Please enter a valid integer.");
                    }
                }

                // Create an ArrayOps object with the given size
                ArrayOps arrayOps = new ArrayOps(size);

                // Output the array contents and the calculated values
                System.out.println("Array: " + arrayOps.toString());
                System.out.println("Minimum value: " + arrayOps.getMin());
                System.out.println("Maximum value: " + arrayOps.getMax());
                System.out.println("Average value: " + arrayOps.getAverage());
                System.out.println("-----------------------------------");

                // Ask the user if they want to continue
                while (true) {
                    System.out.print("Do you want to continue? (y/n): ");
                    String playAgain = scan.nextLine().trim().toLowerCase(); // Get user input

                    switch (playAgain) {
                        case "y" -> {
                            break; // Continue the outer loop
                        }
                        case "n" -> {
                            System.out.println("Program ended. Goodbye!");
                            return; // Directly exit the program
                        }
                        default ->
                            System.out.println("Invalid input. Please enter 'y' or 'n'.");
                    }
                    break;
                }
            }
        }
    }
}
Scroll to Top