Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

Assignment_3.pdf CSCI 2125: Assignment 3 Problem Description In this homework, you will use the priority queue to simulate scheduling of tasks. 1.1. Input and Output Startup.java works as the starting...

1 answer below »
Assignment_3.pdf
CSCI 2125: Assignment 3

Problem Description

In this homework, you will use the priority queue to simulate scheduling of tasks.

1.1. Input and Output

Startup.java works as the starting point of the program, which takes as argument
the name of an input file. So, the program can be executed using commands as follows:

java Startup input.txt
java Startup in.txt

Each non-blank line in the input file contains a comment or specification of a
task. If a line starts with
, the line should be interpreted as a comment line. A task’s
specification is composed of three parts, separated by a space, and looks like as follows:

task-name deadline duration

A task-name is represented using String, while the deadline and duration are long
values. Table 1 presents content of a sample input file. It doesn’t really matter what the
units of time are in this file. If it helps, you may consider the unit to be second. The
execution of the tasks starts at 00 time (can be interpreted as midnight). The deadline is
the unit elapsed time since 00 time.













Each task-specification line tells your program to insert the co
esponding item
into the priority queue, where priority co
esponds to its deadline. Tasks with earlier
(smaller) deadlines have higher priorities, and should be executed first – this is called
Earliest Deadline First scheduling, and is often used in real-time systems. You can read
about it in wikipedia
(http:
en.wikipedia.org/wiki/Earliest_deadline_first_scheduling), if interested.

The first task (i.e., highest priority task) starts executing at 00 time. Tasks
execute sequentially one after another (not in parallel) from highest priority to the
lowest priority. As a task completes, it is considered that the time required for that task
has also elapsed.

A task finishing after its prescribed deadline produces a lag, which is the
difference between the task completion time and its prescribed deadline. The program
computes and reports the total lag over all tasks, which is the summation of lags for all
the late-completed tasks. There is no negative lag if a task completes before its deadline.
As each task completes, its start-time, completion time, and any produced lag is
eported to the terminal. A sample output (co
esponding to the input in Table 1) is
presented in Table 2.

1.2. Source Files

Majority of the source code necessary for this homework is already written for
you. You need to download the following java source files from moodle.

• Startup.java: starting point of the program.
• FileInputReader.java: reads input file.

• Task.java: represents a task.

• Scheduler.java: schedules the tasks.
• HardWorker.java: actually performs a task.

A necessary implementation of UnboundedPriorityQueue is missing. You have to
provide a clean implementation of an UnboundedPriorityQueue, for which you will use
(by composition) a MinHeap. Your Implementation of UnboundedPri- orityQueue must
NOT be restricted/fixed in a particular size/capacity and it must seamlessly work with
est of the program provided. You must NOT modify any source code other than your
own implementation of UnboundedPriorityQueue, Min- Heap, and co
esponding test
code.
The relationships among the classes and their contents/members are shown in the UML
diagram in Figure 1. Notice that MinHeap is described to have a number of overloaded
constructors. You don’t have to provide all those constructors. It will be sufficient to
have only the constructors necessary for the entire simulation program (and your test
code) to work.

1.3. Test Code

Your implementation of MinHeap and UnboundedPriorityQueue must be
accompanied by JUnit test code written in source files exactly named as
TestMinHeap.java and TestUnboundedPriorityQueue.java respectively. Consider all
corner cases, in which your implementation may fail, and deal with them in your tests.
Include enough test cases to make sure that your UnboundedPriorityQueue and
MinHeap function co
ectly.
For more about JUnit testing, please see the instructions available on moodle:

https:
moodle.uno.edu/pluginfile.php/2431551/mod_resource/content/1/Junit_testin
g.pdf

1.4. ReadMe.txt

When the description of the assignment does not explicitly specify something,
you have the liberty to make your choices in those scenarios. Please mention any design
choices you made including any reasoning behind your choices in a ReadMe.txt file. If
you could not complete a certain part co
ectly, mention it in ReadMe.txt. If you did not
find anything to include in the file, simply put your name and email address.




























2. Clean Code
Make sure you have produced clean code as discussed in the first class. Please see the
general guidelines for producing clean code available on moodle:
https:
moodle.uno.edu/pluginfile.php/2431549/mod_resource/content/1/CleanCodeI
nstructions_java.pdf
3. Deadline
November 22nd 2020 midnight (Hard Deadline)
*Late submission is permitted till 25th November midnight with a penalty of losing 25%
every day. After that, submission will be locked.
4. Submission
Keep all your files in a folder and zip it with the following format –
Firstname_Lastname.zip
Upload the zipped file in moodle before the deadline.

