Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a java program which is given to you and you need to fill out the empty bo

ID: 3759637 • Letter: W

Question

Write a java program which is given to you and you need to fill out the empty bodied methods. The program 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 with the given code as well:

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:

A lot of the code has been given. All you need to do is fill out the empty bodied methods.

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

****Given code which you need to complete these three methods:

sortBySchoolById1

sortBySchoolById2

sortBySchoolById3


*******************************************Below is the code that needs the methods to be completed*************************

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

public class Project4 {

/**
* Read students from file into a list
*
* @param filename
* @return the list of students
*/
static ArrayList<Student> readFromFile(String filename) {
  ArrayList<Student> students = new ArrayList<Student>();
  try (Scanner in = new Scanner(new File(filename))) {
   while (in.hasNextLine()) {
    String[] fields = in.nextLine().split(",");
    students.add(new Student(Integer.parseInt(fields[1]), fields[0], fields[2]));
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  }
  return students;
}

/**
* Write the students to a file
*
* @param students
* @param filename
*/
static void writeToFile(ArrayList<Student> students, String filename) {
  try (FileWriter out = new FileWriter(filename)) {
   for (Student s : students) {
    out.write(s.toString());
    out.write(" ");
   }
  } catch (IOException e) {
   e.printStackTrace();
  }
}

/**
* Sorts the students list by school. Same school students are sorted by ID.
*
* Performs two stable sorts with different criteria. One sort and then another sort.
*
* @param students
*/
static void sortBySchoolById1(ArrayList<Student> students) {
  // Your code here
  
}

/**
* Sorts the students list by school. Same school students are sorted by ID.
*
* Performs only one sort that uses a comparator that takes both criteria
* into account.
*
* @param students
*/
static void sortBySchoolById2(ArrayList<Student> students) {
  // Your code here
  
}

/**
*      Use this function in your bucket sort
*
* Return the bucket index for each school (schools in alphabetical order)
*
* "UCB", "UCD", "UCI", "UCLA", "UCM", "UCSD", "UCSF"
*   0           1         2         3           4            5           6
*
* @param school
* @return the bucket index
*/
private static int schoolToIndex(String school) {
  switch (school) {
  case "UCB":
   return 0;
  case "UCD":
   return 1;
  case "UCI":
   return 2;
  case "UCLA":
   return 3;
  case "UCM":
   return 4;
  case "UCSD":
   return 5;
  case "UCSF":
   return 6;
  default:
   System.err.println("Unknown school " + school);
   return -1;
  }
}

/**
* Sorts the students list by school. Same school students are sorted by ID.
*
* Places all the students of the same school into an individual bucket and
* sorts each bucket separately.
*
* @param students
*
*/

static void sortBySchoolById3(ArrayList<Student> students) {
  final int NUM_SCHOOLS = 7;
  // an array of lists. Each element is a reference to a list.
  ArrayList<Student>[] buckets = (ArrayList<Student>[]) new ArrayList[NUM_SCHOOLS];
  
  // Your code here
  
}

public static void main(String[] args) {
  ArrayList<Student> students1 = readFromFile("students.txt");
  // make 2 other copies so we don't have to read the file again
  ArrayList<Student> students2 = (ArrayList<Student>) students1.clone();
  ArrayList<Student> students3 = (ArrayList<Student>) students1.clone();

  // let's time the three sorts for fun. Not really that accurate.
  long start, end;
  
  start = System.currentTimeMillis();
  sortBySchoolById1(students1);
  end = System.currentTimeMillis();
  System.out.println("Two stable sorts took " + (end - start)
    + " milliseconds.");

  start = System.currentTimeMillis();
  sortBySchoolById2(students2);
  end = System.currentTimeMillis();
  System.out.println("One stable sort with a two criteria comparator took "
      + (end - start) + " milliseconds.");

  start = System.currentTimeMillis();
  sortBySchoolById3(students3);
  end = System.currentTimeMillis();
  System.out.println("Bucket sort took " + (end - start)
    + " milliseconds.");

  if (!(students1.equals(students2) && students2.equals(students3))) {
   System.out.println("Fix me: One or more sorts are incorrect.");
  }
  
  writeToFile(students3, "studentsSorted.txt");
}
}

/********************************************************Student class**************************************/

public class Student {

private int id;
private String name;
private String school;

public Student (int id, String name, String school) {
  this.id = id;
  this.name = name;
  this.school = school;
}

int getId() {
  return id;
}

String getSchool() {
  return school;
}

@Override
public String toString() {
  return name + "," + id + "," + school;
}
}

Explanation / Answer

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
public class Project4 {
/**
* Read students from file into a list
*
* @param filename
* @return the list of students
*/
static ArrayList<Student> readFromFile(String filename) {
ArrayList<Student> students = new ArrayList<Student>();
try (Scanner in = new Scanner(new File(filename))) {
while (in.hasNextLine()) {
String[] fields = in.nextLine().split(",");
students.add(new Student(Integer.parseInt(fields[1]), fields[0], fields[2]));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return students;
}
/**
* Write the students to a file
*
* @param students
* @param filename
*/
static void writeToFile(ArrayList<Student> students, String filename) {
try (FileWriter out = new FileWriter(filename)) {
for (Student s : students) {
out.write(s.toString());
out.write(" ");
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Sorts the students list by school. Same school students are sorted by ID.
*
* Performs two stable sorts with different criteria. One sort and then another sort.
*
* @param students
*/
static void sortBySchoolById1(ArrayList<Student> students) {
// Your code here
  
}
/**
* Sorts the students list by school. Same school students are sorted by ID.
*
* Performs only one sort that uses a comparator that takes both criteria
* into account.
*
* @param students
*/
static void sortBySchoolById2(ArrayList<Student> students) {
// Your code here
  
}
/**
* Use this function in your bucket sort
*
* Return the bucket index for each school (schools in alphabetical order)
*
* "UCB", "UCD", "UCI", "UCLA", "UCM", "UCSD", "UCSF"
* 0 1 2 3 4 5 6
*
* @param school
* @return the bucket index
*/
private static int schoolToIndex(String school) {
switch (school) {
case "UCB":
return 0;
case "UCD":
return 1;
case "UCI":
return 2;
case "UCLA":
return 3;
case "UCM":
return 4;
case "UCSD":
return 5;
case "UCSF":
return 6;
default:
System.err.println("Unknown school " + school);
return -1;
}
}
/**
* Sorts the students list by school. Same school students are sorted by ID.
*
* Places all the students of the same school into an individual bucket and
* sorts each bucket separately.
*
* @param students
*
*/

static void sortBySchoolById3(ArrayList<Student> students) {
final int NUM_SCHOOLS = 7;
// an array of lists. Each element is a reference to a list.
ArrayList<Student>[] buckets = (ArrayList<Student>[]) new ArrayList[NUM_SCHOOLS];
  
// Your code here
  
}
public static void main(String[] args) {
ArrayList<Student> students1 = readFromFile("students.txt");
// make 2 other copies so we don't have to read the file again
ArrayList<Student> students2 = (ArrayList<Student>) students1.clone();
ArrayList<Student> students3 = (ArrayList<Student>) students1.clone();
// let's time the three sorts for fun. Not really that accurate.
long start, end;
  
start = System.currentTimeMillis();
sortBySchoolById1(students1);
end = System.currentTimeMillis();
System.out.println("Two stable sorts took " + (end - start)
+ " milliseconds.");
start = System.currentTimeMillis();
sortBySchoolById2(students2);
end = System.currentTimeMillis();
System.out.println("One stable sort with a two criteria comparator took "
+ (end - start) + " milliseconds.");
start = System.currentTimeMillis();
sortBySchoolById3(students3);
end = System.currentTimeMillis();
System.out.println("Bucket sort took " + (end - start)
+ " milliseconds.");
if (!(students1.equals(students2) && students2.equals(students3))) {
System.out.println("Fix me: One or more sorts are incorrect.");
}
  
writeToFile(students3, "studentsSorted.txt");
}
}
/********************************************************Student class**************************************/
public class Student {

private int id;
private String name;
private String school;

public Student (int id, String name, String school) {
this.id = id;
this.name = name;
this.school = school;
}
int getId() {
return id;
}

String getSchool() {
return school;
}

@Override
public String toString() {
return name + "," + id + "," + school;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote