Question 2
a: Iterating over a 2D array in Java is a process to iterating and most likely perfume some kind of action or check on each value. To do this you need nested arrays. An example of a 2D Array is a chess board where the pieces positions are stored in 2 dimensions.dq
b:
public class GameScoreCalculator {
public static int calculateTotalScore(int[][] scores) {
int totalScore = 0;
for (int i = 0; i < scores.length; i++) {
for (int j = 0; j < scores[i].length; j++) {
totalScore += scores[i][j];
}
}
return totalScore;
}
public static void main(String[] args) {
int[][] scores = {
{1, 2, 1040},
{20, 104, 12},
{400015, 27, 13}
};
int totalScore = calculateTotalScore(scores);
System.out.println("Total: " + totalScore);
}
}
GameScoreCalculator.main(null);
Total: 401234
Question 3
a: An array is a way to store a group of objects, and their size cannot change. It is useful to hold a group of numbers, strings, or your own objects you need to have multiple of.
b:
public class GradeCalc {
public static double calculateAverageGrade(int[] grades) {
int sum = 0;
int count = grades.length;
for (int i = 0; i < grades.length; i++) {
sum += grades[i];
}
double average = (double) sum / count;
return average;
}
public static void main(String[] args) {
int[] studentGrades = {4, 100, 82, 71, 92};
double averageGrade = calculateAverageGrade(studentGrades);
System.out.println("Average Grade: " + averageGrade);
}
}
GradeCalc.main(null);
Average Grade: 69.8