views:

40

answers:

1

Hello,
I am trying to create a program that is just like a Process Table.
I have to implement a class PCB (Process Control Block) with several fields such as:
process name (a string)
process priority (an integer)
register set values (an object of a class Register Set containing the following fields: XAR, XDI, XDO, PC.


Then my program needs to then create a Process Table data structure as either an array (max size 100 elements) or an arraylist of type PCB, and initialize the array with data from the file "processes1.txt" Then the Process Table arrraylist has to print out its contents by each process.


So my questions are:
1. How many programs/classes do I have to write? Is it 3. The first program that creates the Process Table arraylist of PCB. The 2nd class would be the PCB class that defines the PCB fields.
2. How would the first program initialize the arraylist with the data from the text file?
3. Could I use an ArrayList of an ArrayList? and how would I do that?

Thank you in advance.

A: 
  1. ProcessTable, ProcessControlBlock, RegisterSet sound like good starts.
  2. I'd create a method in ProcessTable called load(File file) that uses File, and perhaps TextReader to read the configuration. There are many ways to read a text file. Also google on BufferedInputStream. Examples abound.
  3. ArrayLists can hold objects, and ArrayList is indeed an Object, so yes. The use is easy: someArrayList.add(someOtherArraylist); though the declaration is a little harder:
ArrayList<ArrayList<String>> a = new ArrayList<ArrayList<String>>();

which says 'a is to be an ArrayList containing other ArrayLists containing Strings.` there are other ways to write the declaration that are a little more general, but this shows the gist.

Tony Ennis
could I do ArrayList<PCB> a = new ArrayList<ArrayList<String>>();
Luron
does that make sense? probably not.
Luron
i wanna make ArrayList <PCB> ProcessTable = new ArrayList<ArrayList<String>>
Luron
What he probably means is you want a class called ProcessTable, not a list of them. There's only one process tabe, right? This class might have an attribute defined as `private ArrayList<PCB> processControlBlocks = new ArrayList<PCB>();` So we'd expect the PCB class to have attributes such as `name`, `priority`, and `registerSet`.
Tony Ennis