Hands-on Programming Exercises — Ch. 7
for loop and the += operator.int[] numbers = new int[5];. Use a for loop to read values: numbers[i] = input.nextInt();. Then loop again to add each element to sum using sum += numbers[i];.max with the first element and compare with the rest using an if statement.int max = numbers[0]; then loop from index 1: if (numbers[i] > max) max = numbers[i];.length - 1) down to 0.for (int i = numbers.length - 1; i >= 0; i--) to traverse the array backwards. Print each element with a space separator.{1, 2, 3, 4, 5}, copy it using System.arraycopy, modify the copy, and show the original is unchanged.int[] copy = new int[original.length];. Then System.arraycopy(original, 0, copy, 0, original.length);. Modify copy[0] = 99; and print both arrays using a for loop.for-each). Divide the sum by array.length.for (double grade : grades) { sum += grade; } then double average = sum / grades.length;. Print with System.out.println("Average = " + average);.doubleElements that takes an int[] array and doubles every element in it. Print the array in main before and after calling the method to show that arrays are passed by reference.for (int i = 0; i < arr.length; i++) arr[i] *= 2;. Since arrays are passed by reference, changes inside the method affect the original array in main.createSquares that takes an int n and returns a new int[] array of size n where each element at index i contains (i+1)2. Print the returned array in main.int[] result = new int[n];. Fill it with a loop: result[i] = (i + 1) * (i + 1);. Then return result;. In main, receive it: int[] squares = createSquares(n);.int[][]), then compute the sum of all elements using nested for loops.int[][] matrix = new int[3][3];. Use nested loops to read: matrix[i][j] = input.nextInt();. Then nested loops again to sum: sum += matrix[i][j];.for loops — the outer loop iterates rows, the inner loop sums columns within each row.i, initialize int sum = 0;, then inner loop: sum += matrix[i][j];. Print "Row " + (i+1) + " sum = " + sum after each inner loop.