Question

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

Homework Answers

Answer #1

I have written the required method along with class definition

Student.java

class Student {
    String firstName, lastName;
    double gpa;

    public Student(String fn, String ln, double g) {
        this.firstName = fn;
        this.lastName = ln;
        this.gpa = g;
    }

    /**
     * function to sort the student by the last name
     * @param students
     */
    public static void sortByLastName(Student[] students) {
        for (int i = 0; i < students.length; i++) {
            // index for selecting entity
            int index = i;
            for (int j = i + 1; j < students.length; j++) {
                // comparing based on last name
                if (students[j].lastName.compareTo(students[index].lastName) < 1) {
                    index = j;
                }
            }
            // swapping the values
            Student temp = students[index];
            students[index] = students[i];
            students[i] = temp;
        }
    }

    @Override
    public String toString() {
        return String.format("%s, %s,  %.2f\n", this.firstName, this.lastName, this.gpa);
    }

    public static void main(String[] args) {
        Student[] students = new Student[8];
        students[0] = new Student("Saoud", "Mohamed", 3.61);
        students[1] = new Student("Abdelkader", "Farouk", 2.83);
        students[2] = new Student("Beshr", "Alsharqawy", 1.99);
        students[3] = new Student("Nader", "Salah", 3.02);
        students[4] = new Student("Basem", "Hawary", 2.65);
        students[5] = new Student("Abdullah", "Babaker", 2.88);
        students[6] = new Student("Abdelaal", "Khairy", 3.13);
        students[7] = new Student("Mohamedain", "Marsily", 4.00);
        // calling method for sorting the students
        sortByLastName(students);
        // printing the students
        for (Student s : students) {
            System.out.println(s);
        }
    }
}

// OUT

Please do let me know if u have any concern...

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
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 (bubble 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",...
Java : Modify the selection sort algorithm to sort an array of integers in descending order....
Java : Modify the selection sort algorithm to sort an array of integers in descending order. describe how the skills you have gained could be applied in the field. Please don't use an already answered solution from chegg. I've unfortunately had that happen at many occasion ....... ........ sec01/SelectionSortDemo.java import java.util.Arrays; /** This program demonstrates the selection sort algorithm by sorting an array that is filled with random numbers. */ public class SelectionSortDemo { public static void main(String[] args) {...
Assignment #4 – Student Ranking : In this assignment you are going to write a program...
Assignment #4 – Student Ranking : In this assignment you are going to write a program that ask user number of students in a class and their names. Number of students are limited to 100 maximum. Then, it will ask for 3 test scores of each student. The program will calculate the average of test scores for each student and display with their names. Then, it will sort the averages in descending order and display the sorted list with students’...
Using JAVA For this assignment, you will analyze code that uses a file input stream and...
Using JAVA For this assignment, you will analyze code that uses a file input stream and a file output stream. Read through the linked Java™ code. In a Microsoft® Word document, answer the following questions: Could this program be run as is? If not, what is it lacking? Does this program modify the contents of an input stream? In what way? What are the results of running this code? ********************************************** CODE TO ANALYZE  ******************************************************** /********************************************************************** *   Program:   Datasort *   Purpose:   ...
Objective: Write a Java program that will use a JComboBox from which the user will select...
Objective: Write a Java program that will use a JComboBox from which the user will select to convert a temperature from either Celsius to Fahrenheit, or Fahrenheit to Celsius. The user will enter a temperature in a text field from which the conversion calculation will be made. The converted temperature will be displayed in an uneditable text field with an appropriate label. Specifications Structure your file name and class name on the following pattern: The first three letters of your...
write a program that automates the process of generating the final student report for DC faculty...
write a program that automates the process of generating the final student report for DC faculty considering the following restrictions. Consider declaring three arrays for processing the student data: studID studName studGrade The student ID is a random number generated by the 5-digit system in the range of (10000 - 99999). Create a function to assign the student ID generated in an array of numbers. Consider the following to generate the random number: Add the libraries: #include <stdlib.h> #include <ctime>...
IN JAVA Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort: <-- (I need the...
IN JAVA Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort: <-- (I need the code to be written with these) I need Class river, Class CTRiver and Class Driver with comments so I can learn and better understand the code I also need a UML Diagram HELP Please! Class River describes river’s name and its length in miles. It provides accessor methods (getters) for both variables, toString() method that returns String representation of the river, and method isLong()...