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); } }
Is there a way to initilize the names for students and their respective addresses? I am trying to get the Java
[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);
}
}
Step by step
Solved in 6 steps with 3 images
Is it possible to sort the students' names in ascending order using the sorted() method and Sortbyname comparator? What would that look like?