Write a java program that is about sorting students from a file based on the sch
ID: 3760007 • Letter: W
Question
Write a java program that is about sorting students from a file based on the school they are attending and students attending the same school sorted by ID in ascending order. Here is the parts to help complete the methods,
Part 1:
You plan to use the Java library stable sort to do the sorting. Since the sort is stable, you can do
two consecutive sorts and end up with the needed result (note that which sort you do first matters).
To sort a list by some criteria in Java 7, you use the Collections.sort() method like below. The
second argument is an implementation of the Comparator interface, which determines how two
elements are compared.
Collections.sort(myList, myComparator);
Part 2:
It occurs to you that you don't need two separate sorts. You plan to write a single comparator
that sorts by school and breaks ties by ID and use that to sort only once to get the desired result.
Part 3:
Both parts 1 and 2 were simple and fast enough, but you have a hunch you could probably
make this a tiny bit faster. You decide to implement your own simple algorithm for the problem based
on bucket sort. The idea is very simple:
– You create one bucket (a list) for each school.
– You do one pass through the input list and place each student you encounter into its
corresponding bucket.
– You clear (make empty) the original input list since you now have all the students in the
different buckets.
– You sort each of the buckets (by which field?) by using a comparator like in Part 1.
– You go through each bucket in order and put the students one by one into the original list.
Hints:
Make sure you are comfortable using the ArrayList class(we have seen most methods you will
use in the lists lecture) and doing a simple sort using a comparator. You may want to try that with a list
of strings first, just for some quick practice. API docs are here:
http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
http://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html // do not implement
equals() method
you need to complete these three methods:
sortBySchoolById1
sortBySchoolById2
sortBySchoolById3
Explanation / Answer
part 1 :To use java library stable sort to do the sorting.
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
struct student {
student(int student id, std::string student name) : id(student id), student name(student name) { }
int student id;
std::string student name;
};
bool operator<(const student &lhs, const student &rhs) {
return lhs.student id < rhs.student id;
}
int main()
{
std::vector<student> v = {
student(108, "John"),
student(32, "Arnthur"),
student(106, "Ford"),
};
std::stable_sort(v.begin(), v.end());
for (const student &e : v) {
std::cout << e.student id << ", " << e.student name << ' ';
}
}
Part 2:write a single comparator that sorts by id
if we are using c++ with relational operator
bool compareBystudentSchoolName(Student s1, Student s2) {
return s1.studentschoolName < s2.studentschoolName || (s1.studentschoolName == s2.studentschoolName && s1.ID < s2.ID);
}
part 3: To create a list of student and sort by id.
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class studentArrayListSort {
public static void main(String a[]){
List<student> list = new ArrayList<student>();
list.add(new student("John",108));
list.add(new student("Arnthur",32));
list.add(new student("Ford",106));
Collections.sort(list,new studentidComp());
System.out.println("Sorted list entries: ");
for(student s:list){
System.out.println(s);
}
}
}
class studentidComp implements Comparator<student>{
@Override
public int compare(student s1, student s2) {
if(s1.getId() < s2.getId()){
return 1;
} else {
return -1;
}
}
}
class student{
private String name;
private int id;
public student(String n, int s){
this.name = n;
this.id = s;
}
public String getName() { // get student name
return name;
}
public void setName(String name) { // set the value for students.
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String toString(){
return "Name: "+this.name+"-- id: "+this.id;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.