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

Download the ArrayList-based sorting students program from version 3 and add the

ID: 3913194 • Letter: D

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 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<Student>)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<Student> list;

public StudentList() {

list = new ArrayList<Student>();

}

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<Student> list) {

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

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

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

}

}

}

Explanation / Answer

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks

// Activity7C.java

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();

                                /**

                                * calling the new method to sort and display by name

                                */

                                students.printByName();

                                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<Student> list;

                public StudentList() {

                                list = new ArrayList<Student>();

                }

                /**

                * private method to sort a list of students by name

                * @param list - student list to be sorted

                */

                private void sortByName(ArrayList<Student> list) {

                                /**

                                * using selection sort algorithm, sorting the list by name

                                */

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

                                                int index = i;

                                                /**

                                                * finding index of largest element (by name) in the unsorted part

                                                */

                                                for (int j = i + 1; j < list.size(); j++)

                                                                if (list.get(j).nameComesBefore(list.get(index)))

                                                                                index = j;

                                                /**

                                                * Swapping elements

                                                */

                                                Student temp = list.get(index);

                                                list.set(index, list.get(i));

                                                list.set(i, temp);

                                }

                }

                /**

                * method to sort and display the student list by name

                */

                public void printByName(){

                                /**

                                * taking a copy of the students list

                                */

                                ArrayList<Student> tempList=(ArrayList<Student>)list.clone();

                                /**

                                * calling the method to sort this copy of list

                                */

                                sortByName(tempList);

                                /**

                                * displaying the sorted list

                                */

                                System.out.println(" Students sorted by name:");

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

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

                                }

                }

                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<Student> list) {

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

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

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

                                }

                }

}

/*OUTPUT*/

Students ordered by number:

List of all students:

1: 1755 Geordi (3.58)

2: 3044 Sheena (3.92)

3: 6170 Yolanda (4.26)

4: 8032 Casper (2.78)

Students sorted by name:

1: 8032 Casper (2.78)

2: 1755 Geordi (3.58)

3: 3044 Sheena (3.92)

4: 6170 Yolanda (4.26)

End of processing.

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