2021_Programming_Exam_开卷考参考文档

本文最后更新于:2024年10月25日 下午

image-20231214115224920

image-20231214115243096

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
import java.util.ArrayList;
import java.util.Arrays;

public class Q2 {
private ArrayList<String> arrayList;
private double[] doubleArray;
private int rearOfDoubleArray;

public Q2(int initialSize) {
if (initialSize <= 0) {
throw new RuntimeException();
}
doubleArray = new double[initialSize];
arrayList = new ArrayList<>();
}

public ArrayList<String> getArrayList() {
return new ArrayList<>(arrayList);
}

public double[] getDoubleArray() {
return Arrays.copyOf(doubleArray, rearOfDoubleArray);
}



public void add(Number value) {
if(rearOfDoubleArray == doubleArray.length -1) {
double[] newDoubleArray = new double[doubleArray.length +5];
for (int i = 0; i < doubleArray.length; i++){
newDoubleArray[i] = doubleArray[i];
}
doubleArray = newDoubleArray;
}
doubleArray[rearOfDoubleArray] = value.doubleValue();
rearOfDoubleArray++;
}


public void add(Object value) {
arrayList.add(value.toString());

}

public boolean contains(Number value) {
for (double element : doubleArray) {
if (element == value.doubleValue()) {
return true;
}
}
return false;
}


public boolean contains(Object value) {
return arrayList.contains(value.toString());
}

public String toString() {
String s = "";
int count = 0;
for (String element : arrayList){
s += element + ",";
count++;
}
for (int i = 0; i < rearOfDoubleArray; i++) {
if (i != rearOfDoubleArray - 1) {
s += doubleArray[i] + ",";
}else {
s += doubleArray[i];
}
}
return s;
}
}

image-20231214115942916

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
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;

public class Q3 {
public static ArrayList<Student> readStudentsFile(String fileName) {
ArrayList<Student> students = new ArrayList<Student>();
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
String line;

while ((line = br.readLine()) != null) {
line = line.trim();
String[] parts = line.split(",");
Student s = new Student(parts[1], parts[0], parts[2]);
students.add(s);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return students;
}

public static void sortAge(ArrayList<Student> students) {
Collections.sort(students, (a, b) -> {
return a.getDateOfBirth().compareTo(b.getDateOfBirth());
});
}

public static void sortFamilyGivenName(ArrayList<Student> students) {
Collections.sort(students, (a, b) -> {
int comparison = a.getFamilyName().compareTo(b.getFamilyName());
if (comparison == 0) {
return a.getGivenName().compareTo(b.getGivenName());
}
return comparison;
});
}

public static Student getMedianAgeStudent(ArrayList<Student> students) {
sortAge(students);
int medIndex = students.size() / 2;
return students.get(medIndex);
}

public static Student getYoungestStudent(ArrayList<Student> students) {
sortAge(students);
int youngestIndex = students.size() - 1;
return students.get(youngestIndex);
}

public static Student getOldestStudent(ArrayList<Student> students) {
sortAge(students);
int oldestIndex = 0;
return students.get(oldestIndex);
}
}

Hint:

按照年龄大小排序 Collection.sort()

image-20231214130512400