FRQ 1

(a) Explain the concept of iteration over a 2D array in Java. Provide an example scenario where iterating over a 2D array is useful in a programming task.

Iteration over a 2D array in Java involves traversing through each element of the array using nested loops. Since a 2D array is essentially an array of arrays, you’ll typically use two loops: one for the rows and another for the columns.

When you want to calculate the sum of an entire matrix, using iteration over 2D arrays would be useful in this kind of scenario.

(b) You need to implement a method calculateTotalScore that takes a 2D array scores of integers representing player scores and returns the sum of all the elements in the array. Write the method signature and the method implementation. Include comments to explain your code.

public class Main {
    public static void main(String[] args) {
        int[][] student Scores = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
        int sum = 0;
        
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                sum += matrix[i][j];
            }
        }
        
        System.out.println("Total Scores: " + sum);
    }
}

Main.main(null);
Sum of all elements in the matrix: 45

FRQ 4

a) Discuss the purpose and utility of the Math class in Java programming. Provide examples of at least three methods provided by the Math class and explain their usage. To do math functions. Math.random gives a random float to get a random value Math.pow raises a value to a power Math.min finds the minimum of two numbers

(b) Code:

You need to implement a method calculateSquareRoot that takes a double number as input and returns its square root using the Math class. Write the method signature and the method implementation. Include comments to explain your code.

import java.lang.Math.*;
public class Main {
    public static double calculateSquareRoot(double root) {
        return Math.sqrt(root); //square root the number
    }
    public static void main (String[] args) {
        System.out.println(calculateSquareRoot(5.4002));
    }
}
Main.main(null);
2.3238330404742937