I need help with the Task UML Objectives: The focus of this assignment is unders
ID: 3862507 • Letter: I
Question
I need help with the Task UML
Objectives:
The focus of this assignment is understanding of the ArrayList structure and Collections utilities. Use the ArrayList and Collections classes that are defined in the java API, in the java.util package. The StringTokenizer class will also be used to parse simple Strings, this is also found in java.util.
Program Background:
The problem of optimal scheduling has many applications, from ordering processor tasks in a CPU or scheduling real life tasks in order to maximize the profit of available assets.
Program Description:
A schedulable task will be defined by having a deadline, by when it needs to be completed, and a value, which represents the importance or profit of the task. The user will input a number of these tasks and the program will schedule them such that the maximum value is achieved. Often with these scheduling problems it is not possible to schedule all tasks.
This can be done by a fairly simply Greedy Algorithm. A Greedy Algorithm is one that makes choices based upon what appears to be optimal at the time of choosing. This job scheduling algorithm is greedy because it schedules the most valuable remaining task next. So the first step is to organize our tasks in reverse order, making it easy to linearly process them.
UML DIAGRAM FOR AND DISCUSSION FOR Task
Task implements Comparable<Task>
- maxDeadline : int
- deadline : int
- value : int
<<constructor>> Task(deadline: int, value : int)
+ getDeadline( ) : int
+ setDeadline(deadline : int)
+ getValue( ) : int
+ setValue(value : int)
+ getMaxDeadline( ) : int
+ toString( ) : String
+ compareTo( t : Task) : int
An instance of Task represents a single task to be scheduled. Each task has two values, one for the deadline and one for the value of that that task. Additionally, the class has a static data member (denoted by the underline in the UML diagram). A static data member in a class shares the same value for allinstances of that class. This is necessary as we will need to keep track of the latest deadline of all Tasks so we know how big to make our time line.
Data Members:
maxDeadline – static int, must be kept up to date whenever a new Task is created
deadline – Deadline of what time step the Task must be completed by in order to be scheduled
value– Value of the Task
Methods:
Task(deadline: int, value : int) – Class constructor. Sets the non-static data members by calling upon the appropriate set methods.
getDeadline( ) : int – Returns deadline
setDeadline(deadline : int) – Attempts to set deadline data member to the int passed. If int passed is non-positive it will throw an InvalidDeadlineException. If the deadline is valid will update the value of maxDeadline if greater.
getValue( ) : int – Returns value
setValue(value : int) – Attempts to set value data member to the int passed. If int passed is non-positive it will throw an InvalidValueException.
getMaxDeadline( ) : int – Returns maxDeadline
compareTo(t : Task) : int – Override the compareTo method from implementing the Comparable interface. This determines the logical order of Tasks which will be used to sort them. Tasks with higher values should be considered greater than those with lower values.
toString( ) : String – Return a String of the following form:
“Deadline: DEADLINE Value: VALUE”, where DEADLINE and VALUE are replaced by the instances deadline and value.
UML DIAGRAM FOR AND DISCUSSION FOR Scheduler
Scheduler
- inputTasks : ArrayList<Task>
- outputTasks : Task[]
<<constructor>> Scheduler( )
+ addTask( t : Task )
+ scheduleTasks( )
+ calculateValue( ) : int
+ printInputTasks( )
+ printOutputTasks ( )
Scheduler( ) – Class constructor. Creates a new empty ArrayList of Tasks for inputTasks.
addTask(t : Task) – Adds the passed Task to the ArrayList.
scheduleTasks( ) – Implements scheduling algorithm as discussed above.
calculateValue( ) : int – Sums the total value of all Tasks that have been scheduled.
printInputTasks( ) – Prints the Task with the highest value and the Task with the lowest value (see example output for format) followed by all inputted Tasks.
printOutputTasks( ) – Prints the scheduled Tasks in the order they should be performed as determined by scheduleTasks method.
The Collections class can be used to easily perform actions on ArrayLists and other similarly defined data structures. In particular, you will need to use sort, reverse, max, and min.
Task implements Comparable<Task>
- maxDeadline : int
- deadline : int
- value : int
<<constructor>> Task(deadline: int, value : int)
+ getDeadline( ) : int
+ setDeadline(deadline : int)
+ getValue( ) : int
+ setValue(value : int)
+ getMaxDeadline( ) : int
+ toString( ) : String
+ compareTo( t : Task) : int
Explanation / Answer
I have designed prototype for the problem description using java program
*******************************
Task.java
package com.sql.acadgild;
public class Task implements Comparable<Task>{
//Static and public fields
static int maxDeadline;
int deadline;
int value;
public Task() {
// TODO Auto-generated constructor stub
}
public int compareTo(Task arg0) {
// TODO Auto-generated method stub
return 0;
}
//Public Constructor
public Task(int deadline, int value) {
super();
this.deadline = deadline;
this.value = value;
}
//Pubic getters and setters
public static int getMaxDeadline() {
return maxDeadline;
}
public static void setMaxDeadline(int maxDeadline) {
Task.maxDeadline = maxDeadline;
}
public int getDeadline() {
return deadline;
}
public void setDeadline(int deadline) {
this.deadline = deadline;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
@Override
public String toString() {
return "Task [deadline=" + deadline + ", value=" + value + "]";
}
}
*****************************************************
Sceduler.java
package com.sql.acadgild;
import java.util.ArrayList;
public class Scheduler {
ArrayList<Task> task=new ArrayList<Task>();
int totaltask=0;
int taskHigh=0;
int taskLow=0;
public Scheduler() {
// TODO Auto-generated constructor stub
}
//Constructor
public Scheduler(ArrayList<Task> task) {
super();
this.task = task;
}
void addTask(Task task){
System.out.println("You're task has been added");
}
boolean scheduleTasks(){
System.out.println("Sceduling Tasks is done here");
return false;
}
int calculateValue(){
System.out.println("Summing all the total value of all the tasks");
return totaltask;
}
void printInputTasks( ) {
System.out.println("Task with highest Value"+taskHigh);
System.out.println("Task with the lowest value:"+taskLow);
}
void printOutputTasks( ){
System.out.print("Scheduled tasks"+Scheduler.this.scheduleTasks());
}
}