Question

In: Computer Science

Write a Java program that sorts an array of “Student” in an aescending order of their...

Write a Java program that sorts an array of “Student” in an aescending order of their “last names”. The program should be able to apply (Insertion sort):

Student [] studs = new Student[8];

s[0] = new Student("Saoud", "Mohamed", 3.61);

s[1] = new Student("Abdelkader", "Farouk", 2.83);

s[2] = new Student("Beshr" , "Alsharqawy", 1.99);

s[3] = new Student("Nader", "Salah", 3.02);

s[4] = new Student("Basem", "Hawary", 2.65);

s[5] = new Student("Abdullah", "Babaker", 2.88);

s[6] = new Student("Abdelaal", "Khairy", 3.13);

s[7] = new Student("Mohamedain", "Marsily", 4.00);

Solutions

Expert Solution

public class StudentSorter {
        
        static class Student {
                
                private String first, last;
                private double gpa;

                public Student(String first, String last, double gpa) {
                        this.first = first;
                        this.last = last;
                        this.gpa = gpa;
                }

                public String getFirst() {
                        return first;
                }

                public String getLast() {
                        return last;
                }

                public double getGpa() {
                        return gpa;
                }

                @Override
                public String toString() {
                        return first + " " + last + ": " + gpa;
                }
        }

        public static void sortStudents(Student arr[]) {

            for (int i = 1; i < arr.length; i++) { 
                Student key = arr[i]; 
                int j = i - 1;
                while (j >= 0 && arr[j].getLast().compareTo(key.getLast()) > 0) {
                        arr[j + 1] = arr[j];
                    j = j - 1;
                }
                arr[j + 1] = key; 
            }
        }
        
        public static void main(String[] args) {

                Student[] s = new Student[8];
                s[0] = new Student("Saoud", "Mohamed", 3.61);
                s[1] = new Student("Abdelkader", "Farouk", 2.83);
                s[2] = new Student("Beshr", "Alsharqawy", 1.99);
                s[3] = new Student("Nader", "Salah", 3.02);
                s[4] = new Student("Basem", "Hawary", 2.65);
                s[5] = new Student("Abdullah", "Babaker", 2.88);
                s[6] = new Student("Abdelaal", "Khairy", 3.13);
                s[7] = new Student("Mohamedain", "Marsily", 4.00);
                
                
                sortStudents(s);
                
                
                for(Student x: s) {
                        System.out.println(x);
                }
        }

}
**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

(JAVA) Write a program that maintains student test scores in a two-dimesnional array, with the students...
(JAVA) Write a program that maintains student test scores in a two-dimesnional array, with the students identified by rows and test scores identified by columns. Ask the user for the number of students and for the number of tests (which will be the size of the two-dimensional array). Populate the array with user input (with numbers in {0, 100} for test scores). Assume that a score >= 60 is a pass for the below. Then, write methods for: Computing the...
Write a java program that randomizes a list and sorts it. The list will be randomized...
Write a java program that randomizes a list and sorts it. The list will be randomized to be at least 12 elements and no more than 30 elements with values between 1 and 100. You will submit one program with two sorts done separately within the program. The list will be re-randomized before reach sort. Write a flowchart for each sort.
Sorting Benchmarks Write a program that generates and sorts an array of a user-specified number (arraySize)...
Sorting Benchmarks Write a program that generates and sorts an array of a user-specified number (arraySize) of randomly generated numbers. To keep the values to a reasonable range by using the array size as the upper bound for the random numbers (between 1 and arraySize). Your program should call the individual functions that implement the five sorting algorithms discussed in class (see the lecture slides). Each function should keep a count of the number of comparisons/exchanges it makes. Display the...
Write a Y86 program in C language that sorts an array of data using Bubble Sort....
Write a Y86 program in C language that sorts an array of data using Bubble Sort. Allow the user to input up to 10 numbers from the keyboard. Sort the array in place (i.e., no need to allocate additional memory for the sorted array). Your program should be a complete one
Write a Java program that reads a list of integers into an array. The program should...
Write a Java program that reads a list of integers into an array. The program should read this array from the file “input.txt”. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is a two-column list. The first column is the list of the distinct array elements; the second column is the number of occurrences of each element. The list should be sorted on entries in...
Write a program that sorts prices of 10 tacos in ascending order based on the price,...
Write a program that sorts prices of 10 tacos in ascending order based on the price, using arrays. Requirements: The user enters the name of the taco and then the price of the taco HINT: Two arrays make this problem simpler. One with the names and the other with the prices. The indices indicate the combination. For instance, a taco price at index 5 has its name also at index 5 of the other array. HINT: It is a good...
Define what an array index value is in JAVA. Then, write a JAVA program that passes...
Define what an array index value is in JAVA. Then, write a JAVA program that passes an array to a method and finds the average value or mean value (add up the numbers in the array and divide by the number of values) of the array and prints it out.
IN JAVA write a program that creates an array of strings with 8 people in it....
IN JAVA write a program that creates an array of strings with 8 people in it. Second,  Assign a random rank between 1 to 8 to each of the players. The rankings do not change throughout the tournament. Finally, Sort the players based on the rankings and print the data (show rankings of players, in square brackets, at every step after they are ranked). USING JAVA COLLECTIONS IS NOT ALLOWED
Write a java program: Write a nested loop program creating an array list of three positions.  The...
Write a java program: Write a nested loop program creating an array list of three positions.  The first loop terminates with a sentinel value of which user is prompted for input to continue of quit.   Inside loop to enter in each position a prompted input of person's first name.  After the inside loop, edit the second array location making its value "Boston". Print the array's contents.
Write a program in Java with a Scanner. Given an array and a number k where...
Write a program in Java with a Scanner. Given an array and a number k where k is smaller than the size of the array, write a program to find the k'th smallest element in the given array. It is given that all array elements are distinct. Example: Input: arr[] = {7,10,4,3,20,15} k = 3 Output: 7
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT