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

For the lab this week, you will sort an array of objects - using any of the sort

ID: 3912513 • Letter: F

Question

For the lab this week, you will sort an array of objects - using any of the sort methods discussed in Chapter 23 or the Selection sort. It's your choice. Use the following criteria for your assignment: The object class should be a Student with the following attributes: id: integer name: String write the accessors, mutators, constructor, and toString(). In your main test class you will write your main method and do the following things: Create an array of Student objects with at least 5 students in your array. The sort method must be written yourself and included in the main class. The sort method will sort based on student id. Output the array out in unsorted order as it exists. Sort the array Output the sorted array Your code should represent your own work and should not be copied from a site. You will be required to complete a sort of an array (or two) in your final lab - making this lab very important to get correct. Make sure you write all the methods listed above for full credit.

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

// Student.java

public class Student {

                // attributes

                private int id;

                private String name;

                /**

                * constructor to initialize student with id and name

                */

                public Student(int id, String name) {

                                this.id = id;

                                this.name = name;

                }

                /**

                * default constructor to initialize student with default id and name

                */

                public Student() {

                                id = 0;

                                name = "";

                }

               

                //getters and setters

               

                public int getId() {

                                return id;

                }

                public void setId(int id) {

                                this.id = id;

                }

                public String getName() {

                                return name;

                }

                public void setName(String name) {

                                this.name = name;

                }

                /**

                * returns a string representation of student id and name

                */

                @Override

                public String toString() {

                                return "[ ID: " + id + ", Name: " + name + " ]";

                }

}

//Test.java

public class Test {

                /**

                * method to sort an array of students by id

                *

                * @param students

                *            - array of students to be sorted

                */

                static void sort(Student students[]) {

                                /**

                                * Applying selection sort algorithm to sort students by id in ascending

                                * order

                                */

                                for (int i = 0; i < students.length - 1; i++) {

                                                int index = i;

                                                for (int j = i + 1; j < students.length; j++) {

                                                                if (students[j].getId() < students[index].getId()) {

                                                                                index = j;// finding the student with smallest id

                                                                }

                                                }

                                                /*

                                                * Swapping the students at position i and position 'index'

                                                */

                                                Student temp = students[index];

                                                students[index] = students[i];

                                                students[i] = temp;

                                }

                }

                /**

                * method to print students array

                *

                * @param students

                *            -array of students

                */

                static void print(Student students[]) {

                                for (int i = 0; i < students.length; i++) {

                                                System.out.println(students[i]);

                                }

                }

                public static void main(String[] args) {

                                /**

                                * Defining an array of students

                                */

                                Student studentArray[] = new Student[5];

                                /**

                                * filling the array with some students

                                */

                                studentArray[0] = new Student(99, "Oliver");

                                studentArray[1] = new Student(123, "Alice");

                                studentArray[2] = new Student(22, "Felicity");

                                studentArray[3] = new Student(111, "John");

                                studentArray[4] = new Student(100, "Barry");

                                /**

                                * displaying the students before sorting

                                */

                                System.out.println("Before sorting:");

                                print(studentArray);

                                /**

                                * sorting the students by id

                                */

                                sort(studentArray);

                                /**

                                * displaying the students after sorting

                                */

                                System.out.println(" After sorting:");

                                print(studentArray);

                }

}

/*OUTPUT */

Before sorting:

[ ID: 99, Name: Oliver ]

[ ID: 123, Name: Alice ]

[ ID: 22, Name: Felicity ]

[ ID: 111, Name: John ]

[ ID: 100, Name: Barry ]

After sorting:

[ ID: 22, Name: Felicity ]

[ ID: 99, Name: Oliver ]

[ ID: 100, Name: Barry ]

[ ID: 111, Name: John ]

[ ID: 123, Name: Alice ]