Create a Java program that will store 10 student objects in an ArrayList, ArrayList. A student object consists of the following fields: int rollno String name String address Implement two comparator classes to sort student objects by name and by rollno (roll number). Implement your own selection sort method. *Does the program below meet the above requirements? Can the program be made anymore effecient? Is there any improvements that could be made? (modularity, organization, etc..) If so what would they be/look like? I could also use some help with comments to explain the programs functionality.* Please and thank you! Source code: import java.util.ArrayList; import java.util.Comparator; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextArea; class Student { int rollno; String name; String address; public Student(int roll, String name, String add) { super(); this.rollno = roll; this.name = name; this.address = add; } @Override public String toString() { return "Student [rollno=" + rollno + ", name=" + name + ", address=" + address + "]"; } } class Sortbyroll implements Comparator { public int compare(Student x, Student y) { return x.rollno - y.rollno; } } class Sortbyname implements Comparator { public int compare(Student x, Student y) { return x.name.compareTo(y.name); } } public class Main { public static void sorted(ArrayList stu) { Sortbyroll roll = new Sortbyroll(); int n = stu.size(); for(int i = 0; i < n-1; i++) { int minid = i; for (int j = i+1; j < n; j++) if (roll.compare(stu.get(j), stu.get(minid)) < 0) minid = j; Student t = stu.get(minid); stu.set(minid, stu.get(i)); stu.set(i, t); } } public static void main(String[] args) { ArrayList stu = new ArrayList<>(); String[] names = {"Allan", "Chris", "Fedrick", "Diana", "Eliza", "George", "Hannah", "Ian", "Julia", "Kevin"}; String[] addresses = {"620 pioneer st", "801 cheney dr", "504 plymouth ave", "830 washington st", "200 main st", "910 oak st", "300 maple ave", "700 ash st", "100 pine st", "500 elm st"}; for (int i = 0; i < 10; i++) { int roll = i+1; String name = names[i]; String address = addresses[i]; stu.add(new Student(roll, name, address)); } sorted(stu); // Create the GUI JFrame frame = new JFrame("Sorted Students"); JPanel panel = new JPanel(); JTextArea textArea = new JTextArea(15, 30); // 15 rows, 30 columns // Append the sorted student information to the text area StringBuilder sb = new StringBuilder(); for (Student s : stu) { sb.append(s).append("\n"); } textArea.setText(sb.toString()); // Add the text area to the panel and the panel to the frame panel.add(textArea); frame.add(panel); // Set frame properties and display it frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Create a Java program that will store 10 student objects in an ArrayList, ArrayList<Student>. A student object consists of the following fields:

  • int rollno
  • String name
  • String address

Implement two comparator classes to sort student objects by name and by rollno (roll number). Implement your own selection sort method.

*Does the program below meet the above requirements? Can the program be made anymore effecient? Is there any improvements that could be made? (modularity, organization, etc..) If so what would they be/look like? I could also use some help with comments to explain the programs functionality.* 

Please and thank you!

Source code:

import java.util.ArrayList;
import java.util.Comparator;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;

class Student {
    int rollno;
    String name;
    String address;
    public Student(int roll, String name, String add)
    {
        super();
        this.rollno = roll;
        this.name = name;
        this.address = add;
    }
    @Override
    public String toString() 
    {
       return "Student [rollno=" + rollno + ", name=" + name + ", address=" + address + "]";

    }
}

class Sortbyroll implements Comparator<Student> {
    public int compare(Student x, Student y)
    {
        return x.rollno - y.rollno;
    }
}

class Sortbyname implements Comparator<Student> {
    public int compare(Student x, Student y)
    {
    return x.name.compareTo(y.name);
    }
}

public class Main {
    public static void sorted(ArrayList<Student> stu)
    {
        Sortbyroll roll = new Sortbyroll();
        int n = stu.size();
        for(int i = 0; i < n-1; i++)
        {
            int minid = i;
            for (int j = i+1; j < n; j++)
            if (roll.compare(stu.get(j), stu.get(minid)) < 0)
            minid = j;
            Student t = stu.get(minid);
            stu.set(minid, stu.get(i));
            stu.set(i, t);
        }
    }

    public static void main(String[] args) {
        ArrayList<Student> stu = new ArrayList<>();
        String[] names = {"Allan", "Chris", "Fedrick", "Diana", "Eliza", "George", "Hannah", "Ian", "Julia", "Kevin"};
        String[] addresses = {"620 pioneer st", "801 cheney dr", "504 plymouth ave", "830 washington st", "200 main st", "910 oak st", "300 maple ave", "700 ash st", "100 pine st", "500 elm st"};

        for (int i = 0; i < 10; i++) {
            int roll = i+1;
            String name = names[i];
            String address = addresses[i];
            stu.add(new Student(roll, name, address));
        }

        sorted(stu);

        // Create the GUI
        JFrame frame = new JFrame("Sorted Students");
        JPanel panel = new JPanel();
        JTextArea textArea = new JTextArea(15, 30); // 15 rows, 30 columns

        // Append the sorted student information to the text area
        StringBuilder sb = new StringBuilder();
        for (Student s : stu) {
            sb.append(s).append("\n");
        }
        textArea.setText(sb.toString());

        // Add the text area to the panel and the panel to the frame
        panel.add(textArea);
        frame.add(panel);

        // Set frame properties and display it
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 4 images

Blurred answer
Knowledge Booster
Arrays
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education