Question: Download the ArrayList-based sorting students program from version 3 and add the following features to it: Rewrite the selection sort for arrays to sort the

Download the ArrayList-based sorting students program from version 3 and add the following features to it:

Rewrite the selection sort for arrays to sort the students in an ArrayList by name. This new sortByName method will be an instance method, and it should be private. Sort the array list's contents directly yourself, do not copy the items to a regular array or call any built-in Java methods.

Call your sort method from a printByName instance method that makes a copy of the ArrayList by calling its clone() method and prints it. Note: to make clone() work, you need to cast the result to the correct type; call it as follows: (ArrayList)list.clone(). The Java compiler may give you a warning, which is not the same as an error. Your program should still run, and give correct output.

Submit the complete revised StudentList class by the due date specified in the course schedule.

import java.util.*;

public class Activity7C {

public static void main(String[] args) {

StudentList students = new StudentList();

students.add(new Student(8032, "Casper", 2.78));

students.add(new Student(3044, "Sheena", 3.92));

students.add(new Student(6170, "Yolanda", 4.26));

students.add(new Student(1755, "Geordi", 3.58));

students.printByNumber();

System.out.println(" End of processing.");

}

}

class Student {

private int number;

private String name;

private double gpa;

public Student(int number, String name, double gpa) {

this.number = number;

this.name = name;

this.gpa = gpa;

}

public int getNumber() {

return number;

}

public double getGPA() {

return gpa;

}

public boolean nameComesBefore(Student other) {

return name.compareToIgnoreCase(other.name) < 0;

}

public String toString() {

return number + " " + name + " (" + gpa + ")";

}

}

class StudentList {

private ArrayList list;

public StudentList() {

list = new ArrayList();

}

public void add(Student student) {

boolean done = false;

int pos;

// find the insertion point (this is just a linear search)

pos = list.size() - 1;

while (pos >= 0 && !done) {

if (student.getNumber() > list.get(pos).getNumber()) {

done = true;

} else {

pos--;

}

}

list.add(pos + 1, student);

}

public void printByNumber() {

System.out.println(" Students ordered by number:");

printStudents(list);

}

private void printStudents(ArrayList list) {

System.out.println(" List of all students: ");

for (int i = 0; i < list.size(); i++) {

System.out.println(i + 1 + ": " + list.get(i));

}

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!