首页 > 其他分享 >JC1503 Object-OrientedProgramming

JC1503 Object-OrientedProgramming

时间:2023-05-11 13:11:06浏览次数:31  
标签:queue tasks Object OrientedProgramming class priority task JC1503 method


University of Aberdeen
School of Natural and Computing Sciences
Department of Computing Science
2022 – 2023
Programming assignment – Individually Assessed (no teamwork)
Deadline: 22:00, 16th May 2023 (SCNU local time)
Title: JC1503 – Object-Oriented
Programming
Note: This assignment accounts for 30% of your total mark
of the course (Code and report will account for 30% and
70% of the whole assessment, respectively).
Information for Plagiarism: The source code and your report will be submitted for plagiarism
check (e.g., Turnitin or other tools). Any AI tools which automatically help to edit, paraphrase,
or generate your source code and report are not allowed. If those behaviours are detected, your
submissions will be classified as plagiarism. Please refer to the slides available at MyAberdeen for
more information about avoiding plagiarism before you start working on the assessment. Please
also read the following information provided by the university:
https://www.abdn.ac.uk/sls/online-resources/avoiding-plagiarism/
page 2 of 10
Introduction
In this programming assignment, you will design and implement a Task Scheduler in Python,
which is a program that helps manage and prioritise tasks for efficient and timely completion. To
achieve this, you will use the Priority Queue data structure and Object-Oriented Programming
principles. The Priority Queue is an abstract data type that stores and retrieves items based on
their priority. Unlike a regular queue that follows a first-in-first-out (FIFO) data structure, a
priority queue serves items based on their priority. In the Task Scheduler implementation, the
Priority Queue will manage tasks by sorting them according to their priorities and deadlines. Each
task will be defined as an object with attributes such as description, priority, and deadline, which
can be added to the priority queue and executed based on their priority and deadline order.
For instance, let's say you have the following tasks that require completion:
• Complete project report - Priority 2, Deadline: May 1, 2023
• Buy birthday gift for a friend - Priority 3, Deadline: April 30, 2023
• Attend job interview - Priority 4, Deadline: April 28, 2023
• Renew gym membership - Priority 1, No Deadline
The Task Scheduler will add the above tasks to the priority queue based on their priority and
deadline. The queue will have the highest priority (greatest priority number) and earliest deadline
tasks at the top. The order will be as follows:
1. Attend job interview - Priority 4, Deadline: April 28, 2023
2. Buy birthday gift for a friend - Priority 3, Deadline: April 30, 2023
3. Complete project report - Priority 2, Deadline: May 1, 2023
4. Renew gym membership - Priority 1, No Deadline
When executing the tasks, the Task Scheduler will begin with the task that has the highest priority
and earliest deadline,  代做寿JC1503 which is attending the job interview on April 28, 2023. After completing this
task, it will move on to the next task in the queue, which is to buy the birthday gift for the friend
by April 30, 2023. In cases where there are tasks with the same priority, the task scheduler will
prioritise the task with the earliest deadline to ensure that it is completed in a timely manner. To
provide flexibility in task management, the implementation should also allow the user to modify a
task's priority or deadline. This involves removing the task from the queue, making the necessary
updates to its priority and/or deadline, and then reinserting it back into the queue in the
appropriate position based on its new priority and deadline.

任务调度器将根据优先级将上述任务添加到优先级队列中,并且

最后期限队列将具有最高优先级(最高优先级数)和最早截止日期

顶部的任务。顺序如下:

1.参加面试-优先事项4,截止日期:2023年4月28日

2.给朋友买生日礼物-优先级3,截止日期:2023年4月30日

3.完成项目报告-优先级2,截止日期:2023年5月1日

4.续订健身房会员资格-优先事项1,无截止日期

执行任务时,任务调度器将从优先级最高的任务开始

以及最早截止日期,即参加2023年4月28日的面试。完成此操作后

任务,它将转到队列中的下一个任务,即为朋友购买生日礼物

到2023年4月30日。在存在具有相同优先级的任务的情况下,任务调度程序将

在最早的截止日期前对任务进行优先级排序,以确保任务及时完成。到

提供任务管理的灵活性,实现还应允许用户修改

