In Java Create an array of Student objects Display the stude
In Java Create an array of Student objects.
Display the students by age in UNSORTED order.
Apply the Quick sort and Display the students by
age in SORTED order.
public class TestQuickStudent {
public static void main (String [ ] args ) {
/*
***Create your array of Student objects with AT LEAST 5 names
*/
System.out.println ( \" ____________________________\");
/*
***LOOP through your array and print out each UNSORTED check by student age
*/
System.out.println ( \" ____________________________\");
Quick.sort (students);
/*
***LOOP through your array and print out each SORTED check by student age
*/
System.out.println ( \" ____________________________\");
}
}
-Have to create a class Student. The properties/instance variables will be as
follows:
Integer age;
String state;
String firstName;
String lastName;
Solution
Hi, Please find my implementation.
Since you have not posted Quick.java, so i have not tested program.
######## Student.java ##########
public class Student implements Comparable<Student> {
// instance variable
private Integer age;
private String state;
private String firstName;
private String lastName;
// constructor
public Student(Integer age, String state, String firstName, String lastName) {
this.age = age;
this.state = state;
this.firstName = firstName;
this.lastName = lastName;
}
// comparing based on age
@Override
public int compareTo(Student o) {
return age.compareTo(o.age);
}
// setters and getters
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
################## TestQuickStudent.java #############
public class TestQuickStudent {
public static void main (String [ ] args ) {
/*
***Create your array of Student objects with AT LEAST 5 names
*/
// creating Student array
Student []students = {
new Student(24, \"UK\", \"Alex\", \"Gender\"),
new Student(22, \"USA\", \"Pravesh\", \"Kumar\"),
new Student(20, \"BLR\", \"Mukesh\", \"Mittal\"),
new Student(23, \"IND\", \"Tinu\", \"Dabi\"),
new Student(25, \"Germani\", \"Vishal\", \"Gaana\")
};
System.out.println ( \" ____________________________\");
/*
***LOOP through your array and print out each UNSORTED check by student age
*/
for(int i=0; i<students.length; i++){
System.out.println(students[i].getAge());
}
System.out.println ( \" ____________________________\");
Quick.sort(students);
/*
***LOOP through your array and print out each SORTED check by student age
*/
for(int i=0; i<students.length; i++){
System.out.println(students[i].getAge());
}
System.out.println ( \" ____________________________\");
}
}



