0

In my program I have subjects and each subject can have many assignments. I am trying to find the average grade of each assignment where "assignmentGradeAchieved" is not null.

In an array I have stored these assignmentGradeAchieved. I want to find the average result (adding each number together then dividing by the number of items) However it isn't displaying the correct arraysize. It is displaying it as 1 when it should be 2. Here is the line of code where I try to find the arraysize: arraySize = (assignmentGradesAchieved.size());

//For each assignment belonging to the subject, if AssignmentGradeAchieved is not null then add AssignmentGradeAchieved to an arraylist called AssignmentGradesAchieved. //If a subject has more than one assignments that have AssignmentGradeAchieved then more than one AssignmentGradeAchieved should be added to the arryalist called AssignmentGradesAchieved. Therefore the size of the array should be more than one. //one of my subjects has two assignments both with AssignmentGradeAchieved so the array size should be 2 not 1

    for(int i=0; i<subjects.size(); i++){ //for each subject in the list
        Subject subject = subjects.get(i);


        List<Assignment> assignments = subject.getAssignment(); //Get the list of assignments belonging to current subject

            for(int y=0; y<assignments.size(); y++ ) { //For each assignment in the list 
                Assignment assignment = assignments.get(y);

                ArrayList<Double> assignmentGradesAchieved = new ArrayList<Double>(); 
                if(assignments.get(y).getAssignmentGradeAchieved()!=null) {
                    assignmentGradesAchieved.add(assignments.get(y).getAssignmentGradeAchieved());
                    arraySize = 0;
                    arraySize = (assignmentGradesAchieved.size());  
                    for(int z=0; z<assignmentGradesAchieved.size(); z++) {
                        arraySize = 0;
                        allAssignments = allAssignments + assignmentGradesAchieved.get(z);

                        arraySize = (assignmentGradesAchieved.size());      
                        averageAssignGrade = (allAssignments / arraySize);

                    }
                }
    }


    }

THE SOLUTION:

   for(int i=0; i<subjects.size(); i++){ //for each subject in the list
        Subject subject = subjects.get(i);

        List<Assignment> assignments = subject.getAssignment(); //Get the list of assignments belonging to current subject
        ArrayList<Double> assignmentGradesAchieved = new ArrayList<Double>(); 

            for(int y=0; y<assignments.size(); y++ ) { //For each assignment in the list 
                Assignment assignment = assignments.get(y);
                allAssignments =0;

                if(assignments.get(y).getAssignmentGradeAchieved()!=null) {
                    assignmentGradesAchieved.add(assignments.get(y).getAssignmentGradeAchieved());

                for(int z=0; z<assignmentGradesAchieved.size(); z++) {

                    allAssignments = allAssignments + assignmentGradesAchieved.get(z);
                }
                }

    }
            arraySize = 0;
            arraySize = (assignmentGradesAchieved.size());  
            averageAssignGrade = (allAssignments / arraySize);


    }
3
  • 1
    Why do you say it should be 2 when there is only one assignmentGradesAchieved.add Commented Jul 12, 2018 at 20:08
  • @alanfcm So i have for each assignment in assignments " assignmentGradesAchieved.add(assignments.get(y).getAssignmentGradeAchieved());" - basically add assignmentGradeAchieved to this arraylist for each assignment in assignments (that belong to the current subject) so one of my subjects has 2 assignments. so there should be two assignmentGradeAchieved added to the arraylist Commented Jul 12, 2018 at 20:11
  • @alanfcm there is only one but it is in a for loop so it should do it for each assignment Commented Jul 12, 2018 at 20:12

2 Answers 2

2

Your assignmentGradesAchieved declaration is inside your for loop, therefore it is always getting reset so your variable will only always have one assignment grade. Put your declaration out of the loop

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

1 Comment

Thank you so much that worked, can't believe I didn't spot that haha
1

In the code, you are calculating the average every time you find an assignmentGradeAchieved, which is wrong. There are two main strategies:

  • Save the collected numbers and then calculate the average
  • Save separately the sum of assignmentGradeAchieved and the number of assignmentGrade. Then divide to get the average.

For the first case you should have something like:

for(int i=0; i<subjects.size(); i++){ //for each subject in the list
            Subject subject = subjects.get(i);


            List<Assignment> assignments = subject.getAssignment(); //Get the list of assignments belonging to current subject
            // declare outside the loop
            ArrayList<Double> assignmentGradesAchieved = new ArrayList<Double>(); 
            // collect all the grades
            for(int y=0; y<assignments.size(); y++ ) { //For each assignment in the list 
                Assignment assignment = assignments.get(y);
                if(assignments.get(y).getAssignmentGradeAchieved()!=null) {
                    assignmentGradesAchieved.add(assignments.get(y).getAssignmentGradeAchieved());
                }
            }
            // calculate average
            Double allAssignments = 0;
            for(int z=0; z<assignmentGradesAchieved.size(); z++) {
                allAssignments = allAssignments + assignmentGradesAchieved.get(z);
            } 
            int arraySize = (assignmentGradesAchieved.size()); 
            Double averageAssignGrade = (allAssignments / arraySize);
        }

PD: Please be careful with your identation.

PD2: This isn't the best approach (and is purposedly written in this way the solution that I posted), the second one is better. You should try to do it as a homework.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.