-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudent.java
More file actions
160 lines (128 loc) · 4.36 KB
/
Student.java
File metadata and controls
160 lines (128 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import java.util.LinkedList;
public class Student {
private String firstName;
private String lastName;
private int age;
private double averageGrade;
public Student(String firstName, String lastName, int age, double averageGrade) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.averageGrade = averageGrade;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public int getAge() {
return age;
}
public double getAverageGrade() {
return averageGrade;
}
// Переопределение метода toString() для удобного вывода информации о студенте
@Override
public String toString() {
return "Student{" +
"firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", age=" + age +
", averageGrade=" + averageGrade +
'}';
}
}
class HashTable<K, V> {
private static class Entry<K, V> {
private final K key;
private V value;
public Entry(K key, V value) {
this.key = key;
this.value = value;
}
public K getKey() {
return key;
}
public V getValue() {
return value;
}
public void setValue(V value) {
this.value = value;
}
}
private LinkedList<Entry<K, V>>[] table;
private int size;
public HashTable(int capacity) {
// Приведение типов для создания массива связанных списков
table = (LinkedList<Entry<K, V>>[]) new LinkedList[capacity];
size = 0;
}
private int hash(K key) {
return Math.abs(key.hashCode()) % table.length;
}
public void put(K key, V value) {
int index = hash(key);
if (table[index] == null) {
table[index] = new LinkedList<>();
}
for (Entry<K, V> entry : table[index]) {
if (entry.getKey().equals(key)) {
entry.setValue(value);
return;
}
}
table[index].add(new Entry<>(key, value));
size++;
}
public V get(K key) {
int index = hash(key);
if (table[index] != null) {
for (Entry<K, V> entry : table[index]) {
if (entry.getKey().equals(key)) {
return entry.getValue();
}
}
}
return null;
}
public void remove(K key) {
int index = hash(key);
if (table[index] != null) {
for (Entry<K, V> entry : table[index]) {
if (entry.getKey().equals(key)) {
table[index].remove(entry);
size--;
return;
}
}
}
}
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
}
class Main {
public static void main(String[] args) {
HashTable<String, Student> studentTable = new HashTable<>(10);
// Добавление студентов в хэш-таблицу
studentTable.put("ZK123", new Student("John", "Doe", 20, 4.5));
studentTable.put("ZK124", new Student("Jane", "Smith", 22, 4.8));
studentTable.put("ZK125", new Student("Michael", "Johnson", 21, 4.2));
// Получение информации о студентах по номеру зачетной книжки
System.out.println("Student ZK123: " + studentTable.get("ZK123"));
System.out.println("Student ZK124: " + studentTable.get("ZK124"));
System.out.println("Student ZK125: " + studentTable.get("ZK125"));
// Удаление студента
studentTable.remove("ZK124");
// Проверка размера таблицы
System.out.println("Size after removing ZK124: " + studentTable.size());
// Попытка получить студента, которого больше нет
System.out.println("Student ZK124 after removal: " + studentTable.get("ZK124"));
// Проверка на пустоту
System.out.println("Is table empty? " + studentTable.isEmpty());
}
}