-1

I am currently coding using JAVA language, an array and a queue. I am having a problem with using an array that is a "Process" type. I am reading an input file which have the following:

3 1 5 30 3 1 5 30 4 0 5 30 3

Here is the stacktrace. When you see my console, you can see that each element (=Process type process object) takes in all of its properties (a, b, c, d) properly, when I am adding each of them into allProcesses array.

However, when I print out allProcesses array USING the printProcesses() method, the "a" property of each Process objects are all 0 --> allProcess[p].a = 0 whereas it should be 1.

Upon the requests, here are FCFS(), and Process class.

//Sorts "allProcesses" in an ascending order of "a".
public static void FCFS (Process[] allProcesses, int numProcesses) {
    ArrayList<Process> FCFSsortedAllProcesses = new ArrayList<Process>();
    // Iterating through the created list from the position
    for (int p = 0; p < allProcesses.length; p++) {
        for (int j = p + 1; j < allProcesses.length; j++) {
            if (allProcesses[p].a > allProcesses[j].a) {
                Process temp = allProcesses[p];
                allProcesses[p] = allProcesses[j];
                allProcesses[j] = temp;
            }
        }
    }
}

public class Process {
/* Linked list Node*/
    int id;
    static int a;
    int b;
    int c;
    int io; 
    int readyCycle;
    int CPUburstRemaining;
    int CPUcycle;
    int IOburstRemaining;
    int IOcycle;
    String state;
    String relationship;
    int priority;
    //int runningTime;
    int finishingTime;
    int turnaroundTime;
    int ioTime;
    int waitingTime;
    Process next; 
    //Node prev;

    // Constructor to create a new node 
// Next is by default initialized as null 
public Process(int a, int b, int c, int io){
    this.id = id;
    this.a = a;
    this.b = b;
    this.c = c;
    this.io = io;
    this.readyCycle = readyCycle;
    this.CPUburstRemaining = 0;
    this.CPUcycle = 0;
    this.IOburstRemaining = 0;
    this.IOcycle = 0;
    this.state = "unstarted";
    this.relationship = null;
    this.priority = 0;
    this.finishingTime = 0;
    this.turnaroundTime = 0;
    this.ioTime = 0;
    this.waitingTime = 0;

}
}

I tried to find the error using my debugger, but all I figured out is that the Process object doesn't take its "a" property value that was originally given. Here is the debugger window that shows the properties of the first "curElement" in printProcesses(). The "a" property is just missing. The same goes for the other two curElements as well.

Please advise me on how to fix this issue, and let me know if there is any other information I should provide to make it easier for you. Thanks in advance.

public static void main(String args[]) {
    try {
        String fileAddress = args[0];
        File fileInput  = new File(fileAddress); //Read
        Scanner scan    = new Scanner(fileInput);
        int numProcesses  = scan.nextInt();
        Queue<Process> processes = new LinkedList<Process>();
        Process[] allProcesses = new Process[numProcesses];

        //Adding each process to processes queue
        for (int m = 0; m < numProcesses; m++) {
            int a = scan.nextInt();
            int b = scan.nextInt();
            int c = scan.nextInt();
            int io = scan.nextInt();
            Process thisProcess = new Process(a, b, c, io); 
            thisProcess.id = m;
            processes.add(thisProcess);
            allProcesses[m] = thisProcess;
            System.out.println(m + " thisProcess.a = " + thisProcess.a);
            System.out.println(m + " allProcesses[m].a = " + allProcesses[m].a);
        }
        System.out.printf("\noriginal\n");
        printProcesses(allProcesses, numProcesses); //original
        FCFS(allProcesses, numProcesses);
        System.out.println();
        System.out.printf("sorted\n");
        printProcesses(allProcesses, numProcesses); //sorted
    }   
    catch (Exception e){
        e.printStackTrace();
            System.out.printf(" Error: File not foundd. \n");
    }
}
public static void printProcesses (Process[] allProcesses, int numProcesses) {
    System.out.printf("The original input was:  ");
    for (int p = 0; p < allProcesses.length; p++) {
        Process curElement = allProcesses[p];
        System.out.printf("%d %d %d %d   ", curElement.a, curElement.b, curElement.c, curElement.io);
    }
    System.out.print("\n\n");
}
Alex Kang
  • 11
  • 5
  • Are you doing some method interception while calling `printProcesses`. Can you show the Process class? – HariUserX Mar 19 '19 at 20:24
  • Please show the `Process` class and `FCFS` method. – Robert Bain Mar 19 '19 at 20:26
  • Thank you. Updated with Process class and FCFS method. I don't think I have done any method interception (in my understanding which is limited) – Alex Kang Mar 19 '19 at 20:36
  • 1
    Your field `a` is static. Read https://stackoverflow.com/questions/797964/what-is-the-exact-meaning-of-static-fields-in-java and https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html. – JB Nizet Mar 19 '19 at 20:36
  • Oh my god. You are right! Thank you sm :) – Alex Kang Mar 19 '19 at 20:38

1 Answers1

0

You have static int a;. So it will have the last update which in your case is zero. And the reason you are not getting a in debugger is most likely your settings. There should be show static variables in your debug menu for eclipse.

HariUserX
  • 1,341
  • 1
  • 9
  • 17