简体   繁体   中英

Calculate average (grade) using HashMap in Java

Hey there I'm trying to calculate average from data in Map.

First in a code there is class Student in which a... well a student is created with parameters as follow: String firstName, String lastName, int registerDiaryNumber.

Also methods equals, hashCode and toString are Overridden.

Then the class Grade is created and it takes 8 int as parameter (8 grades total)

Finally in main there is this piece of code:

public static void main (String[] args) {

Student student1 = new Student("Terry", "Pratchett", 2);
Student student2 = new Student("Arashi", "Ryuuno", 3);
Student student3 = new Student("Nobunaga", "Oda", 4); 
Student student4 = new Student("Pheonix", "Wright", 5);
Student student5 = new Student("Ainz", "Gown", 1);

Grades student1Math = new Grades(4, 5, 4, 3, 2, 5, 5, 3);
Grades student2Math = new Grades(5, 6, 5, 5, 6, 5, 5, 4);
Grades student3Math = new Grades(3, 3, 5, 4, 3, 4, 5, 2);
Grades student4Math = new Grades(5, 4, 5, 3, 4, 5, 3, 5);
Grades student5Math = new Grades(4, 5, 5, 3, 3, 4, 6, 5);

Map<Student, Grades> mathGrades = new HashMap<>();
mathGrades.put(student1, student1Math);
mathGrades.put(student2, student2Math);
mathGrades.put(student3, student3Math);
mathGrades.put(student4, student4Math);
mathGrades.put(student5, student5Math);

for (Map.Entry<Student, Grades> entry : mathGrades.entrySet()){
    System.out.println("Number " + entry.getKey() +" got grades as follow:" +entry.getValue());
    System.out.println("Student average grade is: ");
    }
}

And thats where I stuck - I have no idea how to calculate average from given grades I tried putting methods in class grade but it did not work. If you want to check the whole code then please check the link below (This is an assignment from JAVA bootcamp and it states "using HashMap create a program that will store Students (personal) data and their grades. Program has to show average grade of every student." The average can be either an int or double (rounded).

https://kodilla.com/pl/project-java/173235#

You need to change the constructor of Grade class that takes a List of Integers for example.

The solution of the problem will be as simple as following:

public static void main(String[] args) {
    Student student1 = new Student("Terry", "Pratchett", 2);
    Student student2 = new Student("Arashi", "Ryuuno", 3);
    Student student3 = new Student("Nobunaga", "Oda", 4);
    Student student4 = new Student("Pheonix", "Wright", 5);
    Student student5 = new Student("Ainz", "Gown", 1);

    // A static initialization of a list of integers
    Grades student1Math = new Grades(new ArrayList<Integer>() {{
        add(5);
        add(6);
        add(5); // add more here...
    }});
    Grades student2Math = new Grades(new ArrayList<Integer>() {{
        add(5);
        add(6);
        add(5);
    }});
    Grades student3Math = new Grades(new ArrayList<Integer>() {{
        add(3);
        add(3);
        add(5);
    }});
    Grades student4Math = new Grades(new ArrayList<Integer>() {{
        add(5);
        add(4);
        add(5);
    }});
    Grades student5Math = new Grades(new ArrayList<Integer>() {{
        add(4);
        add(5);
        add(5);
    }};

    Map<Student, Grades> mathGrades = new HashMap<>();
    mathGrades.put(student1, student1Math);
    mathGrades.put(student2, student2Math);
    mathGrades.put(student3, student3Math);
    mathGrades.put(student4, student4Math);
    mathGrades.put(student5, student5Math);

    for (Map.Entry<Student, Grades> entry : mathGrades.entrySet()){
        double currentStudentAvgSum = 0.0;
        
        List<Integer> studentGrades = mathGrades.get(entry.getKey());
        for(Dobule currentGrade : studentGrades){
            currentStudentAvgSum + = currentGrade;
        }
        double currentStudentAvg = currentStudentAvgSum / studentGrades.size();
        
        System.out.println("Student average grade is: " + currentStudentAvg);
    }
}

And the for loop calculates average scores per student.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM