Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help implementing these methods in a larger scale program. public class Task extends java.lang.Object implements java.lang.Comparable A class to represent a single task

I need help implementing these methods in a larger scale program.

public class Task

extends java.lang.Object

implements java.lang.Comparable

A class to represent a single task to be scheduled. Contains the task's deadline and value. The Task class also holds a static value that holds the largest deadline of all Tasks created.

Constructor Detail

Task

 public Task(int deadlineIn, int valueIn) 

Task Constructor; sets the deadline and value of the Task.

Parameters:

deadlineIn - Deadline of the Task.

valueIn - Value of the Task.

Throws:

InvalidDeadlineException

InvalidValueException

-------------------------------------------------------------------------------------

Method Detail

setDeadline

public void setDeadline(int deadlineIn) 

Sets the deadline of the Task. Deadlines have a minimum value of 1. This also ensures that the overall maximum deadline for all Tasks is updated if the new deadline is greater than the previous maximum.

Parameters:

deadlineIn - Desired deadline of this Task.

Throws:

InvalidDeadlineException - If an invalid deadline is given as a parameter value.

-------------------------------------------------------------------------------------

setValue

public void setValue(int valueIn) 

Sets the value of the Task.

Parameters:

valueIn - Desired value of this Task.

Throws:

InvalidValueException - If the parameter valueIn is less than 1

-------------------------------------------------------------------------------------

getDeadline

public int getDeadline() 

Retrieves the deadline for this Task.

Returns:

The Task's deadline.

getValue

public int getValue() 

Retrieves the value for this Task.

Returns:

The Task's value.

-------------------------------------------------------------------------------------

getMaxDeadline

public static int getMaxDeadline() 

Retrieves the maximum deadline across all Task objects that have been created thus far.

Returns:

The maximum deadline of all Tasks. 0 if no Tasks have yet been created.

-------------------------------------------------------------------------------------

compareTo

public int compareTo(Task t) 

Compares this Task to a passed Task, determining logical order. Order is determined by the Tasks' value. Lower valued Tasks are considered less than the other, higher valued Tasks are considered greater than the other, Tasks with the same value are equal.

Specified by:

compareTo in interface java.lang.Comparable

Parameters:

t - Incoming Task to be compared with the calling Task.

Returns:

Returns an int less than 0 if the local Task is less than the passed Task, an int greater than 0 if the local Task is greater than the passed Task, and a 0 if the Tasks are equal.

-------------------------------------------------------------------------------------

toString

public java.lang.String toString() 

Generates and returns a String representation of the Task. The String is in the form: "Deadline: DEADLINE Value: VALUE", where DEADLINE and VALUE are replaced by the Task's deadline and value.

Overrides:

toString in class java.lang.Object

Returns:

A String of this Task.

-----------------------------------------

public class Scheduler

extends java.lang.Object

Implements a greedy scheduling algorithm that maximizes value or "profit". Intended use is to accept an undefined amount of Tasks, then call upon scheduleTasks to perform the algorithm. The total profit of the scheduled Tasks can then be found by calling upon calculateValue.

Constructor Detail

Scheduler

public Scheduler()

Constructs a new Scheduler instance that is ready for Tasks to be added, but has no pre-existing valid schedule of Tasks.

-------------------------------------------------------------------------------------

Method Detail

addTask

public void addTask(Task t)

Adds a Task to be considered by this Scheduler. Adding a Task will also invalidate any previously computed schedule by this Scheduler.

Parameters:

t - The Task to be considered for possible scheduling.

-------------------------------------------------------------------------------------

scheduleTasks

public void scheduleTasks()

Implements a greedy scheduling algorithm for the Tasks which have been added thus far to this Scheduler. Tasks are prioritized based on decreasing value and are scheduled in timeslots as close to their deadline as possible (but not exceeding it), without overwriting an already scheduled Task. The method assumes that each Task takes only 1 timeslot to complete.

Note, the total number of timeslots should correspond to the maximum deadline for all tasks. Some timeslots may be unused in the schedule, and not all Tasks are guaranteed to be scheduled depending on conflicting deadlines and available resources.

Upon completing this method, a schedule is considered valid if one or more tasks were successfully scheduled. The scheduling of Tasks should not affect the order of the original Task list.

-------------------------------------------------------------------------------------

getSchedule

public Task[] getSchedule()

Retrieves an array of Tasks corresponding to the most recently computed schedule by this Scheduler. Each array position corresponds to a single timeslot in the schedule, and the total number of timeslots should correspond to the maximum deadline of all Tasks created to present.

Returns:

The array of Tasks corresponding to the computed schedule.

Throws:

InvalidScheduleException - if no valid schedule has been computed yet

-------------------------------------------------------------------------------------

calculateValue

public int calculateValue()

Sums the values of all actively scheduled Tasks, resulting in total profit for the determined schedule. Idle timeslots are ignored.

Returns:

Total profit of the scheduled Tasks.

Throws:

InvalidScheduleException - if no valid schedule has been computed yet

-------------------------------------------------------------------------------------

allTasksString

public java.lang.String allTasksString()

Constructs and returns a formatted String reporting the contents of the list of tasks that have been added for consideration by this Scheduler.

String is of the form: "Task with Max value: MAX_TASK Task with Min Value: MIN_TASK " followed by all Tasks on a line of their own in the order in which they were added to this Scheduler object. MAX_TASK and MIN_TASK are the Tasks with the maximum and minimum value, respectively.

Returns:

String of all inputed Tasks.

Throws:

java.util.NoSuchElementException - when no Tasks have been added yet

-------------------------------------------------------------------------------------

scheduleResultString

public java.lang.String scheduleResultString()

Constructs and returns a formatted String reporting the contents of the computed schedule array. String is of the form: "Scheduled Tasks: ", followed by all Tasks in the computed schedule on a line of their own. If a slot in the schedule array contains no Task, the row should report "IDLE" instead of a task.

Returns:

String of all scheduled Tasks.

Throws:

InvalidScheduleException - if no valid schedule has been computed yet

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

What steps can organizations take to foster learning? LO2

Answered: 1 week ago

Question

What is meant by 'Wealth Maximization ' ?

Answered: 1 week ago