Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: having problem on creating this shortest job first nonpreemptive algorithm

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default having problem on creating this shortest job first nonpreemptive algorithm

    hello there i'm new in java programming and i'm trying to create a scheduling algorithm which is shortest job first nonpreemptive.

    i've tried to use 3 arrays which are

    System.out.println("enter number of process: ");
    int process = in.nextInt();

    String name[] = new String[process];
    int ArrivalT[] = new int[process];
    int BurstT[] = new int[process];

    and then i've used for loop for inputs of name, BT and AT.
    i've also got the Average turn around time(ATAT) wherein i sum up all the burst time and then divided it by the number of process.
    =================================
    || PROCESS || BT || AT ||
    || P1 || 10 || 2 ||
    || P2 || 6 || 1 ||
    || P3 || 8 || 0 ||
    =================================
    Average Turn Around Time: 8.00 ms
    =================================

    so my problem is i don't know how to get first the lowest AT and display it first then sort the rest by lowest to highest BT and still after i get and sorted the values, it will still be linked with each other. this is what i want to get after sorting

    =================================
    || PROCESS || BT || AT ||
    || P3 || 8 || 0 || <]===== display first the lowest AT
    || P2 || 6 || 1 || <]=====then afterwise sort the BT from lowest to highest.
    || P1 || 10 || 2 ||
    =================================

    can anybody help me? what should i do?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,186
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: having problem on creating this shortest job first nonpreemptive algorithm

    Do you have to use parallel arrays to hold the data or can you create a class to hold the data, put instances of the class in an array and sort them?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: having problem on creating this shortest job first nonpreemptive algorithm

    i can use other but i'm clueless on what i should do and i need some help.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,186
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: having problem on creating this shortest job first nonpreemptive algorithm

    If the data is in a class, and instances of the class are in an array
    what do you want to do with the contents of the array?
    Something like:
    Select one item from the array based on criteria one.
    Sort the remainder of the items in the array based on criteria two.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Sep 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: having problem on creating this shortest job first nonpreemptive algorithm

    i want it to be something like this.
    it'll contain a string, integer, integer.

    something like this
    name-arrivaltime-burst time
    firstprocess 8 2
    secondprocess 3 4
    thirdprocess 5 6
    fourthprocess 7 10

    something like that. it'll contain 3 values.

  6. #6
    Junior Member
    Join Date
    Sep 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: having problem on creating this shortest job first nonpreemptive algorithm

    and then i'll sort it depending on it's arrival time or burst time. but still after sorting it, it'll still contain the 3 values wherein string,integer,integer.
    example is:
    name-arrivaltime-burst time
    firstprocess 8 2
    secondprocess 3 4
    thirdprocess 5 6
    fourthprocess 7 10

    then i will sort it depending on its arrivaltime. so it'll become like this:
    name - arrivaltime - bursttime
    secondprocess 3 4
    thirdprocess 5 6
    fourthprocess 7 10
    firstprocess 8 2

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,186
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: having problem on creating this shortest job first nonpreemptive algorithm

    Ok, now define the class, create a list of instances of the class in a container and look at finding the instance of the class with the first criteria.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Sep 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: having problem on creating this shortest job first nonpreemptive algorithm

    regarding on my sorting problem here's what i've done. i've sorted it through its burst time so this is what i do
    it'll be sorted from the least burst time to the highest bursttime.

    import java.util.*;
    public class SHORTESTJOBFIRST_NP_FINAL{
    public static void main(String[]args)
    {
    Scanner in = new Scanner(System.in);
    System.out.print("Enter Number of Process: ");
    int process = in.nextInt();
    String name[] = new String[process];
    int BurstT[] = new int[process];
    int ArrivalT[] = new int[process];
    for(int x = 0; x<process; x++)
    {
    System.out.print("Name of process: ");
    name[x] = in.next();
    System.out.print("Enter BurstTime: ");
    BurstT[x] = in.nextInt();
    System.out.print("Enter ArrivalT: ");
    ArrivalT[x] = in.nextInt();
    }
    System.out.print("\f");
    System.out.println("**************[USER INPUTTED VALUES]**************");
    System.out.println("============================== ====================");
    System.out.println("||\tPROCESS\t||\tBT\t||\tAT\t| |");
    System.out.println("============================== ====================");
    for(int x=0;x!=process;x++) {
    System.out.println("||\t" + name[x] + "\t||\t" + BurstT[x] + "\t||\t" + ArrivalT[x] + "\t||");
    }
    System.out.println("============================== ====================");
    int tempAT, tempBT;
    String tempname;
    boolean check = false;
     
    do{
    check=false;
    for (int i=0;i < (process-1); i++){
    if (BurstT[i] > BurstT[i+1]){
    tempAT=ArrivalT[i];
    ArrivalT[i]=ArrivalT[i+1];
    ArrivalT[i+1]=tempAT;
     
    tempBT=BurstT[i];
    BurstT[i]=BurstT[i+1];
    BurstT[i+1]=tempBT;
     
    tempname= name[i];
    name[i]= name[i+1];
    name[i+1]=tempname;
     
    check=true;
    }
    }
    }while(check);
    System.out.println("*********[AFTER SORTING INPUTTED VALUES]**********");
    System.out.println("============================== ====================");
    System.out.println("||\tPROCESS\t||\tBT\t||\tAT\t| |");
    System.out.println("============================== ====================");
    for(int x=0;x!=process;x++) {
    System.out.println("||\t" + name[x] + "\t||\t" + BurstT[x] + "\t||\t" + ArrivalT[x] + "\t||");
    }
    System.out.println("============================== ====================");
    System.out.println();
    double sumBT = 0;
    //nevermind this computing for the waiting time coz i'm still not finished in this
    System.out.println("Computing for Waiting Time: ");
    for(int x=0;x!=process;x++)
    {
    System.out.println(name[x]+" = "+BurstT[x]+" - "+ArrivalT[x]);
    }
    System.out.println("Turn Around Time(TAT) for each processes: ");
    for(int x=0;x!=process;x++)
    {
    System.out.println(name[x]+" = "+BurstT[x]);
    }
     
     
    for(int x=0;x!=process;x++)
    {
    sumBT +=BurstT[x];
    }
    System.out.printf("Average Turn Around Time(ATAT): %5.2f ms",(sumBT/process));
    }
    }
    now what i nid it to do is get 1 process that has the very least arrival time because that's where the process will start and then display it on the top, and then i'll sort the rest by regarding on their BurstTime from lowest to highest.
    //sorry i'm not good in explaining so i'll just visualize what i want to say.

    i'll say if i input this:
    **************[USER INPUTTED VALUES]**************
    ==================================================
    || PROCESS || BT || AT ||
    ==================================================
    || p1 || 8 || 4 ||
    || p2 || 10 || 1 ||
    || p3 || 2 || 2 ||
    || p4 || 7 || 3 ||
    || p5 || 4 || 0 ||
    ==================================================

    this will be the result but then
    *********[AFTER SORTING INPUTTED VALUES]**********
    ==================================================
    || PROCESS || BT || AT ||
    ==================================================
    || p3 || 2 || 2 ||
    || p5 || 4 || 0 ||
    || p4 || 7 || 3 ||
    || p1 || 8 || 4 ||
    || p2 || 10 || 1 ||
    ==================================================

    I want it to be like this.
    ==================================================
    || PROCESS || BT || AT ||
    ==================================================
    || p5 || 4 || 0 || <==== where in first is i'll get the lowest ArrivalTime then display it on top of the sorted list
    || p3 || 2 || 2 || <==== and then the rest is sorted via their BurstTime and it'll be lowest to highest BurstTime
    || p4 || 7 || 3 ||
    || p1 || 8 || 4 ||
    || p2 || 10 || 1 ||
    ==================================================

    can somebody help me ? please?
    Last edited by morphling18; September 29th, 2012 at 02:35 AM.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,186
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: having problem on creating this shortest job first nonpreemptive algorithm

    Please edit your post and format the code with proper indentations. The source statements should not all start in the first column. Unformatted code is hard to read and understand.

    i've sorted it through its burst time so this is what i do
    it'll be sorted from the least burst time to the highest bursttime.
    After sorting on BT you need to find the lowest AT and put that at the front.
    Last edited by Norm; September 29th, 2012 at 07:15 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Sep 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: having problem on creating this shortest job first nonpreemptive algorithm

    @Norm
    what should i use to get that? any advice sir, or any recommendation on what i should use to it.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,186
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: having problem on creating this shortest job first nonpreemptive algorithm

    Are you having a problem to search through an array for the lowest value in the array?
    One technique is to assume the first item in the array is the smallest and then to compare all the other elements against that one to find a smaller one. When a smaller one is found, use it to compare against the rest of the elements looking for a smaller one. Continue to the end of the array.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. ArrayIndexOutOfBoundsException problem in decryption algorithm
    By aesguitar in forum What's Wrong With My Code?
    Replies: 10
    Last Post: July 13th, 2012, 03:49 PM
  2. Replies: 3
    Last Post: May 25th, 2012, 08:48 PM
  3. CHALLENGE - find the shortest path
    By ice in forum Algorithms & Recursion
    Replies: 13
    Last Post: January 20th, 2011, 11:30 AM
  4. Replies: 0
    Last Post: May 11th, 2010, 03:37 AM
  5. I have algorithm problem
    By Newoor in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 11th, 2009, 07:11 PM