Program description:
The purpose of this assignment is to give you experience in both Java, as well as in the design of algorithms for sorting different kinds of data. The system you will construct will consist of a window that contains a title, a table with two columns, and two buttons, as shown here. The first column of the table will contain the names of people and the second column will contain their corresponding ages. The table will initially be empty and a user will be able to type values into it. One button will cause the entries in the table to be sorted based on the name in column 1 and the second button will cause the entries to be sorted based on the ages in column 2.
my code:
//Class that only contains name and age variables
public class Person {
String Name;
int Age;
public Person(String name, int age) {
this.Name = name;
this.Age = age;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
/////////////////////////////////////////////////////////////
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class List extends JFrame implements ActionListener {
private JButton btnName;
private JButton btnAge;
private JPanel roster, list, button;
private JTextField[] name,age;
private Person[] people;
public List(){
super("Roster");
setupComps();
setupPanels();
setupFrame();
}
private void setupComps(){
btnName = new JButton("Sort by Name");
btnName.addActionListener(this);
btnAge = new JButton("Sort by Age");
btnAge.addActionListener(this);
name = new JTextField[6];
age = new JTextField[6];
for(int i = 0; i<6; i++){
name[i] = new JTextField(12);
age[i] = new JTextField(5);
}
}
private void setupPanels(){
roster = new JPanel(new FlowLayout());
roster.add(new JLabel("ROSTER"));
list = new JPanel(new GridLayout(7,2));
list.add(new JLabel("Name"));
list.add(new JLabel("Age"));
for(int i = 0; i < 6; i++){
list.add(name[i]);
list.add(age[i]);
}
button = new JPanel(new GridLayout(1,2));
button.add(btnName);
button.add(btnAge);
}
private void setupFrame(){
setSize(800,600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
setVisible(true);
add(roster);
add(list);
add(button);
}
private void getData(){
for(int i = 0; i < 6; i++){
people[i] = new Person(name[i].getText(), Integer.parseInt(age[i].getText()));
}
}
private void setData(){
for(int i = 0; i < 6; i++){
name[i].setText(people[i].Name);
age[i].setText(Integer.toString(people[i].Age));
}
}
private void sortAge(){
boolean swapped = true;
int j = 0;
Person tmp;
while(swapped){
swapped = false;
j++;
for(int i = 0; i < people.length - j; i++){
if(people[i].Age > people[i+1].Age){
tmp = people[i];
people[i+1] = tmp;
swapped = true;
}
}
}
}
private void sortName(){
boolean swapped = true;
int j = 0;
Person tmp;
while(swapped){
swapped = false;
j++;
for(int i = 0; i < people.length - j; i++){
if(people[i].Name.compareTo(people[i+1].Name) > 0){
tmp = people[i];
people[i+1] = tmp;
swapped = true;
}
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
List names = new List();
}
//clicked button Event
@Override
public void actionPerformed(ActionEvent e) {
getData();
String Command = e.getActionCommand();
if(Command.contains("Name")){
sortName();
} else if(Command.contains("Age")){
sortAge();
}
setData();
}
}