-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStudentStatsArray.java
149 lines (137 loc) · 3.19 KB
/
StudentStatsArray.java
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
public class StudentStatsArray
{
private final Student[] students;
public StudentStatsArray(Student[] students)
{
this.students = students;
}
// Returns the average gpa of the students
public double averageGpa()
{
double total = 0;
for (Student s: students)
{
total += s.getGpa();
}
return total / students.length; // t
}
// Returns the gpa range of the students
public double getGpaRange()
{
double maxGpa = students[0].getGpa(), minGpa = students[0].getGpa();
for (Student s: students)
{
if (s.getGpa() > maxGpa)
{
maxGpa = s.getGpa();
}
else if (s.getGpa() < minGpa)
{
minGpa = s.getGpa();
}
}
return maxGpa - minGpa;
}
// Returns the name of the student that has the longest length
public String getLongestName()
{
String longestName = "";
for (Student s: students)
{
if (s.getName().length() > longestName.length())
longestName = s.getName();
}
return longestName;
}
// Returns a count of the number freshman students
public int getNumFreshman()
{
int numFreshman = 0;
for (Student s: students)
{
if (s.getYear() == 1)
numFreshman++;
}
return numFreshman;
}
// Returns the index of the first student with a name equal to name.
// Returns -1 if not found
public int search(String name)
{
for (int i = 0; i < students.length; i++)
{
if (students[i].getName().contains(name))
{
return i;
}
}
return -1;
}
// Returns the index of the first student with a gpa greater than or equal to the gpa
// Returns -1 if not found
public int search(double gpa) // me when
{
for (int i = 0; i < students.length; i++)
{
if (students[i].getGpa() >= gpa)
return i;
}
return -1;
}
// Returns 1 if the students are sorted in ascending order by their gpa; -1 if they
// are sorted in descending order; 0 otherwise.
public int sortStatus()
{
int sort = 0;
double prevGpa = 0;
prevGpa = students[0].getGpa();
for (Student s: students)
{
if (s.getGpa() >= prevGpa)
{
sort = 1;
prevGpa = s.getGpa();
}
else
{
sort = 0;
break;
}
if (sort == 0)
break;
}
if (sort == 1)
return sort;
prevGpa = students[0].getGpa();
for (Student s: students)
{
if (s.getGpa() <= prevGpa)
{
sort = -1;
prevGpa = s.getGpa();
}
else
{
sort = 0;
break;
}
if (sort == 0)
break;
}
return sort;
}
// Returns the array of students in JSON like format
public String toString() {
String output = "[\n";
for (int i = 0; i < students.length; i++) {
Student s = students[i];
output += "{\n";
output += "\tname: " + s.getName() + ",\n";
output += "\tgpa: " + s.getGpa() + ",\n";
output += "\tyear: " + s.getYear() + "\n";
output += "},\n" + (i < students.length - 1 ? "" : "");
}
output += "]";
return output;
}
}