In this lab, you will create a rudimentary “job simulator”. This will be a time-
ID: 3790891 • Letter: I
Question
In this lab, you will create a rudimentary “job simulator”. This will be a time-based simulator
which means each “step” in the simulation will represent a single time unit. A job is modeled
simply by a number of instructions to be processed. A provided Processor class is used to
“process” jobs. A Processor has a speed field; this field determines how many instructions it can
process in a single time unit. A Job Simulator will maintain a Job queue and will have a set of
Processor objects to process Jobs.
There are two primary methods used by clients of the Simulator. The enqueueJob method
allows a new Job to be added to the Simulator’s Job queue. The other primary method is the
step which simulates job processing.
When the step method is called, the Simulator must do the following tasks, in order.
1. For each Processor it has, dequeue a Job. Assign the first dequeued Job to the first
Processor, and so-on.
2. For each dequeued Job, use the assigned Processor to process the Job. This means
reducing the number of remaining Job instructions by the Processor speed.
3. Add all Jobs that have no more instructions to be processed to the list of finished Job, in
order that they were dequeued.
4. Enqueue the other Jobs back into the Job queue, in the order that they were dequeued.
A client may call enqueueJob on a Simulator whenever it likes; assume that that call does not
take up any time units. Your Simulator must maintain the proper state of remaining Job
instructions, queued Jobs, and finished Jobs, knowing that a series of calls to enqueueJob and
step can be made in any order and any number of times.
Part 1 - Implementation
1. Create a class that extends the lab.Simulator abstract class.
2. Create a factory class that implements the lab.SimulatorFactory
interface and that constructs new instances of your Simulator implementation.
Part 2 - Testing
1. Enable assertions in your Java execution environment.
2. Create a tester class in this package:
lab.test
3. In the main method of your tester class, pass an instance of your SimulatorFactory class
to one of the static test methods in lab.test.SimulatorTester.
PROVIDED CLASSES:
*Implement class*
public class Implementation extends Simulator
{
public Implementation(LinkedHashSet<Processor> processors) {
super(processors);
// TODO Auto-generated constructor stub
}
@Override
public Deque<Job> getJobQueue() {
// TODO Auto-generated method stub
return null;
}
@Override
public LinkedHashSet<Job> getFinishedJobs() {
// TODO Auto-generated method stub
return null;
}
@Override
public void enqueueJob(Job newJob) {
// TODO Auto-generated method stub
}
@Override
public void step() {
// TODO Auto-generated method stub
}
}
*Factory Class*
public class Factory implements SimulatorFactory{
@Override
public Simulator createSimulator(LinkedHashSet<Processor> processors) {
// TODO Auto-generated method stub
return null;
}
}
Explanation / Answer
Code:
SimulatorTester.java
package lab.test;
import java.util.LinkedHashSet;
import lab.Factory;
import lab.Implementation;
import lab.Job;
import lab.Processor;
public class SimulatorTester {
public static void main(String[] args) {
LinkedHashSet<Processor> pList = new LinkedHashSet<Processor>();
pList.add(new Processor("processor1", 1));
pList.add(new Processor("processor2", 0.5));
Implementation sim = (Implementation) new Factory().createSimulator(pList);
sim.enqueueJob(new Job("Job1", 5));
sim.enqueueJob(new Job("Job2", 3));
sim.step();
LinkedHashSet<Job> jobList = sim.getFinishedJobs();
System.out.println(jobList);
}
}
Implementation.java
package lab;
import java.util.Deque;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
public class Implementation extends Simulator {
public Implementation(LinkedHashSet<Processor> processors) {
super(processors);
}
@Override
public Deque<Job> getJobQueue() {
return super.getJobQueue();
}
@Override
public LinkedHashSet<Job> getFinishedJobs() {
return finishedJobs;
}
@Override
public void enqueueJob(Job newJob) {
jobQueue.add(newJob);
}
@Override
public void step() {
HashSet<Processor> lhs = super.getProcessors();
Iterator<Processor> itr = lhs.iterator();
while(itr.hasNext()){
Processor processor = itr.next();
if(processor.isAvailable()) {
Job job = jobQueue.pop();
processor.process(job);
}
}
}
}
Factory.java
package lab;
import java.util.LinkedHashSet;
public class Factory implements SimulatorFactory{
@Override
public Simulator createSimulator(LinkedHashSet<Processor> processors) {
Simulator sim = new Implementation(processors);
return sim;
}
}
Job.java
package lab;
public class Job {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNumberOfInstructions() {
return numberOfInstructions;
}
public void setNumberOfInstructions(int numberOfInstructions) {
this.numberOfInstructions = numberOfInstructions;
}
private int numberOfInstructions;
private String name;
public Job(String name, int numberOfInstructions) {
super();
this.name = name;
this.numberOfInstructions = numberOfInstructions;
}
}
Processor.java
package lab;
public class Processor {
private double speed;
private boolean available;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isAvailable() {
return available;
}
public void setAvailable(boolean available) {
this.available = available;
}
public double getSpeed() {
return speed;
}
public Processor(double d) {
super();
this.speed = d;
}
public Processor(String name, double d) {
this.name = name;
this.speed = d;
this.available = true;
}
public void setSpeed(double speed) {
this.speed = speed;
}
public void process(Job job) {
if(this.isAvailable()) {
System.out.println("Processor - "+this.name+" starting job - "+job.getName());
while(job.getNumberOfInstructions() > 0) {
try {
this.available = false;
System.out.println("Processor - "+this.name+" running job - "+job.getName());
Thread.sleep((long) (this.getSpeed()*1000));
job.setNumberOfInstructions(job.getNumberOfInstructions() - 1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Simulator.finishedJobs.add(job);
}
this.available = true;
System.out.println("Processor - "+this.name+" finished job - "+job.getName());
}
}
Simulator.java
package lab;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.LinkedHashSet;
public abstract class Simulator {
public Deque<Job> getJobQueue() {
return jobQueue;
}
public LinkedHashSet<Processor> getProcessors() {
return processors;
}
public void setProcessors(LinkedHashSet<Processor> processors) {
this.processors = processors;
}
public void setJobQueue(Deque<Job> jobQueue) {
Simulator.jobQueue = jobQueue;
}
private LinkedHashSet<Processor> processors;
protected static Deque<Job> jobQueue = new ArrayDeque<Job>();
protected static LinkedHashSet<Job> finishedJobs = new LinkedHashSet<Job>();
public Simulator(LinkedHashSet<Processor> processors) {
this.processors = processors;
}
public abstract LinkedHashSet<Job> getFinishedJobs();
public abstract void enqueueJob(Job newJob);
public abstract void step();
}
Output:
Processor - processor1 starting job - Job1
Processor - processor1 running job - Job1
Processor - processor1 running job - Job1
Processor - processor1 running job - Job1
Processor - processor1 running job - Job1
Processor - processor1 running job - Job1
Processor - processor1 finished job - Job1
Processor - processor2 starting job - Job2
Processor - processor2 running job - Job2
Processor - processor2 running job - Job2
Processor - processor2 running job - Job2
Processor - processor2 finished job - Job2
[lab.Job@7852e922, lab.Job@4e25154f]
Comment : you can modify the SimulatorTester to enque and deque jobs via threads, so that it will run in as you expected.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.