Advertisement
lpuarmy

Priority | Retno

May 26th, 2013
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.93 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Priority {
  4.     public static void main(String[] args) {
  5.         Scanner input = new Scanner(System.in);
  6.  
  7.         System.out.println("    ----------------------------------------------");
  8.         System.out.println("\t Penjadwalan CPU - Priority");
  9.         System.out.println("    ----------------------------------------------");
  10.         System.out.print("\n Jumlah Proses : ");
  11.         int n = input.nextInt();
  12.  
  13.         int[] aTime = new int[n];
  14.         int[] bTime = new int[n];
  15.         int[] process = new int[n];
  16.         int[] prio = new int[n];
  17.        
  18.         for(int i=0;i<n;i++) {
  19.             process[i]=i+1;
  20.             System.out.print("\n P["+(i+1)+"] ->");
  21.             System.out.print(" Arrival time : ");
  22.             aTime[i] = input.nextInt();
  23.             System.out.print("         Burst time   : ");
  24.             bTime[i] = input.nextInt();
  25.             System.out.print("         Priority     : ");
  26.             prio[i] = input.nextInt();
  27.         }
  28.  
  29.         int aTemp, bTemp, pTemp, prTemp;
  30.         for(int i=0;i<n;i++){
  31.             for(int j=0;j<n-1;j++){
  32.                 if(aTime[j]>aTime[j+1]){
  33.                     pTemp = process[j];
  34.                     process[j] = process[j+1];
  35.                     process[j+1] = pTemp;
  36.  
  37.                     aTemp = aTime[j];
  38.                     aTime[j] = aTime[j+1];
  39.                     aTime[j+1] = aTemp;
  40.  
  41.                     bTemp = bTime[j];
  42.                     bTime[j] = bTime[j+1];
  43.                     bTime[j+1] = bTemp;
  44.                    
  45.                     prTemp = prio[j];
  46.                     prio[j] = prio[j+1];
  47.                     prio[j+1] = prTemp;
  48.                 }
  49.             }
  50.         }
  51.  
  52.         for(int i=0;i<n;i++){
  53.             for(int j=1;j<n-1;j++){
  54.                 if(prio[j]>prio[j+1]){
  55.                     pTemp = process[j];
  56.                     process[j] = process[j+1];
  57.                     process[j+1] = pTemp;
  58.  
  59.                     aTemp = aTime[j];
  60.                     aTime[j] = aTime[j+1];
  61.                     aTime[j+1] = aTemp;
  62.  
  63.                     bTemp = bTime[j];
  64.                     bTime[j] = bTime[j+1];
  65.                     bTime[j+1] = bTemp;
  66.                    
  67.                     prTemp = prio[j];
  68.                     prio[j] = prio[j+1];
  69.                     prio[j+1] = prTemp;
  70.                 }
  71.             }
  72.         }
  73.  
  74.        
  75.         float wTime = 0;
  76.         int tTime = 0;
  77.         float wTot = 0;
  78.        
  79.         tTime=aTime[0];
  80.         System.out.println("\n  -----------------------------------------------");
  81.         System.out.println("  |   Process\t|   Start Time \t|   Stop Time\t|");
  82.         System.out.println("  -----------------------------------------------");
  83.        
  84.         for (int i=0;i<n;i++) {
  85.             System.out.println("  |\tP"+process[i]+"\t|\t"+tTime+" \t|\t "+(tTime+bTime[i])+"\t|");
  86.             if (i == n-1) continue;
  87.             tTime += bTime[i];
  88.             wTime = tTime - aTime[i+1];
  89.             wTot += wTime;
  90.         }
  91.         System.out.println("  -----------------------------------------------------------------------");
  92.         float avg = wTot / n;
  93.         System.out.println("\n -> Average waiting time : "+avg);
  94.         System.out.println("");
  95.         System.out.println("  -----------------------------------------------------------------------");
  96.         System.out.println("\nGannt Chart");
  97.         tTime = aTime[0];
  98.         int h = 22;
  99.         try{
  100.             for(int i=0;i<=n;i++) {
  101.                 System.out.print(tTime);
  102.                 for(int j=1;j<=bTime[i];j++) {
  103.                     if(i == n) break;
  104.                     System.out.printf("%c",h);
  105.                 }
  106.                 tTime += bTime[i];
  107.             }
  108.         }catch (Exception e){
  109.             System.out.println("");
  110.             System.exit(0);
  111.         }
  112.        
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement