In this post we are going to solve the problem stated in the previous post; Process Simulator Problem. We used Java language and Object Oriented programming.
To solve our problem, we have created two classes; Processes class and Simulator class.
Process class contains the characteristics and properties of the needed process as stated in Problem statement in the previous post along with their getters and setters methods. Following is a snippet of Process class. You can download the full code from here.
Simulator class is the driver for our project. It contains the main() method that instructs an object of class Simulator, open File object to read the input file, and output File object to write the results.
After than the program call method simulate on instance of class Simulator and pass it the input, and output file objects.
The following snippet of code shows the implementation of main() method.
The following figure shows an example about the input file, which contains the order of execution for the processes, and the desired states for each process.
In method simulate(), we pass the input file object and the output file object. Then we open a BufferedReader to InputStreamReader which holds a FileInputStream, which in turn, wrapps the input file sent as input parameter. See the following figure.
After that, we read the input file line by line and append it to the out BufferedReader.
Then we split each single line to know the required operation; ( ‘C’ = create, ‘I’ = Interrupt, etc ). Then we pass this operation to the selected operation. See the following figure.
For more information about the program, you can download it from here; it contains the Simulator class, Process class, input text file, and the expected output text file.