Create and test Java code that implements standard Java java.lang.Comparable<T>
ID: 3625719 • Letter: C
Question
Create and test Java code that implements standard Java java.lang.Comparable<T> and java.util.Comparator<T> interfaces using Java generics.Requirements:
1. Create a class Student that has the following 3 fields: name, age, studentId. The
Student class must implement the java.lang.Comparable<Student> interface based
on the studentId field and follow the standard OOP design rules:
a. All data fields are private.
b. The class has two constructors - a default one and a 3-argument one.
c. All data fields have associated public accessor and mutator methods.
d. The class has a toString() method that returns a human readable presentation
of the class attributes (data fields).
e. The compareTo(Student o) method of the Student class should delegate the
comparison processing to the standard String class compareTo(String o)
method based on the studentId field.
2. Code a separate class StudentCompare that implements the java.util.Comparator<Student> interface for the objects of type Student using the
age field as the comparison criterion. The compare(Student o1, Student o2) method
of the StudentCompare class performs comparisons using the age fields of the o1
and o2 instances of the Student class.
3. Provide a separate test class that:
a. Creates and populates an array of type Comparable<Student> with at least 5 different Student instances.
b. Performs sorting of this array using both of the comparison techniques
implemented according to the items 1 and 2 above.
c. Displays both sorted arrays using the toString() method of the Student class.
4. Use the test results from the item 3 above, to formulate in the assignment report
your conclusions regarding the two comparison approaches described.
5. Your code should include javadoc comments for all classes, methods, and data
fields.
6. The project submission should include the HTML documentation for your code
generated by the javadoc utility.
Hints:
1. Use Java 6 API specification http://download.oracle.com/javase/6/docs/api/ for
the information on Java 6.
2. The javadoc utility may be invoked from the command-line or inside NetBeans.
3. Use the appropriate sorting methods from the Java java.util.Arrays class to sort
your array.
4. Use Java coding guidelines when naming your identifiers and creating class fields.
The fields should be private or protected and appropriate accessor and mutator
methods should be provided.
Explanation / Answer
I did not have time to do the javadoc completely, but it will be easy for you to add.
StudentSortTest.java
import java.util.*;
public class StudentSortTest {
/**
* @param args
*/
public static void main(String[] args) {
Student [] students = new Student[] {new Student("Harold",22,"1238"),
new Student("Marge",19,"9990"),
new Student("Maggie",17,"8900"),
new Student("Millie",78,"0001"),
new Student("Artie",77,"0101")
};
Arrays.sort(students);
System.out.println("Students sorted according to their built-in compareTo method");
for (Student s : students) {System.out.println(s);}
Arrays.sort(students,new StudentCompare());
System.out.println();
System.out.println("Students sorted with StudentCompare:");
for (Student s : students) {System.out.println(s);}
}
}
-----------------------------------
StudentCompare.java
import java.util.Comparator;
public class StudentCompare implements Comparator<Student> {
@Override
public int compare(Student arg0, Student arg1) {
return new Integer(arg0.getAge()).compareTo(arg1.getAge());
}
}
---------------------------------
Student.java
public class Student implements Comparable<Student> {
private String name;
private int age;
private String studentId;
/**
* @param name
* @param age
* @param studentId
*/
public Student(String name, int age, String studentId) {
super();
this.name = name;
this.age = age;
this.studentId = studentId;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
/**
* @return the studentId
*/
public String getStudentId() {
return studentId;
}
/**
* @param studentId the studentId to set
*/
public void setStudentId(String studentId) {
this.studentId = studentId;
}
/**
* @param o the student to compare with
*
* @return a negative, positive or zero value according to whether this studentId is less than, greater than or equal to the argument's studentId
*/
public int compareTo(Student o) {
return studentId.compareTo(o.studentId);
}
/**
* @return a String showing the field values
*/
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", studentId="
+ studentId + "]";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.