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

public class Student { private String lastName; private double gpa; public Stude

ID: 3874783 • Letter: P

Question

public class Student {
private String lastName;
private double gpa;

public Student(String lName, double gpa) {
this.gpa = gpa;
lastName = lName;
}

public Student(Student other) {
this(other.lastName, other.gpa);
}
public double getGPA()
{ return gpa; }
public String getName()
{ return lastName; }
public String toString() {
return String.format("%10s %5.2f", lastName, gpa);
}
}

public class StudentBody {
public ArrayList students;
public StudentBody()
{
students = new ArrayList();
}
public void addStudent(Student s)
{
students.add(new Student(s));
}
// returns an unsorted list of student names that are on probation (i.e., GPA < 2.0)
public ArrayList warningList()
{
// FILL THIS IN

}
// Returns a list of students, sorted by GPA. If two students have equivalent GPAs
// the shorter name should go first. Do not change the order in the data member students.
public ArrayList summary()
{
// FILL THIS IN

}
}

Explanation / Answer

public class Student {
private String lastName;
private double gpa;
public Student(String lName, double gpa) {
this.gpa = gpa;
lastName = lName;
}
public Student(Student other) {
this(other.lastName, other.gpa);
}
public double getGPA()
{ return gpa; }

public void setGPA(double a)
{ gpa = a; }

public String getName()
{ return lastName; }

public void setName(String nm)
{ lastName = nm; }

public String toString() {
return String.format("%10s %5.2f", lastName, gpa);
}
}
public class StudentBody {
public ArrayList<Student> students;
public StudentBody()
{
students = new ArrayList<Student>();
}
public void addStudent(Student s)
{
students.add(new Student(s));
}
// returns an unsorted list of student names that are on probation (i.e., GPA < 2.0)
public ArrayList<Student> warningList()
{
    ArrayList<Student> list = new ArrayList<Student>();
  
    String name;
    double gpa;
    for (int i = 0; i<students.size(); i++){
        if (students.get(i).getGPA() < 2.0){
            list.add(new Student(students.get(i)));
        }
    }
   
    return list;
}
// Returns a list of students, sorted by GPA. If two students have equivalent GPAs
// the shorter name should go first. Do not change the order in the data member students.
public ArrayList<Student> summary()
{

    ArrayList<Student> list = new ArrayList<Student>();
   
    String name;
    double gpa;
    for (int i = 0; i<students.size(); i++){
        list.add(new Student(students.get(i)));
    }
    for (int i = 0; i<list.size(); i++){
        for (int j = i; j<list.size(); j++){
            if (list.get(i).getGPA() > list.get(j).getGPA()){
                name = list.get(i).getName();
                gpa = list.get(i).getGPA();
                list.get(i).setName(list.get(j).getName());
                list.get(i).setGPA(list.get(j).getGPA());
                list.get(j).setName(name);
                list.get(j).setGPA(gpa);
            }
            if (list.get(i).getGPA() == list.get(j).getGPA()){
                if (list.get(i).getName().length() > list.get(j).getName().length()){
                  name = list.get(i).getName();
                  gpa = list.get(i).getGPA();
                  list.get(i).setName(list.get(j).getName());
                  list.get(i).setGPA(list.get(j).getGPA());
                  list.get(j).setName(name);
                  list.get(j).setGPA(gpa);
                 }
            }
        }
    }
   
    return list;
}
}