Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions 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
Check Mark
Knowledge Booster
Background pattern image
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
    SEE MORE QUESTIONS
    Recommended textbooks for you
    Text book image
    Database System Concepts
    Computer Science
    ISBN:9780078022159
    Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
    Publisher:McGraw-Hill Education
    Text book image
    Starting Out with Python (4th Edition)
    Computer Science
    ISBN:9780134444321
    Author:Tony Gaddis
    Publisher:PEARSON
    Text book image
    Digital Fundamentals (11th Edition)
    Computer Science
    ISBN:9780132737968
    Author:Thomas L. Floyd
    Publisher:PEARSON
    Text book image
    C How to Program (8th Edition)
    Computer Science
    ISBN:9780133976892
    Author:Paul J. Deitel, Harvey Deitel
    Publisher:PEARSON
    Text book image
    Database Systems: Design, Implementation, & Manag...
    Computer Science
    ISBN:9781337627900
    Author:Carlos Coronel, Steven Morris
    Publisher:Cengage Learning
    Text book image
    Programmable Logic Controllers
    Computer Science
    ISBN:9780073373843
    Author:Frank D. Petruzella
    Publisher:McGraw-Hill Education