Advertisement
Guest User

Untitled

a guest
Apr 24th, 2015
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.41 KB | None | 0 0
  1. /* Project #1 - Interval Scheduling
  2.  * AUTHOR: Uyen Nguyen
  3.  * CLASS: COMP496ALG
  4.  * PROFESSOR: John Noga
  5.  * DATE: 04/24/2015
  6.  */
  7.  
  8. import java.io.BufferedReader;
  9. import java.io.FileReader;
  10. import java.util.Arrays;
  11. import java.util.Comparator;
  12.  
  13. public class IntervalScheduling {
  14.    
  15.     public static void main(String args[]){
  16.         IntervalScheduling ie = new IntervalScheduling();
  17.         ie.run();
  18.     }
  19.    
  20.     public void run() {
  21.         int[][] jobs;
  22.  
  23.         System.out.println("Project 1: Machine Scheduling with Deadlines");
  24.         System.out.println("Reading jobs.txt");
  25.         jobs = getJobs();
  26.        
  27.         // Sort by the 2nd column
  28.         Arrays.sort(jobs, new Comparator<int[]>(){
  29.             @Override
  30.             public int compare(int[] n1, int[] n2) {
  31.                 return Integer.compare(n1[1], n2[1]);
  32.             }
  33.         });
  34.    
  35.         System.out.println("The best order to schedule the jobs is:");
  36.         for (int i = 0; i < jobs.length; i++) {
  37.             System.out.println("(" + jobs[i][0] + ", " + jobs[i][1] + ")");
  38.         }
  39.        
  40.         int time = 0;
  41.         int lateness = 0;
  42.         for (int i = 0; i < jobs.length; i++) {
  43.             time += jobs[i][0];
  44.             if (time > jobs[i][1]) lateness += time - jobs[i][1];
  45.         }
  46.         System.out.println("It has a total lateness of " + lateness + ".");
  47.     }
  48.    
  49.     private int[][] getJobs() {
  50.         int[][] jobs = null;
  51.        
  52.         BufferedReader br;
  53.         BufferedReader c;
  54.         try {
  55.             String line = "";
  56.             int numJobs = 0;
  57.            
  58.             // Count jobs
  59.             c = new BufferedReader(new FileReader("src/jobs.txt"));
  60.             while ((line = c.readLine()) != null) {
  61.                 line = line.trim();
  62.                 if (line.length() != 0) numJobs++;
  63.             }
  64.             System.out.println("Number of jobs: " + numJobs);
  65.             c.close();
  66.            
  67.             // Allocate array based on number of jobs found in .txt file
  68.             jobs = new int[numJobs][2];
  69.            
  70.             // Parse jobs into jobs array
  71.             br = new BufferedReader(new FileReader("src/jobs.txt"));
  72.             line = "";
  73.             int counter = 0;
  74.             while((line = br.readLine()) != null && counter < numJobs) {
  75.                 line = line.trim();
  76.                 if (line.length() != 0) {
  77.                     String [] str = line.split(" ");
  78.                     if (isInt(str[0]) && isInt(str[1])) {
  79.                         jobs[counter][0] = Integer.parseInt(str[0]);
  80.                         jobs[counter][1] = Integer.parseInt(str[1]);
  81.                         counter++;
  82.                     }
  83.                 }
  84.             }
  85.             br.close();
  86.         } catch(Exception e) {System.out.println("Failed to open file.");}
  87.         return jobs;
  88.     }
  89.    
  90.     private boolean isInt(String s) {
  91.         try {
  92.             Integer.parseInt(s);
  93.             return true;
  94.         } catch(Exception e) {}
  95.         return false;
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement