In: Computer Science
A Lecturer wishes to create a program that lists his students sorted by the number of theory assignment marks they have completed. The listing should be greatest number of assignment first, sub-sorted by name in lexicographical order (A to Z). A class Student stores the name and marks of assignment completed for a student. [5 marks]
Note: Assume the variable name of collection is list.
i. Provide a definition of Student with an equals() method. The comparison is based on name of the student. [3 marks]
ii. Write java statement to declare a collection variable list of type Student [2 marks]
iii. Implements interface Comparator such that fulfil a natural ordering that matches the given requirement. Define method compareTo() such that it compares based on greatest number of assignment first, sub-sorted by name in lexicographical order (A to Z)). [5 marks]
iv. Provide definition of find(String) method which returns the index if given name exists in collection. [2 marks]
v. Provide a static method in application class to display all students by calling toString method. Assume the name of object of collection is list. [4 marks]
Application class:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class Application {
public static void main(String[] args) {
List<Student> list = new
ArrayList<Student>();
list.add(new
Student("sai",90));
list.add(new
Student("kumar",85));
list.add(new
Student("abhilash",74));
list.add(new
Student("venkat",68));
list.add(new
Student("jay",90));
list.add(new
Student("alex",60));
list.add(new
Student("jonas",90));
list.add(new
Student("magnus",57));
Collections.sort(list);
Iterator iterator =
list.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
Student class:
import java.util.Comparator;
public class Student implements Comparable<Student>
{
private String name;
private int marks;
public Student(String name, int marks) {
super();
this.name = name;
this.marks = marks;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return
true;
if (obj == null)
return
false;
if (getClass() !=
obj.getClass())
return
false;
Student other = (Student)
obj;
if (marks != other.marks)
return
false;
if (name == null) {
if (other.name
!= null)
return false;
} else if
(!name.equals(other.name))
return
false;
return true;
}
@Override
public int compareTo(Student s2) {
if (this.marks < s2.marks)
return 1;
else if
(this.marks > s2.marks)
return -1;
else
{
if((this.name).compareToIgnoreCase(s2.name)<0)
return -1;
else
if((this.name).compareToIgnoreCase(s2.name)>0)
return 1;
else
return 0;
}
}
@Override
public String toString() {
return "Student [name=" + name + ", marks=" + marks +
"]";
}
}
output:
1.public class Student {
String name;
int marks;
@Override
public boolean equals(Object obj) {
if (this == obj)
return
true;
if (obj == null)
return
false;
if (getClass() !=
obj.getClass())
return
false;
Student other = (Student)
obj;
if (name == null) {
if (other.name
!= null)
return false;
} else if
(!name.equals(other.name))
return
false;
return true;
}
}
2.List<Student> list = new ArrayList<Student>();
3.class StudentComparator implements
Comparator<Student>{
public int compare(Student s1, Student s2) {
if (s1.marks < s2.marks)
return 1;
else if (s1.marks > s2.marks)
return -1;
else
{
if((s1.name).compareToIgnoreCase(s2.name)<0))
return -1;
else
if((s1.name).compareToIgnoreCase(s2.name)>0))
return 1;
}
return 0;
}
}
4. int position = students.indexof(name)
5.ToString method:
@Override
public String toString() {
return "Student [name=" + name + ",
marks=" + marks + "]";
}
Printing all students:
Collections.sort(list);
Iterator iterator =
list.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
}