-5

I have a problem with understanding the whole structure and I do not know how to get the average grades of students and how to calculate them

class Grades  {
    int grade1;
    int grade2;
    int grade3;
    int grade4;
    int grade5;
    int grade6;
    int grade7;

    public Grades(int grade1, int grade2, int grade3, int grade4, int grade5, int grade6, int grade7) {
        this.grade1 = grade1;
        this.grade2 = grade2;
        this.grade3 = grade3;
        this.grade4 = grade4;
        this.grade5 = grade5;
        this.grade6 = grade6;
        this.grade7 = grade7;
    }
    public int getGrade1() {
        return grade1;
    }
    @Override
    public String toString() {
        return grade1 + ", " + grade2 + ", " +  grade3 + ", " +  grade4 + ", " +  grade5 + ", " +  grade6 + ", " +  grade7;
    }
}
  public class ObjectsTestetON {
  public static void main(String[] args) {
  Student student1 = new Student("Ania", "Nowak", "00001");
  Student student2 = new Student("Marcin", "Kasprzak", "00002");
  Student student3 = new Student("Kasia", "Siodłak", "00003");
  Student student4 = new Student("Błażej", "Markowski", "00004");

  Grades grad1 = new Grades(5, 4, 5, 4, 5, 5, 4);
  Grades grad2 = new Grades(3, 4, 5, 4, 3, 4, 4);
  Grades grad3 = new Grades(4, 4, 5, 4, 4, 5, 4);
  Grades grad4 = new Grades(2, 4, 3, 4, 3, 2, 3);

  Map<Student, Grades> gradesParameter = new HashMap<>();

  gradesParameter.put(student1, grad1);
  gradesParameter.put(student2, grad1);
  gradesParameter.put(student3, grad1);
  gradesParameter.put(student4, grad1);


 //generally i don't know how to construct this :(
 // to get avearge value for every student grades
  for (Grades i : gradesParameter.keySet()) { // :(
  System.out.println("key: " + i + " value: " + gradesParameter.get(i)); // :(
   }
  }
}

I did it as it was in my course generally every grades1, 2 etc you could assign a specific name like test card homework or other but I did not do it I left it as an assessment 1 2 3 4 .. everything can be done in several ways and this is probably not very effective for this type of issue normally you could use different indicators in this example average grades without grades from homework etc but it was not important I modeled only like example

EurekaII
  • 11
  • 5
  • 2
    Can you share the `Student` and `Grades` class? – arsho May 29 '22 at 11:59
  • Average grade for single student, or average for all? – Chaosfire May 29 '22 at 12:01
  • You get a compilation-error in the head of the for-loop, right ? `Grades i : gradesParameter.keySet()` should raise a type-mismatch error. Please post all error-output (including stacktrace) to your answer. – hc_dev May 29 '22 at 12:08
  • for all students – EurekaII May 29 '22 at 12:17
  • 2
    @EurekaII Side note: You are assigning `grad1` for all the different students. You most likely want to use `grad1` to `grad4` as well. – Progman May 29 '22 at 12:23
  • @Progman by undoing the changes, I reverted to the point when I was assigning values ​​my fault – EurekaII May 29 '22 at 12:45
  • As a side question did you override `equals` and `hashCode` in your `Student` class? If not, your map lookups for **new instances** of existing students will not work. – WJS May 29 '22 at 13:25
  • @WJS i add Grade class i question – EurekaII May 29 '22 at 14:24

2 Answers2

0

The Map is defined with key of type Student and value of type Grades.

  • To get all keys from a map use Map.keySet() which returns a Set (no duplicates!) of keys.
  • To get all values from a map use Map.values() which returns a Collection (may contain duplicates) of values.
Map<Student, Grades> gradesParameter = new HashMap<>();

// get all keys
Set<Student> students = gradesParameter.keySet();

// keys to print all students
for (Student student : gradesParameter.keySet()) { // here was your error
  System.out.println("key: " + student + " value: " + gradesParameter.get(student)); // works by using student as key
}

// get all values
Collection<Grades> grades = gradesParameter.values();
System.out.println("values: " + grades);
  
// values to calculate the average value of students' grades
double averageGradeOfCourse = calculateAverage(grades); // the overall average over all students' grades

The calculation depends on the definition of your value class Grades.

For example:

  • the average of multiple students' grades (e.g. the course-average)
public static double calculateAverage(Collection<Grades> allGrades) {
    int total = 0;
    double count = 0; // typed double only to have result a double

    // accumulate total and count over all grades
 
    return count == 0 ? 0 : (total / count); // attention: div by zero!
}

Simple average getter in class Grades

Given your implementation of class Grades which holds separate int fields for 7 grades.

You could add a method getAverage() to your class Grades that returns a double as average of all 7 grades.

class Grades  {
    int grade1;
    int grade2;
    int grade3;
    int grade4;
    int grade5;
    int grade6;
    int grade7;

    public Grades(int grade1, int grade2, int grade3, int grade4, int grade5, int grade6, int grade7) {
        this.grade1 = grade1;
        this.grade2 = grade2;
        this.grade3 = grade3;
        this.grade4 = grade4;
        this.grade5 = grade5;
        this.grade6 = grade6;
        this.grade7 = grade7;
    }

    // field getter and toString methods omitted for brevity 

    // could also name it getAverage()
    public double asAverage() {
        long total = 0;  // suspect the sum of ints could be long
        // use a double here (although int would be sufficient) to have double as result of division 
        double count = 7;  // count of all occurrences accumulated, must not be zero!

        // calculation starts here
        total = grade1 + grade2 + grade3 +  grade4 +  grade5 + grade6 + grade7;

        return total / count;  // count must not be 0 to prevent division by zero error!
    }
}
hc_dev
  • 5,553
  • 20
  • 27
  • java: cannot find symbol symbol: class Set location: class ObjectsTestetON – EurekaII May 29 '22 at 12:19
  • java: cannot find symbol symbol: class Collection location: class ObjectsTestetON – EurekaII May 29 '22 at 12:20
  • @EurekaII You might learn about essential collection-classes and `import`s in Java to fix this. I left out the imports from my code-example as well as the average-calculation. – hc_dev May 29 '22 at 12:24
  • @hc_dev I will try to follow your example more and be a better StackOverflow Citizen/Community member :) – Sash Sinha May 29 '22 at 12:37
  • it was about the average grade of a single student not the whole group – EurekaII May 29 '22 at 12:57
  • 1
    For an average you might want to use doubles for `total` and `return type`. – WJS May 29 '22 at 13:30
  • @WSJ, Golden rule for [division of integers in Java](https://stackoverflow.com/questions/7220681/division-of-integers-in-java) – hc_dev May 29 '22 at 13:47
  • it's get loong average Average: 4.285714285714286 thanks for your help – EurekaII May 29 '22 at 14:56
0

I don't know what your Grades class looks like, but I assume it contains an int[]:

public class Grades {
    private final int[] grades;
    
    public Grades(int... grades) {
        this.grades = grades;
    }

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

To find the average grade out of all grades:

gradesParameter.values().stream().mapToDouble(grades -> IntStream.of(grades.getGrades()).average().getAsDouble()).average().getAsDouble();

To find every average grade if all students have the same amount of them:

Collection<Grades> values = gradesParameter.values();
double[] result = new double[7];
int size = values.size();
for (Grades grades : values) {
    int[] grades = grades.getGrades();
    for (int i = 0; i < grades.length; i++) {
        result[i] += grades[i] / size;
    }
}
Cactusroot
  • 800
  • 2
  • 16