Untitled

 avatar
user_0846129
java
a year ago
2.8 kB
11
Indexable
Never
package OSproject;

import java.text.DecimalFormat;
import java.util.Scanner;

public class sample {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        DecimalFormat format = new DecimalFormat ("##.##");

        System.out.println("Enter the number of processes: ");
        int job = sc.nextInt();

        int Burst[] = new int[job];

        for (int i = 0; i < job; i++) {

            System.out.println("Enter the burst time for " + (i+1) + ": ");
            Burst[i] = sc.nextInt();
        }


        int Priority[] = new int[job];

        for (int i = 0; i < job; i++) {

            System.out.println("Enter the priority number for process " + (i+1) + ": ");
            Priority[i] = sc.nextInt();
        }

        int P1[] = new int[job];

        for ( int i = 0; i < job; i++) {

            P1[i] = Priority[i];
        }

        for (int i = 0; i < job; i++) {

            for (int j = 0; j < job-1; j++) {

                if(Priority[j] > Priority[j+1]) {

                    int temp = Priority[j];
                    Priority[j] = Priority[j+1];
                    Priority[j+1] = temp;
                }
            }
        }



        int wait = 0;
        int turnTable = 0;
        int waitingTime[] = new int[job];
        int turnaround[] = new int[job];
        float totalWait = 0;
        float totalTurn = 0;
        int k = 0;
        int l = 0;

        System.out.format("%63s","===============================================================================\n");
        System.out.format("%15s %15s %15s %18s", "Process", "Burst Time", "Waiting Time", "Turnaround Time\n");
        System.out.format("%63s","===============================================================================\n");


        for (int i = 0; i < job; i++) {


            for (int j = 0; j < job; j++) {

                if (Priority[i] == P1[j]) {

                    turnTable += Burst[j];

                    System.out.format("%12d %13d %14d %15d\n",  (j+1), Burst[j], wait, turnTable);

                    waitingTime[k] = wait;
                    totalWait += wait;
                    wait += Burst[j];

                    turnaround[k] = waitingTime[k] + Burst[j];
                    totalTurn += turnaround[l];


                }

            }

        }

        System.out.println("\n\t\t\t\t\tThe Average Waiting Time is: " + format.format(totalWait/job));
        System.out.println("\n\t\t\t\t\tThe Average Turnaround Time is: " + format.format(totalTurn/job));


        System.out.format("%63s","===============================================================================\n");
    }

}