import java.util.HashMap;
import java.util.Map;
class Student {
String name;
String surname;
String id;
public Student(String name, String surname, String id) {
this.name = name;
this.surname = surname;
this.id = id;
}
@Override
public boolean equals(Object o) {
Student e = (Student) o;
return this.name.equals(e.name) && this.surname.equals(e.surname) && this.id.equals(e.id);
}
@Override
public int hashCode() {
return Integer.parseInt(id.substring(0, 3));
}
}
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;
}
@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);
for (Grades i : gradesParameter.keySet()) {
System.out.println("key: " + i + " value: " + gradesParameter.get(i));
}
}
}
I have a problem with understanding the whole structure and I do not know how to get the average marks of students and how to calculate themhow to get averarage from hashmap?
Comments (3)
- Popular
- New
- Old
You must be signed in to leave a comment
Lisa L
29 May 2022, 12:34
I kept your Grades class but it isn't really necessary, it just contains one array. With your approach (keeping the map) the best way would be to create a average() method inside the Grades class (returns sum of all grades / num grades)
+2
Karol Grzeszczak
29 May 2022, 13:45
these% s hard for me to understand and how to make the name and surname?
0
Karol Grzeszczak
29 May 2022, 14:13
ok i get it 😊
0