// #2 SHELL package ooos; public class PCB { // UML shows 9 constants, each is: // "#": protected, visible by other classes in package // protected are also visible by subclasses, but PCB does not have subclasses // underlined: static, associated once with class, not each object // CAPITAL: constant, does not change. In Java, we use final // integer type with assigned values // For example: protected static final int NEW = 0; // UML shows 6 variables, each is: // "-": private, visible only within this class // type: i.e. String, int, long // For example: private int state = NEW; // UML shows 8 methods, each is: // "#": protected, like the constants above // Constructor has same name as class, and does NOT have a return type, not even VOID protected PCB(String name, int priority) { // 1: this.name refers to the variable above // "this" is a pointer/reference/handle to this PCB object // name is just the parameter in the constructor // 2: similar for priority // 3: OOOS is a class, procTable is a static variable within OOOS, hence a class variable // To refernece this static variable: OOOS.procTable // UML shows to apply the register method to this object // UML shows the parameter should be "this", the pointer to this PCB object // We are handing a reference to ourselves over to another method OOOS.procTable.register(this); } // getState is just an accessor method for the state variable protected int getState() { return state; } // getName, getPrio, getCpuUsage, getVisits are also accessor methods // setPrio is just a mutator method for the priority variable protected void setPrio(int priority) { // 1: this.priority = priority; } // The CONSTANTS above, e.g. NEW, are just integers and not very good for printing // (Users cannot remember the numbers easily) // Instead, manually convert each integer into an easily printable string protected String getStateString() { switch (state) { case NEW : return "NEW"; // and other state values } return ""; } // setState is a mutator method, but more complicated // Parameter "state" is the state the process is entering // this.state is the variable above, and is the current state of the process // Each time a setState is called on a process, the following can be determined: // 1: the current time // 2: if the process is currently on the CPU, then it must be leaving the CPU // Record the usage time from starting on CPU to leaving CPU // 3: if the process is starting on the CPU, then record the entry time // also increment the number of visits to the CPU // 4: mutate the state variable protected void setState(int state) { // 1: get the current time, store in local long t // 2: IF // 3: IF // 4: mutate } }