• Please be advised that, there is no option of new file submission after the final
deadline. Also, no update is permitted in graded work. Make sure you have
submitted the right source file.
srcForStudents.zip
__MACOSX/._srcForStudents
srcForStudents/Scheduler.java
srcForStudents/Scheduler.java
import java.util.List;
public class Scheduler{
    UnboundedPriorityQueue queue;
    public Scheduler(){
        queue = new UnboundedPriorityQueue
();
    }
    public void scheduleTasks(List taskList){     
        for(Task task: taskList){
            queue.enqueue(task);
        }
    }
    public long dispatchTasks(){
        long startTime = 0;
        long completionTime = 0;
        long totalLag = 0;
        Task task = null;
        while(!queue.isEmpty()){
            task = queue.dequeue();
            completionTime = HardWorker.completeTask(task, startTime);
            if(completionTime > task.getDeadline()){
                totalLag += completionTime - task.getDeadline();
            }
            startTime = completionTime;
        }
        return totalLag;
    }
}
__MACOSX/srcForStudents/._Scheduler.java
srcForStudents/HardWorker.java
srcForStudents/HardWorker.java
public class HardWorker{
    public static long completeTask(Task task, long startTime){
        long endTime = startTime + task.getDuration();
        
        System.out.printf("[%4d] %s STARTED\n", startTime, task.getName());
        System.out.printf("[%4d] %s ENDED", endTime, task.getName());
        
        if(endTime > task.getDeadline()){
            System.out.println(", behind schedule by " + (endTime - task.getDeadline()));
        }else{
            System.out.println(", completed in time");
        }
        return startTime + task.getDuration();
    }
}
__MACOSX/srcForStudents/._HardWorker.java
srcForStudents/input.txt
task-name deadline duration
uild-project XXXXXXXXXX
unit-tests XXXXXXXXXX
integration-test XXXXXXXXXX
egression-test XXXXXXXXXX
__MACOSX/srcForStudents/._input.txt
srcForStudents/Task.java
srcForStudents/Task.java
public class Task implements Comparable{  
    private String name;
    private Long duration;
    private Long deadline;
    public Task(String name, long deadline, long duration){
        this.name = name;       
        this.duration = duration;
        this.deadline = deadline;
    }
    public long getDeadline(){
        return this.deadline.longValue();
    }
    public long getDuration(){
        return this.duration.longValue();
    }
    public String getName(){
        return this.name;
    }
    @Ove
ide
    public int compareTo(Task other){
        return this.deadline.compareTo(other.getDeadline());
    }
    @Ove
ide
    public String toString(){
        return "Name:" + name + ", duration:" + duration 
                        + ", deadline:" + deadline; 
    }
}
__MACOSX/srcForStudents/._Task.java
srcForStudents/FileInputReader.java
srcForStudents/FileInputReader.java
import java.util.List;
import java.util.A
ayList;
import java.io.*;
public class FileInputReader{
    private static final String COMMENT_LINE_MARKER = "
";
    public static List readTasksInfo(String filePath){
        BufferedReader reader = null;
        List taskList = new A
ayList
();
        try{
            reader = new BufferedReader(new FileReader(new File(filePath)));
        }catch(FileNotFoundException e){
            e.printStackTrace();
        }
        try{
            String line = null;
            while((line = reader.readLine())!= null){               
                if(line.startsWith(COMMENT_LINE_MARKER)) continue;
                
System.out.println(line);
                Task task = parseLine(line.trim());             
                taskList.add(task);             
            }
            
 close the file
            reader.close();
        }catch(IOException e){
            e.printStackTrace();
        }
        return taskList;
    }
    private static Task parseLine(String line){
        String[] values = line.split(" ");
        if(values.length < 3){
            throw new IllegalArgumentException("Description of a task in the input file does not have three required parts: " + line);
        }
        return new Task(values[0], Long.parseLong(values[1]), Long.parseLong(values[2]));
    }
}
__MACOSX/srcForStudents/._FileInputReader.java
srcForStudents/Startup.java
srcForStudents/Startup.java
import java.util.List;
public class Startup{
    public static void usage(){
        System.out.println("Usage: ");
        System.out.println("\t java Startup ");
    }
    public static void main(String[] args){
        if(args.length == 0){
            System.out.println("Missing required parameter!");
            usage();
            return;
        }
        List tasksList = FileInputReader.readTasksInfo(args[0]);
        
        Scheduler scheduler = new Scheduler();
        scheduler.scheduleTasks(tasksList);
        long totalLag = scheduler.dispatchTasks();
        System.out.println("Total lag (behind expected schedule): " + totalLag);
    }
}
__MACOSX/srcForStudents/._Startup.java
Answered Same Day Nov 11, 2021

Solution

Abr Writing answered on Nov 15 2021
144 Votes
src/FileInputReader.java
src/FileInputReader.java
import java.util.List;
import java.util.A
ayList;
import java.io.*;
public class FileInputReader{
    private static final String COMMENT_LINE_MARKER = "
";
    public static List readTasksInfo(String filePath){
        BufferedReader reader = null;
        List taskList = new A
ayList
();
        try{
            reader = new BufferedReader(new FileReader(new File(filePath)));
        }catch(FileNotFoundException e){
            e.printStackTrace();
        }
        try{
            String line = null;
            while((line = reader.readLine())!= null){               
                if(line.startsWith(COMMENT_LINE_MARKER)) continue;
                
System.out.println(line);
                Task task = parseLine(line.trim());             
                taskList.add(task);             
            }
            
 close the file
            reader.close();
        }catch(IOException e){
            e.printStackTrace();
        }
        return taskList;
    }
    private static Task parseLine(String line){
        String[] values = line.split(" ");
        if(values.length < 3){
            throw new IllegalArgumentException("Description of a task in the input file does not have three required parts: " + line);
        }
        return new Task(values[0], Long.parseLong(values[1]), Long.parseLong(values[2]));
    }
}
src/HardWorker.java
src/HardWorker.java
public class HardWorker{
    public static long completeTask(Task task, long startTime){
        long endTime = startTime + task.getDuration();
        
        System.out.printf("[%4d] %s STARTED\n", startTime, task.getName());
        System.out.printf("[%4d] %s ENDED", endTime, task.getName());
        
        if(endTime > task.getDeadline()){
            System.out.println(", behind schedule by " + (endTime - task.getDeadline()));
        }else{
            System.out.println(", completed in time");
        }
        return startTime + task.getDuration();
    }
}
src/input.txt
task-name deadline duration
uild-project 1220 20
unit-tests 1230 2300
integration-test 1359 300
egression-test 1410...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here