1

I am learning Java on my own and have just finished learning the basics of arrays, or so I think. I want to create a class grade sheet that keeps each student's grades and average. I have a for loop that asks each of the 10 students to enter their 4 test grades. Once I get those 4 grades, I take the average. I then store the student's grades and average into an array, one array per student. I essentially create 10 arrays with these 5 elements, for each student using the for loop. I now want to take the 5th element, the average of the 4 grades, from each student's array and populate another array called averages so I can perform other calculations. Is this possible with my logic? I think I could hard code 10 arrays, 1 for each student like so:

double averages[] = {student1[4], student2[4], ..., student10[4]};

Isn't this a bad way to go about this though? Any constructive help or guidance would be appreciated. Please do not post code that gives away the answer, as I won't learn from that. I just want a hint in the right direction. :)

Here's my code up until the point of confusion:

import java.util.Scanner;

public class ClassAverages {

    public static void main(String[] args) {

        Scanner keyboard = new Scanner(System.in);

        double grade1 = 0.0, grade2 = 0.0, grade3 = 0.0, grade4 = 0.0, average = 0.0;

        // get grades from each of the 10 students
        for (int student = 1; student <= 3; student++) {
            System.out.println("Student " + student);
            System.out.println("---------\n");
            System.out.print("Enter the first grade: ");
            grade1 = keyboard.nextDouble();
            while (grade1 < 0) { // input validation for grade 1
                System.out.print("You entered a negative value for grade. Please re-enter a positive grade: ");
                grade1 = keyboard.nextDouble();
            }
            System.out.print("Enter the second grade: ");
            grade2 = keyboard.nextDouble();
            while (grade2 < 0) { // input validation for grade 2
                System.out.print("You entered a negative value for grade. Please re-enter a positive grade: ");
                grade2 = keyboard.nextDouble();
            }
            System.out.print("Enter the third grade: ");
            grade3 = keyboard.nextDouble();
            while (grade3 < 0) { // input validation for grade 3
                System.out.print("You entered a negative value for grade. Please re-enter a positive grade: ");
                grade3 = keyboard.nextDouble();
            }
            System.out.print("Enter the fourth grade: ");
            grade4 = keyboard.nextDouble();
            System.out.println();
            while (grade4 < 0) { // input validation for grade 4
                System.out.print("You entered a negative value for grade. Please re-enter a positive grade: ");
                grade4 = keyboard.nextDouble();
                System.out.println();
            }

            // calculate the current student's average
            average = (grade1 + grade2 + grade3 + grade4) / 4;

            // for each student, 1 to 10, create an array with their 4 grades and average
            double studentX[] = { grade1, grade2, grade3, grade4, average };
            System.out.println("SCORE 1\t\tSCORE 2\t\tSCORE 3\t\tSCORE 4\t\tAVERAGE");
            System.out.print(studentX[0] + "\t\t");
            System.out.print(studentX[1] + "\t\t");
            System.out.print(studentX[2] + "\t\t");
            System.out.print(studentX[3] + "\t\t");
            System.out.print(studentX[4] + "\n");
            System.out.println();

            // I want to use each student's average for each corresponding element in the averages array 
            // create an array of all student's averages
            // double averages[] = {student1average, student2average,...student10average} ???
        }
    }
}

OUTPUT AS OF NOW:

Student 1
---------

Enter the first grade: 100
Enter the second grade: 100
Enter the third grade: 100
Enter the fourth grade: 100

SCORE 1     SCORE 2     SCORE 3     SCORE 4     AVERAGE
100.0       100.0       100.0       100.0       100.0

Student 2
---------

Enter the first grade: 90
Enter the second grade: 90
Enter the third grade: 90
Enter the fourth grade: 80

SCORE 1     SCORE 2     SCORE 3     SCORE 4     AVERAGE
90.0        90.0        90.0        80.0        87.5

Student 3
---------

Enter the first grade: 100
Enter the second grade: 100
Enter the third grade: 90
Enter the fourth grade: 80

SCORE 1     SCORE 2     SCORE 3     SCORE 4     AVERAGE
100.0       100.0       90.0        80.0        92.5

2 Answers 2

1

You could use a for loop. However this will require you to have a collection of items to iterate through. I think the best way for you to do this is to organize your students into their own separate class (give that class all the grades as parameters in its constructor and have it do the calculations for average within that class).

So if you call your class student then your constructor could look like thisstudent(int grade1, int grade2, int grade3, int grade4). Use these parameters to calculate the average for each student object.

Then in your ClassAverage class just instantiate new student objects as you want and add them to an array of student objects and then its as simple as iterating through the array of student, extracting the average field that you created for each student object (extract this simply by stating studentName.average, assuming you named your average field average in the student class).

So by the end you should have something that looks like this. Let's assume that the array of student objects is called studentArray

int[] averageArray = new int[studentArray.length];
for (int i = 0; i < studentArray.length; i++){
averageArray[i] = studentArray[i].average;
}

Good luck!

Sign up to request clarification or add additional context in comments.

3 Comments

I agree with class structure being the way to approach this. However, since he just learned arrays, I'm not sure if this would be in scope of the question he is asking.
in java 8 it can be: int[] averageArray = Stream.of(studentArray).map(student -> student.average).toArray();
I agree that this might be a bit complex but I have realized that one of the best ways to teach compsci is to encounter some difficult concepts and spend a lot of time trying to figure it out. It does you no good to shelter yourself from something that might be difficult! However I do completely agree that this might be a bit too complex for someone starting off.
1

Arrays are very bad form of storing information. Mostly because they don't contain logic that uses data inside this arrays and any other code can make that data invalid because array doesn't check what values you give it. This is what classes are for: to have data and code that works with it in one place, and to protect data from changes from other places in code.

So for your case arrays are OK for fields of the same type(for example, array of students is OK), but not for values of that types(array of arrays of students grades is NOT OK). So what you should do here is to use a class:

class Student
{
     int grades[] = new int[4];
     public Student(int grade1, grade2, int grade3, int grade4)
     {
          grades[0] = grade1; 
          grades[1]=grade2; 
          grades[2]=grade3; 
          grades[3]=grade4;
     }
     public double average()
     {
         double result = 0;
         for(int i =0; i<grades.count; i++)
           result += grades;
         return result / grades / count;
      }
  // and so on
}

They allow you to take logic inside object, sou you can use it like this:

Student albert = new Student(5, 4, 2, 1);
System.out.println(albert.avarage()); // prints 3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.