任务的优先级或截止日期。这包括从队列中删除任务

更新到其优先级和/或截止日期,然后将其重新插入

根据新的优先事项和截止日期采取适当的立场。


Guidance and Requirements
For this assessment, you will be required to complete coding tasks and a final written report that
outlines the steps you took. To help you get started, we will provide you with a template code and
a template report. It is important that your assessment code and report follow the designated
structure and include all required content as outlined in each section. Marks are allocated for each
subtask, and your written report should contain all distinct sections/subtasks that provide a
comprehensive and reflective account of the processes you undertook.
page 3 of 10
To test your implementation, the project template includes four tests. Upon running the test.py
file by command python3 test.py, you should expect all four tests to fail initially, indicating that
your implementation is not yet correct.
Once you have completed all the steps, after running python3 test.py again, you should see the
following, if you have implemented the assignment correctly:
page 4 of 10
Note that not all tasks have tests defined. Make sure you mention in the report which tasks you
have completed.
In the later stages, you can use the command python3 main.py check the console output.
A detailed description of each task is given below. To complete these steps, follow the instructions
and ensure you use the provided template. All processes taken to run the final project must be
clearly described. For submission instructions refer to the later sections.
**Please read all the information below carefully**
Step 1: Design and Implement the Task Class (30 points)
The Task class is a blueprint for creating task objects, each with a description, priority level, and
deadline. It provides methods to access and modify these attributes and a string representation of
the object. Complete the class implementation following the instructions below.
1.1) Implement the __init__() method in the Task class, which should be invoked when an object of
a class is created. The __init__() method takes three parameters: description, priority, and deadline.
Its purpose is to initialise three instance variables (__description, __priority, and __deadline) with
the values passed as arguments.
1.2) Implement the getter and setter methods for the instance variables __description, __priority,
and __deadline in the Task class. These getter and setter methods allow us to access and modify
the instance variables of a Task object from outside the class while also maintaining encapsulation
by keeping the variables private and only allowing access through these methods.
For instance, the get_description() method is a getter method that returns the current value of the
__description instance variable. The set_description() method is a setter method that sets the value
of the __description instance variable to the value passed as an argument description. Similarly, the
get_priority() method returns the current value of the __priority instance variable, and the
set_priority() method sets the value of the __priority instance variable to the value passed as an
argument priority. The get_deadline() method returns the current value of the __deadline instance
variable, and the set_deadline() method sets the value of the __deadline instance variable to the
value passed as an argument deadline.
1.3) The __str__() method is a built-in special method in Python that gets called when we try to
print an object, which normally returns a string representation of the object. Implement the
method __str__() in the Task class. The method should return a string that includes the description,
priority, and deadline of the task, separated by commas and labelled with their corresponding
names.
page 5 of 10
Step 2: Design and Implement the PriorityQueue Class (30 points)
This is a class definition for a priority queue data structure. A priority queue is a collection of tasks
where each task is assigned a priority and a deadline. Complete the class implementation following
the instructions below.
2.1) Implement the __init__() method in the PriorityQueue class. This method should initialise an
instance variable __tasks as an empty collection using an appropriate data structure underneath.
2.2) Implement the functionality of the add_task() in the PriorityQueue class. It should take a Task
object as input and adds it to the collection of tasks __tasks. After adding the task, the collection is
sorted in decreasing order of priority and increasing order of deadline.
Hint: The collection can be sorted by passing a key function to the sort() method. The key function
should return a tuple with two values: the negative priority and the deadline of each task. The
negative priority is used to sort the tasks in descending order, because the default sorting order is
ascending. You can define the key function inside the add_task() method using a lambda function,
or you can define it outside the method as a helper function and pass it as the key parameter to the
sort() method.
2.3) Implement the remove_task() and peek_task() methods in the PriorityQueue class. The
remove_task() method removes and returns the task with the highest priority and earliest deadline
from the front of the tasks collection, while the peek_task() method returns the task with the
highest priority and earliest deadline from the front of the tasks collection without removing it.
Both methods check if the tasks collection is not empty first, and return None if it's empty.
2.4) Implement the get_tasks() methods in the PriorityQueue class, which simply returns the
collection of tasks.
Step 3: Design and Implement the Scheduler Class (30 points)
This is a class definition for a scheduler that uses a priority queue to manage a collection of tasks.
3.1) Implement the __init()__ method in the Scheduler Class. It creates an instance variable named
__queue. This variable is an object of the PriorityQueue class that is utilised to store tasks. Initially,
self.__queue is assigned an empty instance of PriorityQueue, which does not contain any tasks.
3.2) Implement the add_task() and remove_task() methods in the Scheduler Class. The add_task()
method adds a new task to the priority queue, held in self.__queue, by calling the add_task()
method of the PriorityQueue class. On the other hand, the remove_task() method removes the
highest priority task from the priority queue held in self.__queue and returns it. This is
accomplished by calling the remove_task() method of the PriorityQueue class. Together, these
methods provide the fundamental functionality required for managing tasks in the Scheduler class.
Now, if you run the test.py file by command python3 test.py. It should result in one successful test
out of the four tests (as shown in the screenshot), indicating that the implementation can
successfully add and remove tasks. However, there are still three more errors that need to be fixed
in the subsequent steps.
page 6 of 10
3.3) Implement the reorder_task() method in the Scheduler Class to modify the priority and
deadline of a task. This method takes a task, a new priority, and a new deadline as input. First, it
searches for the given task in the priority queue. If the task cannot be found, the method continues
execution without changing the queue. If the task is found, it is removed from the queue using the
get_tasks() method of the PriorityQueue class. The method then sets the new priority and deadline
using the set_priority() and set_deadline() methods of the Task class. Finally, it adds the modified
task back to the priority queue using the add_task() method of the PriorityQueue class.
Now, if you run the test.py file by using the command python3 test.py, you should expect one more
test to pass (refer to the screenshot below). This indicates that your implementation can
successfully modify the priority and deadline of a specific task using the 'reorder_task()' method,
and then reorder the tasks in the priority queue. However, there are still two more errors that need
to be fixed in the later steps.
page 7 of 10
3.4) Implement the execute_task() method in the Scheduler Class, which removes and prints the
task with the highest priority and earliest deadline from the front of the priority queue using the
remove_task() method of the __queue instance variable. If there are no tasks in the queue, it prints
"No tasks to execute".
Now, if you run the test.py file by using the command python3 test.py, you should expect one more
test to pass (refer to the screenshot below). This indicates that your implementation can
successfully execute the task with the highest priority and earliest deadline from the front of the
priority queue. There are one more error that need to be fixed in the later step.
3.5) Implement the display_tasks() method in the Scheduler Class, which displays the current tasks
in the priority queue. It first gets the collection of tasks using the get_tasks() method of the __queue
page 8 of 10
instance variable. If there are tasks in the queue, it prints "Current tasks:" followed by each task
using a for loop. If there are no tasks in the queue, it prints "No tasks".
Now, if you run the test.py file by using the command python3 test.py, you should expect all the
four tests being passed successfully (refer to the screenshot below).
Step 4: Demonstrate the Functionality by main function (10 points)
Use if __name__ == '__main__': and create a main function that starts your program. The following
functionality should be implemented:
4.1) Create an instance of the Scheduler class
4.2) Create three instances of the Task class, with different descriptions, priorities, and deadlines
as below.
• task1: Finish project, priority 3, 1st May 2023
• task2: Exam revision, priority 2, 1
st July 2023
• task3: Buy groceries, priority 1, None
4.3) Call the add_task() method of the Scheduler class to add the three tasks to the priority queue.
4.3) Call the display_task() method to display the current tasks in the priority queue.
4.4) Call the execute_task() method to execute the task with the highest priority and earliest
deadline. Then, display the current tasks in the priority queue by calling the display_tasks() method
to show the updated collection of tasks in the priority queue. Check the output to verify the
correctness of the implementation.
4.5) Use the reorder_task() method to modify the priority and deadline of a task. For example, to
change task3 from priority 1 with no deadline to priority 4 with a deadline of May 10th, 2023, call
the reorder_task() method on the scheduler object and pass in task3, the new priority value of 4,
and the new deadline of May 10th, 2023. After modifying the task, display the current tasks in the
priority queue again using the display_tasks() method to show the updated collection of tasks in
the priority queue.
4.6) Continuously execute the task with the highest priority and earliest deadline from the front of
the priority queue using the execute_task() method of the Scheduler class, until there are no more
tasks left in the queue.
Step 5: Write the report (100 points)
Your report should be structured according to the template provided below.
page 9 of 10
1. Introduction (5 points, 100-150 words)
• Explain the purpose of the Task Scheduler (2 points). This can include what problem it aims
to solve and why it is important.
• Describe the main features and functionality of the Task Scheduler (3 points). This can
include what it can do, how it works, and any limitations it may have.
2. Task Class Design (20 points, 200-250 words)
• Explain the attributes of the Task class (6 points). This can include what information is stored
for each task.
• Describe the methods of the Task class, their purpose, and implementation (10 points).
This can include how each method works and what it is used for.
• Discuss encapsulation and how it is achieved in the Task class (4 points). This can include
how information is hidden from other parts of the code and why this is important.
3. PriorityQueue Class Design (25 points, 300-350 words)
• Explain the choice of the priority queue data structure used in the PriorityQueue class (6
points). This can include why this data structure was chosen and how it works.
• Describe the attributes of the PriorityQueue class (4 points). This can include what
information is stored in the priority queue.
• Explain the methods implemented for adding, removing, and peeking at tasks, along with
their purpose and implementation (10 points). This can include how each method works
and what it is used for.
• Discuss how sorting tasks based on deadlines as a secondary criterion is achieved (5
points). This can include how the priority queue sorts tasks and why sorting by deadline is
important.
4. Scheduler Class Design (25 points, 300-350 words)
• Explain how the Scheduler class utilises the PriorityQueue class (6 points). This can include
how the priority queue is used to manage tasks and prioritise them.
• Describe the attributes of the Scheduler class (4 points). This can include what information
is stored in the scheduler.
• Explain the methods implemented for adding, removing, reordering, and executing tasks,
along with their purpose and implementation (10 points). This can include how each
method works and what it is used for.
• Discuss any design choices made in the Scheduler class and their reasoning (5 points). This
can include why certain methods were implemented and how they contribute to the
overall functionality of the Task Scheduler.
5. Testing and Demonstration (20 points, 200-250 words)
• Describe the test program and main function used to demonstrate the Task Scheduler's
functionality (8 points). This can include how users can interact with the Task Scheduler
and what they can do with it.
• Provide sample input/output to show the Task Scheduler's performance (8 points). This
can include examples of tasks being added, removed, and executed.
page 10 of 10
• Discuss any challenges faced during testing and how they were resolved (4 points). This
can include any issues encountered during testing and how they were fixed.
6. Conclusion (5 points, 100-150 words)
• Summarise the project and its main features (2 points).
• Reflect on the learning experience and the application of OOP concepts (2 points). This can
include what you learned from building the Task Scheduler and how it helped you
understand OOP concepts.
• Suggest possible improvements or future enhancements (1 point). This can include any
features or functionalities that could be added to improve the Task Scheduler.
Useful Information
• Please describe and justify each step that is needed to reproduce / run your work by using
clear descriptive writing, supporting code-snippets and screenshots. When using
screenshots please make sure they are clearly readable.
• If you use open source code, you must point out where it was obtained from (even if the
sources are online tutorials or blogs) and detail any modifications you have made to it in
your tasks. You should mention this in both your code and report. Failure to do so will result
in zero marks being awarded on related (sub)tasks
Marking Criteria
• Quality of the source code, including the commenting of the code
• Technical correctness of the code, and structure.
• Reproducibility. How easy is it for the course coordinator to repeat your work based on
your report and code?
• Quality of the report, including structure, clarity, and brevity
Submission Instructions
You should submit a PDF version of your report along with your code. The name of the PDF file
should have the form “JC1503_<your Class programme>_<your Surname>_<your
Firstname>_<Your Student ID>”. For instance, “JC1503_CS_Smith_John_4568985.pdf”, where
4568985 is your student ID, and your class programme should be selected from AI, CS or BMIS.
It is your responsibility to ensure that your code runs in Codio. If you do it on another device, and
then move it to Codio, you need to ensure it runs there, and that it runs as expected.
You should submit your code and the report using Codio and MyAberdeen portal, respectively.
Your Codio submission should consist of two files: main.py, which contains your implementation,
and test.py, which contains the test cases provided in the code template to validate the
functionality of your implementation.
Any questions pertaining to any aspects of this assessment, please address them to one of the
module coordinators.

   WX:codehelp

