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 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...
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...
java 2D array / recursion explain every method You are requested to write a Java program...
java 2D array / recursion explain every method You are requested to write a Java program of a simple Memory Management Unit. The program should allow the following: 1. The user can create a process asking for memory. The program will return a process ID if the requested memory can be allocated. It will also print the allocated Base and Limit. 2. The user can delete a process by specifying a process ID. The program should do that and free...
Write C program that reorders elements of an array of integers such that the new order...
Write C program that reorders elements of an array of integers such that the new order is in descending order (first number being the largest). Must have a main function and a swap function. - int main() will declare an array with the values { 32, 110, 79, 18, 22, 2}. This array will be passed to the swap function. - the void swap function will perform the necessary operations to reorder the elements of the array. - After swap()...
Write a Java program that will use a two-dimensional array and modularity to solve the following...
Write a Java program that will use a two-dimensional array and modularity to solve the following tasks: Create a method to fill the 2-dimensional array with (random numbers, range 0 - 30). The array has rows (ROW) and columns (COL), where ROW and COL are class constants. Create a method to print the array. Create a method to find the largest element in the array Create a method to find the smallest element in the array Create a method to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT