Search code examples
javaround-robin

Using round robin algorithm to calculate time needed


Maybe someone out there is feeling friendly and would find this brain teaser interesting.. but I feel like maybe I started confusing myself.

The object of this is to calculate, using the round robin algorithm, the time it would take for all of the processes to complete. I have it prompt the user for the time quantum, and then how many processes it would like to calculate. From there, I throw a for statement based on however many processes there are to assign the process arival time and the burst time.

For those who arent familiar, the time quantum is how many cycles it will process before it switches to the next, the burst is how many cycles it takes to complete that process, and of course the arival time is how many cycles have completed before it arives. Simple algorithm, but it's to show how CPU's schedule. IF ANYONE COULD HELP AT ALL THAT WOULD BE FANTASTIC! I'm so lost. I wanted to do this in C#, but my programming skills are less than sufficent in C# for that.

The two problems I am having are in my if statements, I started to lose myself, and for whatever reason, it gives me an error when compiling with dif < parrive.get[ii] or dif < parrive.get(ii) OR even assigning parrive.get[ii] to another variable at the beginning of my if statements and using another variable(as shown)...

import java.io.*;
import java.util.*;

public class Thread{       

    public Thread() {
        inputexecute();
    }

    public void inputexecute(){

        Scanner x = new Scanner(System.in);
        int xx = 0;

        String choice = null;
        ArrayList parrive = new ArrayList();
        ArrayList pburst = new ArrayList();

        while (true){
            System.out.println("Enter the time quantum: ");
            int quant = x.nextInt();
            System.out.println("Enter the number of processes: ");
            int pnum = x.nextInt();

            for( int i=0; i<pnum; i++)
            {
                System.out.println("Enter the arival for p"+i+": ");
                int arrive = x.nextInt();
                parrive.add(arrive);

                System.out.println("Enter the burst time: ");
                int burst = x.nextInt();
                pburst.add(burst);    


            }

            int dif;
            for(int ii=0; ii < pnum; ii++)
            {
                int asw == parrive.get[ii];

                if (asw < quant)
                {
                    dif = quant - asw;
                }                    
                if (quant < asw)
                {
                    asw = asw - quant;
                }
                if (dif > 0)
                {

                }
            }
        } /* end while*/
    } /* end exec input*/
} /* class thread */

Solution

  • The way I would write it

    List<Integer> parrive = new ArrayList<Integer>();
    
    for(int asw: parrive) {
        int dif = Math.abs(asw - quant);
        if (dif == 0) continue;
        // if dif > 0
    
    }
    

    I am assuming

    asw = asw - quant;
    

    should be

    dif = asw - quant;