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...
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 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 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
Write a Java program to create an array of a specific size (which is an input...
Write a Java program to create an array of a specific size (which is an input from the user) and fill it with random numbers between 1 and 100. Then sort the array and count how many of these numbers are originally at sorted position. Display that original array, the sorted array, and the count (number of elements originally at sorted position).
Write a program in Java that initializes an array with ten random integers and then print...
Write a program in Java that initializes an array with ten random integers and then print three lines of output, containing: Every element at an odd index Every odd element All elements in reverse order   The program should use three different methods to implement the functionalities above. Call the primary source file ArrayManipulator.java
IN JAVA Write a program with a method that returns an array. The method should accept...
IN JAVA Write a program with a method that returns an array. The method should accept as input a comma-delimited string with three values from a user. The array should store each value in a different element. Use Try..Catch error handling and print any failure messages, or print success from within method if the execution is successful (see Chapter 13 in the text). Call the method from the main method of the program to demonstrate its functionality by looping through...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT