Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Project #1 - Interval Scheduling
- * AUTHOR: Uyen Nguyen
- * CLASS: COMP496ALG
- * PROFESSOR: John Noga
- * DATE: 04/24/2015
- */
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.util.Arrays;
- import java.util.Comparator;
- public class IntervalScheduling {
- public static void main(String args[]){
- IntervalScheduling ie = new IntervalScheduling();
- ie.run();
- }
- public void run() {
- int[][] jobs;
- System.out.println("Project 1: Machine Scheduling with Deadlines");
- System.out.println("Reading jobs.txt");
- jobs = getJobs();
- // Sort by the 2nd column
- Arrays.sort(jobs, new Comparator<int[]>(){
- @Override
- public int compare(int[] n1, int[] n2) {
- return Integer.compare(n1[1], n2[1]);
- }
- });
- System.out.println("The best order to schedule the jobs is:");
- for (int i = 0; i < jobs.length; i++) {
- System.out.println("(" + jobs[i][0] + ", " + jobs[i][1] + ")");
- }
- int time = 0;
- int lateness = 0;
- for (int i = 0; i < jobs.length; i++) {
- time += jobs[i][0];
- if (time > jobs[i][1]) lateness += time - jobs[i][1];
- }
- System.out.println("It has a total lateness of " + lateness + ".");
- }
- private int[][] getJobs() {
- int[][] jobs = null;
- BufferedReader br;
- BufferedReader c;
- try {
- String line = "";
- int numJobs = 0;
- // Count jobs
- c = new BufferedReader(new FileReader("src/jobs.txt"));
- while ((line = c.readLine()) != null) {
- line = line.trim();
- if (line.length() != 0) numJobs++;
- }
- System.out.println("Number of jobs: " + numJobs);
- c.close();
- // Allocate array based on number of jobs found in .txt file
- jobs = new int[numJobs][2];
- // Parse jobs into jobs array
- br = new BufferedReader(new FileReader("src/jobs.txt"));
- line = "";
- int counter = 0;
- while((line = br.readLine()) != null && counter < numJobs) {
- line = line.trim();
- if (line.length() != 0) {
- String [] str = line.split(" ");
- if (isInt(str[0]) && isInt(str[1])) {
- jobs[counter][0] = Integer.parseInt(str[0]);
- jobs[counter][1] = Integer.parseInt(str[1]);
- counter++;
- }
- }
- }
- br.close();
- } catch(Exception e) {System.out.println("Failed to open file.");}
- return jobs;
- }
- private boolean isInt(String s) {
- try {
- Integer.parseInt(s);
- return true;
- } catch(Exception e) {}
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement