Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am trying to write a method that returns an array containing all elements of s

ID: 3628752 • Letter: I

Question

I am trying to write a method that returns an array containing all elements of students who have a gpa LESS THAN 2.0. This array should not contain any null elements. That is, if there are 2 failing students, the array returned by this method should only contain two elements. Here is the other work that I've done that I assume you need to call in this method.

package People;
public class Student {

String name;
double gpa;

public Student(String n, double g) {
name = n;
gpa = g;
}
public String getName(){
return name;
}
public double getGpa(){
return gpa;
}
public void setName(String n){
name = n;
}
public void setGpa(double g){
gpa = g;
}

public boolean equals(Student s) {
if ((s.name == name) && (s.gpa == gpa)){
System.out.println("Passed!");
return true;
}
else {
System.out.println("Failed!");
return false;
}
}
}
Now, I need to write the fore mentioned method in a different class called Teacher, also in the package People. What I don't know how to do is to a) use the methods in Student that I've already written (which I know I need to do) and b) how to get the gpa's from the array and see if they are above a 2.0

package People;

public class Teacher {
public static Student [] getFailing(Student[] students){


}
}


HELP?! Thanks!

Explanation / Answer

public static Student[] getFailing(Student[] students)
{
// make one pass to count number of failing students
int numFailing = 0;
for(Student x : students)
if(x.getGpa() < 2.0)
numFailing++;
// create array to store failing students
Student[] output = new Student[numFailing];
// on the second pass populate the array
int idx = 0;
for(Student x : students)
if(x.getGpa() < 2.0)
output[idx++] = x;
return output;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote