Logo

FRQ 1

Question 2: Iteration over 2D arrays (Unit 4)

Situation: You are developing a game where you need to track player scores on a 2D grid representing levels and attempts.

(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.

In Java, iterating over a 2D array means we traversing through each element of the array, row by row, and column by column. This is usually done by nested loops where the outer loop iterates by row and the inner loop iterates each element in the row, or by columns.

An example of this is turning a picture into grayscale. Since pixels of an image are in a 2D array, converting colored image to gray scale would require you to iterate through each pixel of the image and compute to the gray scale value for each of the pixels.

(b) Code: 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 static int calculateTotalScore(int[][] scores){

    // Initialize variable totalScore
    int totalScore = 0;

    // Iterate through each row representing players
    for (int i = 0; i < scores.length; i++) {

        // Iterate through each column and add the scores for each player
        for(int j = 0; j < scores[i].length; j++){
            // Add the score to the total
            totalScore += scores[i][j];
        }
    }
    return totalScore;
}
// Test
int[][] scores = {
    {99, 89, 98},
    {94, 85, 81},
    {75, 82, 74}
};

int test = calculateTotalScore(scores);
System.out.println("The total score is: " + test);
The total score is: 777

Question 3: ArrayList (Unit 6)

Situation: You are developing a student management system where you need to store and analyze the grades of students in a class.

(a) Define an arrayList in Java. Explain its significance and usefulness in programming.

An arrayList is a resizable array that contains elements of any datatype. They can change in size and are flexible. This is useful when storing collections that need to be modified as values can easily be added and removed.

(b) Code: You need to implement a method calculateAverageGrade that takes an arrayList grades of integers representing student grades and returns the average of all the elements in the arrayList. Write the method signature and the method implementation. Include comments to explain your code.

import java.util.ArrayList;

public static double calculateAverageGrade(ArrayList<Integer> grades) {
    // Initialize variable to hold the sum of grades
    int sum = 0;

    // Iterate through each grade in the ArrayList
    for (int grade : grades) {
        // Add the grade to the sum
        sum += grade;
    }

    // Calculate the average grade and return it
    double average = (double) sum / grades.size();
    return average;
}
// Test

ArrayList<Integer> grades = new ArrayList<>();
        grades.add(85);
        grades.add(90);
        grades.add(80);

double averageGrade = calculateAverageGrade(grades);
System.out.println("Average Grade: " + averageGrade);
Average Grade: 85.0

Question 4: Math Class (Unit 2)

(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.

The Math class in Java provides static methods for common mathematical operations. Instead of writing code that finds the absolute value of a number, you could just call the Math class and do Math.abs(number)

(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 static double calculateSquareRoot(double number){
    // Finds square root
    double root = Math.sqrt(number);

    // Returns square root value
    return root;
    
}
double number = 87;

double square_root = calculateSquareRoot(number);
System.out.println(square_root)
9.327379053088816