Task 6.5 Arrays - Solution

Task Detail:

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;
private int[][] grades;

public GradeBook(String courseName, int[][] grades) {
this.courseName = courseName;
this.grades = grades;
}

public GradeBook() {
}

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

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

//assume, student 0 got the highest average
double highestAverage = averageMarks[0];
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++) {
sum += grades[studentIndex][j];
}
return sum / grades[studentIndex].length;
}

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++) {
sum += grades[i][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) {
int minMarks = grades[studentIndex][0];
for (int j = 1; j < grades[studentIndex].length; j++) {
if (grades[studentIndex][j] < minMarks) {
minMarks = grades[studentIndex][j];
}
}
return minMarks;
}

public int getMaximumMarks(int studentIndex) {
int maxMarks = grades[studentIndex][0];
for (int j = 1; j < grades[studentIndex].length; j++) {
if (maxMarks < grades[studentIndex][j]) {
maxMarks = grades[studentIndex][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;
}

public int[][] getGrades() {
return grades;
}

public void setGrades(int[][] grades) {
this.grades = grades;
}

}
```

GradeBookTest.java

```import java.util.Scanner;

public class GradeBookTest {

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

System.out.println("Enter the course name: ");
gradeBook.setCourseName(input2.nextLine());
System.out.println("How many students are there in class: ");
int studentsCount = input1.nextInt();
int[][] grades = new int[studentsCount][];

for (int i = 0; i < grades.length; i++) { // for each student
System.out.println("How many tests student " + i + " given: ");
grades[i] = new int[input1.nextInt()];
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);
grades[i][j] = input1.nextInt();
}
}
gradeBook.setGrades(grades);

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());
gradeBook.printAverageMarksOfEachStudent();
gradeBook.printMinimumMarksOfEachStudent();
gradeBook.printMaximumMarksOfEachStudent();
}
}
```