# Task 6.5 Arrays - Solution

Make a GradeBook class with courseName and grades fields. Where courseName is a String and grades is a 2-dimensional int array. Each row represent one student and columns contains marks in different tests. e.g.

int[][] marks = {
{80,90,85},
{70, 95, 80}
};

Above array contains marks of 2 students for 3 different tests. Number of tests for each students can be different, so your program shall ask number of tests from user for each student. Also get number of students from user input. After fully initializing the GradeBook object, print following information statistics:

(Below lists is updated, some complex tasks removed)
1. Which student given maximum tests, print just index - index represent student id
2. Who got maximum average marks
3. Average marks of class
4. Average marks of each student
5. Minimum marks of each student
6. Maximum marks of each student

Soltution

```public class GradeBook {

private String courseName;

this.courseName = courseName;
}

}

public int getStudentWithMaxTests() {
//assume first student given highest number of tests
int maxTestsStudent = 0;
for (int i = 1; i < grades.length; i++)
maxTestsStudent = i;
return maxTestsStudent;
}

public int getStudentWithMaxAverage() {
// we need to find average for all students
// then we would check whose average is highest
for (int i = 0; i < grades.length; i++) {
averageMarks[i] = findAverageMarksOfStudent(i);
}

//assume, student 0 got the highest average
double highestAverage = averageMarks;
int studentNumber = 0;
for (int i = 1; i < averageMarks.length; i++) {
if (highestAverage < averageMarks[i]) {
highestAverage = averageMarks[i];
studentNumber = i;
}
}
return studentNumber;
}

public double findAverageMarksOfStudent(int studentIndex) {
double sum = 0;
for (int j = 0; j < grades[studentIndex].length; j++) {
}
}

public double getClassAverage() {
double sum = 0;
int totalTests = 0;
for (int i = 0; i < grades.length; i++) {
for (int j = 0; j < grades[i].length; j++) {
totalTests++;
}
}
return sum / totalTests;
}

public void printAverageMarksOfEachStudent() {
for (int i = 0; i < grades.length; i++) {
System.out.println("Average marks of student " + i + " = " + findAverageMarksOfStudent(i));
}
}

public int getMinimumMarks(int studentIndex) {
for (int j = 1; j < grades[studentIndex].length; j++) {
}
}
return minMarks;
}

public int getMaximumMarks(int studentIndex) {
for (int j = 1; j < grades[studentIndex].length; j++) {
}
}
return maxMarks;
}

public void printMinimumMarksOfEachStudent() {
for (int i = 0; i < grades.length; i++) {
System.out.println("Minimum marks of student " + i + " = " + getMinimumMarks(i));
}
}

public void printMaximumMarksOfEachStudent() {
for (int i = 0; i < grades.length; i++) {
System.out.println("Maximum marks of student " + i + " = " + getMaximumMarks(i));
}
}

// Standard get and set methods below
public String getCourseName() {
return courseName;
}

public void setCourseName(String courseName) {
this.courseName = courseName;
}

}

}

}
```

```import java.util.Scanner;

public static void main(String[] args) {
Scanner input1 = new Scanner(System.in);
Scanner input2 = new Scanner(System.in);

System.out.println("Enter the course name: ");
System.out.println("How many students are there in class: ");
int studentsCount = input1.nextInt();

for (int i = 0; i < grades.length; i++) { // for each student
System.out.println("How many tests student " + i + " given: ");
for (int j = 0; j < grades[i].length; j++) { //this loop get marks of student i in each test
System.out.println("Enter the marks of test " + j + " of student " + i);
}
}

System.out.println("Student who given maximum tests = " + gradeBook.getStudentWithMaxTests());
System.out.println("Student who got maximum average marks = " + gradeBook.getStudentWithMaxAverage());
System.out.println("Average marks of class = " + gradeBook.getClassAverage());
}
}
```