Hi,
I'm having problems with line 37 in my Driver class. I know that I get that error because, according to Java, the variable Task, in the Driver, has not been initialized, but I do not see how I'm not initializing it.
Thanks
import java.util.Scanner;
import java.io.*;
public class Driver
{
public static void main(String [] arg) throws IOException
{
Scanner Scan = new Scanner(new File("mfq.txt"));
LineWriter lw = new LineWriter("csci.txt");
//numbers of queue in the system
int queues = 5;
//Array holding priority Queues of objects
ObjectQueue [] objQueue = new ObjectQueue [queues];
//Variable to hold any process/task to be pass to the CPU
//from the text file or a queue
Process Task;
//variable to finish all process when set to false
boolean END;
//CPU object
CPU Core = new CPU();
//variable to hold time elapsed in the machine
int Machine_Time = 0;
// q set to -1 as initial value
//int q;
//move all process into objQueue[0]
while (Scan.hasNext())
{
Task = new Process (Scan.nextLine(), lw, 0);
objQueue[0].insert(Task);
}
//set all process into the first priority queue
do
{
//set END to false for a continuous loop until all queues are empty and q to -1
END = false;
int q = -1;
//check if there is any process in the input Queue
if (!objQueue[0].isEmpty())
{
//check if process needs to be send to the CPU
if (((Process)objQueue[0].query()).getSendTime() == Machine_Time)
{
q = 0;
}
}
//if process does not have to be send to CPU, look in priority queues
if (q != 0)
{
for (int i = 1; i < queues; i++)
{
if (!objQueue[i].isEmpty())
{
q = i;
break;
}
}
}
if (q > -1)
{
Task = (Process) objQueue[q].remove();
if (Core.Busy())
{
if (q < queues - 1)
q++;
Task.setQueue(q);
objQueue[q].insert(Task);
}
else
{
if (q == 0)
Core.getProcess(Task, Machine_Time);
else
Core.getProcess(Task);
}
Core.Process();
//if CPU is not busy
if (!Core.Busy())
{
Task = Core.freeCPU(Machine_Time);
if (Task.getTimer() == 0)
Task.EndProcess();
else
{
if (q < queues - 1)
q++;
Task.setQueue(q);
objQueue[q].insert(Task);
}
}
//check if all queues are empty
for (int c = 0; c < queues; c++)
{
if (!objQueue[c].isEmpty())
{
END = true;
break;
}
}
}
}while (END);
}
}
public class Process
{
//current queue
private int queue = 0;
//time to send to CPU/Core
private int submit_time;
//process id
private int id;
//time for process to be completed
private int timer;
//time process arrives for the first time into the CPU
private int time_in;
//time process is completed
private int time_out;
private LineWriter lw;
//initialize queue, id, timer and submit_time variables
public Process(String str, LineWriter l, int q)
{
lw = l;
queue = q;
String temp = "";
id = 0;
timer = 0;
submit_time = 0;
for (int c = 0; c < str.length(); c++)
{
while (str.charAt(c) == ' ')
{
c++;
}
while ((c < str.length()) && (str.charAt(c) != ' '))
{
temp = temp + str.charAt(c);
c++;
}
if (submit_time == 0)
submit_time = Integer.valueOf(temp);
else if (id == 0)
id = Integer.valueOf(temp);
else if (timer == 0)
timer = Integer.valueOf(temp);
temp = "";
}
}
//decrease time left for process to be completed
public int getTimer()
{
return timer;
}
//set new time for process to be completed
public void setTimer()
{
timer--;
}
//returns process id
public int getId()
{
return id;
}
//set current queue
public void setQueue(int x)
{
queue = x;
}
//return current queue
public int getQueue()
{
return queue;
}
//return time when the process is send to the CPU
public int getSendTime()
{
return submit_time;
}
//records the machine time that the process was send to the CPU
public void setTimeIn( int x)
{
time_in = x;
}
//set the machine time when the CPU releases a process
public void setTimeOut(int x)
{
time_out = x;
}
//retunrs machine time when process is completed
public int getTimeOut()
{
return time_out;
}
public void EndProcess()
{
lw.println(time_in + " " + " " + id + " " + time_out );
//lw.println();
System.out.println(time_in + " " + " " + id + " " + time_out );
//System.out.println();
}
}
public class CPU
{
//time that the CPU will spend in a process
private int CPU_Time;
//process received by the CPU
private Process Work;
//variable to hold busy flag
private boolean busy_flag;
public CPU()
{
busy_flag = false;
CPU_Time = 0;
}
//returns true or false if CPU_Time is not 0 (CPU busy)
public boolean Busy()
{
return busy_flag;
}
//pass process for the first time to CPU
public void getProcess(Process Task, int t)
{
Work = Task;
Work.setTimeIn(t);
CPU_Time = (int) java.lang.Math.pow(2, (double) Work.getQueue());
busy_flag = true;
}
//pass process from queue into CPU
public void getProcess(Process Task)
{
Work = Task;
CPU_Time = (int) java.lang.Math.pow(2, (double) Work.getQueue());
busy_flag = true;
}
//decrease CPU timer by 1 unit and increase and clears busy flag if CPU_Time = 0
public void Process()
{
CPU_Time--;
//decrease time needed to complete process
Work.setTimer();
if (CPU_Time == 0 || Work.getTimer() == 0)
busy_flag = false;
}
//returns Process to be passed to queue
public Process freeCPU(int x)
{
Work.setTimeOut(x);
return Work;
}
}
/**
* Write a description of class ObjectQueue here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class ObjectQueue
{
private Object[] item;
private int front;
private int rear;
private int size;
public ObjectQueue(){
size = 100;
item = new Object[size];
front = size-1;
rear = size-1;
}
public ObjectQueue(int max){
size = max;
item = new Object[size];
front = size-1;
rear = size-1;
}
public boolean isEmpty(){
return front == rear;
}
public boolean isFull(){
return rear == size-1 ? front == 0 : front == rear+1;
}
public void clear(){
front = size-1;
rear = size-1;
}
public void insert(Object x){
if (isFull()){
System.out.println("Insert Runtime Error: Queue Overflow");
System.exit(1);
}
if (rear == size-1)
rear = 0;
else
rear++;
item[rear] = x;
}
public Object remove(){
if (isEmpty()){
System.out.println("Remove Runtime Error: Queue Underflow");
System.exit(1);
}
if (front == size-1)
front = 0;
else
front++;
return item[front];
}
public Object query(){
if (isEmpty()){
System.out.println("Query Runtime Error: Queue Underflow");
System.exit(1);
}
if (front == size-1)
return item[0];
else
return item[front+1];
}
}
// LineWriter.java
import java.io.*;
/* @author Richard Stegman
* @version 2.1, 1/5/07
*
* LineWriter provides simple methods for opening and writing to text files.
*
* Example:
*
* LineWriter lw = new LineWriter(foo.txt");
* lw.println("This is a string.");
* lw.println();
* lw.println(108);
* lw.close();
*
*/
public class LineWriter {
private PrintWriter outFile;
/*
* LineWriter constructor.
* Creates a new file or overwrites an existing file.
* @param fileName name of the file to be created
*/
public LineWriter(String fileName) {
try {
FileWriter fw = new FileWriter(fileName);
BufferedWriter bw = new BufferedWriter(fw);
outFile = new PrintWriter(bw);
}
catch (IOException e) {
System.out.println("LineWriter cannot open output file: " + fileName);
e.printStackTrace();
}
}
/*
* LineWriter constructor.
* Creates a new file. If the file exists can append to it.
* @param fileName name of the file to be created
* @param mode if equal to "a" opens in append mode
*/
public LineWriter(String fileName, String mode) {
try {
FileWriter fw = new FileWriter(fileName, "a".equals(mode));
BufferedWriter bw = new BufferedWriter(fw);
outFile = new PrintWriter(bw);
}
catch (IOException e) {
System.out.println("LineWriter cannot open output file: " + fileName);
e.printStackTrace();
}
}
/**
* Outputs a string and newline to the file.
* @param s string to be output
*/
public void println(String s) {
outFile.println(s);
}
/**
* Outputs a string to the file.
* @param s string to be output
*/
public void print(String s) {
outFile.print(s);
}
/**
* Outputs an integer and newline to the file.
* @param i integer to be output
*/
public void println(int i) {
outFile.println(i);
}
/**
* Outputs an integer to the file.
* @param i integer to be output
*/
public void print(int i) {
outFile.print(i);
}
/**
* Outputs a long and newline to the file.
* @param l long to be output
*/
public void println(long l) {
outFile.println(l);
}
/**
* Outputs a long to the file.
* @param l long to be output
*/
public void print(long l) {
outFile.print(l);
}
/**
* Outputs a short and newline to the file.
* @param s short to be output
*/
public void println(short s) {
outFile.println(s);
}
/**
* Outputs a short to the file.
* @param s short to be output
*/
public void print(short s) {
outFile.print(s);
}
/**
* Outputs a byte and newline to the file.
* @param b byte to be output
*/
public void println(byte b) {
outFile.println(b);
}
/**
* Outputs a byte to the file.
* @param b byte to be output
*/
public void print(byte b) {
outFile.print(b);
}
/**
* Outputs a boolean and newline to the file.
* @param b boolean to be output
*/
public void println(boolean b) {
outFile.println(b);
}
/**
* Outputs a boolean to the file.
* @param b boolean to be output
*/
public void print(boolean b) {
outFile.print(b);
}
/**
* Outputs a float and newline to the file.
* @param f float to be output
*/
public void println(float f) {
outFile.println(f);
}
/**
* Outputs a float to the file.
* @param f float to be output
*/
public void print(float f) {
outFile.print(f);
}
/**
* Outputs a double and newline to the file.
* @param d double to be output
*/
public void println(double d) {
outFile.println(d);
}
/**
* Outputs a double to the file.
* @param d double to be output
*/
public void print(double d) {
outFile.print(d);
}
/**
* Outputs a character and newline to the file.
* @param c char to be output
*/
public void println(char c) {
outFile.println(c);
}
/**
* Outputs a character to the file.
* @param c char to be output
*/
public void print(char c) {
outFile.print(c);
}
/**
* Outputs an object and newline to the file.
* @param o object to be output
*/
public void println(Object o) {
outFile.println(o);
}
/**
* Outputs an object to the file.
* @param o object to be output
*/
public void print(Object o) {
outFile.print(o);
}
/**
* Outputs a newline character to the file.
*/
public void println() {
outFile.println();
}
/**
* Closes the file.
*/
public void close() {
outFile.flush();
outFile.close();
}
}