Queues
The goal of this lab period is to augment your LinkedList class from last week by implementing a Queue interface, and to practice basic file in-out procedures.
Tasks
Implementing a Queue
- Setup
Make a new folder for this week's project.
Copy over your LinkedList and LinkedListTests files from the previous project and download this Queue interface.
- Implement the Queue interface
Edit the declaration of your LinkedList class to implement the Queue interface:
public class LinkedList<T> implements Queue<T>
After adding this implementation declaration, your IDE might tell you you're missing a few methods. If you check the Queue interface, you'll probably see that your LinkedList is missing the
offer
,peek
, andpoll
methods. Implement these methods by calling the appropriate method from your LinkedList (your solutions to homework assignment 4 might be helpful here).Having done so, add a few tests to the LinkedListTests file to confirm that your queue is operating as expected.
Reading in data from a file
- Create a Job class
When jobs are created, they will be specified by their arrival time (which we'll just represent as a double, with our first job arriving at time 0.0). Additionally, each job will specify its total processing time, ie the amount of time necessary to spend working on a job in order to complete it.
As such, we'll want to create a Job class template:
public Job(double arrivalTime, double processingTime)
: this constructs the job with the specified arrival time and necessary total processing time. Save these two values as fields of the class.- Reading in a file
Download this file of jobs. If you open the file, you'll see a header line followed by 20 lines specifying a pair of numbers. Each pair of numbers denotes the arrival time and the total processing time of a job. Our goal is to be able to take this file (and other files constructed similarly) and parse them to successfuly create a sequence of Job objects in Java with the same data as this file.
To accomplish this, we'll use two classes from the java.io package, namely
FileReader
andBufferedReader
. To use them, let's create a class called JobReader and make a static (i.e. we don't need to create an instance of JobReader to use this method) read method inside it to read in this file:public static Queue<Job> readJobFile(String filename){ try { // assign to a variable of type FileReader a new FileReader object, passing filename to the constructor FileReader fr = new FileReader(filename); // assign to a variable of type BufferedReader a new BufferedReader, passing fr to the constructor BufferedReader br = new BufferedReader(fr); // assign to a variable of type Queue<Job> a new LinkedList. Queue<Job> jobSequence = new LinkedList<Job>(); // assign to a variable of type String line the result of calling the readLine method of the BufferedReader. String line = br.readLine(); // everytime we call br.readLine(), we advance to the next line of the file we are reading. // Since the first line of the job files are just the headers, // let's skip the first line by calling br.readLine again: line = br.readLine(); // start a while loop that loops while line isn't null while(line != null){ // assign to an array of type String the result of calling split on the line with the argument "," // let's see what this array holds: //Create a new job by parsing the arrival time and processing time out of the String array //Offer it to jobSequence //Read the next line of the file } // call the close method of the BufferedReader: br.close(); return jobSequence; } catch(FileNotFoundException ex) { System.out.println("JobReader.read():: unable to open file " + filename + ": file not found"); } catch(IOException ex) { System.out.println("JobReader.read():: error reading file " + filename); } return null; }
This method has a few lines that must be completed. What these lines should do is given to you in comments.
Now that we have a class to model our jobs, we're ready to import data to create a sequence of jobs to run.
This project will be focused on some queueing theory basics. As such, we will need a way of modeling jobs to run on our computer.
When you are done, you can proceed to the project.