标签:queue,tasks,Object,OrientedProgramming,class,priority,task,JC1503,method
From: https://www.cnblogs.com/wolfjava/p/17390758.html

相关文章

  • app逆向之安卓native层安全逆向分析(七):unidbg自尝试某潮流app+dvmObject[]处理
    前言跟着龙哥搞了几次unidbg了,这次也自己尝试用来分析下某潮流app了。分析1.抓包先抓个包 我们要搞的就是这个sign-v1了。  2.调试找参数jadx一顿分析,一搜: 搜出来还不少,往下翻,找找一些特征,很快找到这里 点进去    ok,用objectionhook之后,发现不是......
  • requests标头在json序列化时报错TypeError: Object of type CaseInsensitiveDict is n
    requests的作者似乎为了解决header里大小写兼容的问题,而创建了大小写不敏感的数据结构CaseInsensitiveDict,具体分析可以参见:详解Requests中的数据结构CaseInsensitiveDict。requests返回的response_header即是一个CaseInsensitiveDict类型,而且我们知道response_header里通常并非......
  • Method com/mysql/jdbc/JDBC4ResultSet.getObject(Ljava/lang/String;Ljava/lang/Clas
      mybatis-plus生成的日期类型默认是localdatetime,数据库是datetime,按道理转换应该可以,我又不想把实体类转换成date查看依赖<--locadate/locadatetime的时间依赖--><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-ty......
  • GSON -- convert java object to json
    Library:gson(2.2.4) <dependency><groupId>com.google.code.gson</groupId><artifactId>gson</artifactId><version>2.2.4</version></dependency>  (1)SimpleJava“POJO”/JSON packagecom.hmkco......
  • Object类
    Object类Object类的常见方法有哪些?Object类是一个特殊的类,是所有类的父类。它主要提供了以下11个方法:/***native方法,用于返回当前运行时对象的Class对象,使用了final关键字修饰,故不允许子类重写。*/publicfinalnativeClass<?>getClass()/***native方法,......
  • 字面量,Object新增api,面向过程和面向对象,类构造函数
    /*字面量速写:如果属性名和形参名一致,可以直接写形参名方法速写:省略冒号和functionsayHello(){}//实际上是sayHello:function(){}计算属性名通过[]计算出来*///方法速写//字面量速写://functionsan(a,b,c,d){//constsayHello=function(){//......
  • Java之Object类
    1、Object类概述 Object类存储在java.lang包中,是所有java类(Object类除外)的终极父类(可以在代码中明确地写出声明要“继承Object类”,没有任何错误)。当然,数组也继承了Object类。但是,接口是不继承Object类的。接口只是是抽象类的延伸,可以将它看做是纯粹的抽象类,接口中的所有方法,......
  • TypeError: 'numpy.float64' object cannot be interpreted as an integer
    报错内容:Traceback(mostrecentcalllast):File"C:\Users\xuan\.conda\envs\pytorch1-6\lib\site-packages\scipy\sparse\_sputils.py",line225,inisintlikeoperator.index(x)TypeError:'numpy.float64'objectcannotbeinterpre......
  • 一统天下 flutter - widget 自定义: 通过 SingleChildRenderObjectWidget 实现自定义
    源码https://github.com/webabcd/flutter_demo作者webabcd一统天下flutter-widget自定义:通过SingleChildRenderObjectWidget实现自定义组件示例如下:lib\widget\custom\single_child_render_object_widget.dart/**通过SingleChildRenderObjectWidget实现自......
  • ConditionObject源码分析
    ConditionObject是AbstractQueuedSynchronizer(AQS)实现的内部类,类图如下: 1、Condition接口ConditionObject实现了Condition接口。先来看看Codition接口。 Codition中主要定义了挂起线程和唤醒线程的接口方法。Condition接口详情如下:1publicinterfaceCon......