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); } }
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);
}
}

Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 4 images









