Is there a way to initilize the names for students and their respective addresses? I am trying to get the Java program below to display the rollcall number with an actual name and correlating address. It would also be helpful if the names were in ascending order like the roll call numbers are. For example the programs output would look like: [rollno=1, name=Allan, address=620 pioneer st] [rollno=2, name=Chris, address=801 cheney dr] [rollno=3, name=Fedrick, address=504 plymouth ave] etc... What would that look like?  Please and thank you!   Source Code: import java.util.ArrayList; import java.util.Comparator; import java.util.Random; 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) {         Random r = new Random();         ArrayList stu = new ArrayList<>();         for (int i = 0; i < 10; i++) {             int roll = r.nextInt(10);             String name = "Name" + i;             String address = "Address" + 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

Is there a way to initilize the names for students and their respective addresses? I am trying to get the Java program below to display the rollcall number with an actual name and correlating address. It would also be helpful if the names were in ascending order like the roll call numbers are. For example the programs output would look like:

[rollno=1, name=Allan, address=620 pioneer st]

[rollno=2, name=Chris, address=801 cheney dr]

[rollno=3, name=Fedrick, address=504 plymouth ave]

etc...

What would that look like? 

Please and thank you!

 

Source Code:

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Random;
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) {
        Random r = new Random();
        ArrayList<Student> stu = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            int roll = r.nextInt(10);
            String name = "Name" + i;
            String address = "Address" + 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
steps

Step by step

Solved in 6 steps with 3 images

Blurred answer
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

Is it possible to sort the students' names in ascending order using the sorted() method and Sortbyname comparator? What would that look like? 

Solution
Bartleby Expert
SEE SOLUTION
Knowledge Booster
Random Class and its operations
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