Tải bản đầy đủ (.docx) (26 trang)

asm1- CTDL-1690 -Greenwich PASS grade. Code phía dưới

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.48 MB, 26 trang )

ASSIGNMENT 1

Qualification

BTEC Level 5 HND Diploma in Computing

Unit number and title

Unit 19: Data Structures and Algorithms

Submission date

Date Received 1st submission

Re-submission Date

Date Received 2nd submission

Student Name

Student ID

Class

GCD

G

Assessor name

Student declaration


I certify that the assignment submission is entirely my own work and I fully understand the consequences of plagiarism. I understand that
making a false declaration is a form of malpractice.
Student’s signature
Grading grid
P1

P2

P3

M1

M2

M3

D1

D2


 Summative Feedback:

Grade:
Assessor Signature:
Internal Verifier’s Comments:

IV Signature:

 Resubmission Feedback:


Date:


Table of Figures
Figure 1: ADTs...............................................................................................................................................5
Figure 2: Example for stack............................................................................................................................6
Figure 3: push onto stack................................................................................................................................7
Figure 4: push method in stack.......................................................................................................................7
Figure 5: test push method..............................................................................................................................7
Figure 6: Stack after push...............................................................................................................................7
Figure 7: pop() of stack...................................................................................................................................8
Figure 8: after pop..........................................................................................................................................9
Figure 9: peek() of stack...............................................................................................................................10
Figure 10: method peek in class stack..........................................................................................................10
Figure 11: test peek method..........................................................................................................................10
Figure 12: After peek....................................................................................................................................10
Figure 13: example for application of stack in memory...............................................................................11
Figure 14: : example for application of stack in memory(1)........................................................................12
Figure 15: : example for application of stack in memory(2)........................................................................12
Figure 16: Pharmacist menu using switch case............................................................................................14
Figure 17: Pharmacist Controller methods...................................................................................................15
Figure 18: Pharmacist system Create method..............................................................................................16
Figure 19: Pharmacist system Read method.................................................................................................17
Figure 20: Pharmacist system Delete method..............................................................................................18


Table of Contents
Table of Contents..................................................................................................................................3
Table of Figures.....................................................................................................................................4

1. Data structures...............................................................................................................................5
1.1 Abstract data type (P1)......................................................................................................................................................5
1.1.1 Definition:...................................................................................................................................................................5
1.1.2 Examples.....................................................................................................................................................................6
Overview stack operations..................................................................................................................................................6
a. push(E element):.........................................................................................................................................................7
c. peek() :.......................................................................................................................................................................10
1.2 ADT usages.......................................................................................................................................................................11
1.2.1 Application of Stack in memory (P2)........................................................................................................................11
Definition:......................................................................................................................................................................11
1.2.2 Application of an ADT (P3)........................................................................................................................................13
Scenario:........................................................................................................................................................................13
Requirement:................................................................................................................................................................13
The class Pharmacist Controller....................................................................................................................................14
Running program...............................................................................................................................................................16

References...........................................................................................................................................19
Java ArrayList (2023).....................................................................................................................................................19
Available at: (Accessed: 14 July 2023).....................................19
ArrayList in Java - GeeksforGeeks (2016)......................................................................................................................19
Available at: (Accessed: 14 July 2023)..........................................19

Index of comments..............................................................................................................................20


1. Data structures
1.1 Abstract data type (P1)
1.1.1 Definition:
A mathematical representation of data types is called an abstract data type (ADT). Its behavior
(semantics), as seen from the perspective of a user, is determined by the data's potential values, applicable

operations, and the behavior of those operations.
ADTs are used to abstract away the implementation details of data structures and algorithms. This makes
it easier to use and understand these data structures and algorithms, and it also makes them more reusable.

Figure 1: ADTs

There are many different ways to represent ADTs. Some common ways include:
- Interfaces: An interface is a contract that lists the functions that can be performed on an ADT. This is the
most typical manner in which object-oriented programming languages describe ADTs.
- Enumerations: An enumeration is a list of named constants. This can be used to represent the possible
values of an ADT.
- Data structures: Data structures are a method of data organization. This can be used to illustrate how an
ADT is implemented.
Here are some examples of ADTs:
 Stack: A stack is a data structure that stores data in a Last In, First Out (LIFO) order.
 Queue: A queue is a data structure that stores data in a First In, First Out (FIFO) order.
 Linked list: A linked list is a data structure that stores data in a linked list.


1.1.2 Examples
The notion of Linear structure will be introduced before talking about Stack. A collection of data has a
linear structure in which the items are ordered in a specific sequence, it has two ends, bottom and top or
left and right, and it can only be accessed sequentially. Elements are typically linked sequentially and
require linear memory space. Stack and Queue are two types of linear data structures.
Stack is an ADT with top-down data structure, things like adding elements and removing elements are
done on top of the Stack structure. It is a linear data structure because the data set is ordered in ascending
order, the first in first, the last in second, can only access the elements in sequence (it cannot be go directly
from bottom to top). Stack's sorting principle is called LIFO (last in, first out) the element added to the
end will be accessed first, then the element below it.
The example for stack is stacks of thing below if you want to remove the bottom thing, you have to take

one by one from the top to the one you want. Otherwise if you want to add a new one you must put it on
the top.

Figure 2: Example for stack

Overview stack operations
-

push (Element item): Add an item of data type Element to the top of the stack, return true
(successful), false (failed).
pop (): Remove an item from the top of the stack and return its value.
peek (): Get the value on the top of the stack.
empty (): Checks if the stack has an item, returns true if true, returns false if false.


a. push(E element):
How it works: The function will accept an item of data type Element as input and then verify if the stack is
full. Finally, conduct a push operation, which will add the value to the top of the stack and push the
preceding element down, resulting in the new element being at the top of the stack. Failure (full stack limit
or full memory, for example) returns false.

Figure 3: push onto stack

After push 6 onto the stack, 6 become TOP instead of 1, return true.
Source code:

Figure 4: push method in stack

Code test:


Figure 5: test push method

Running:

Figure 6: Stack after push


b. pop():
How it works: The pop() function checks whether the stack is empty or not, if empty, returns null, if
there is an element, deletes that element from the stack, the top element after deletion will be the element
below. Returns the deleted value.

Figure 7: pop() of stack

Do pop() with stack S, the top element (5) will be removed, the return value will be 5. The purpose of
returning the deleted value is so that the user knows what the deleted value is or uses it for something
else.


Source code:

Code test:

Figure 8: after pop


c. peek() :
How it works: The peek() function checks whether the stack is empty or not, if empty, returns null, if not,
returns the top value of the stack. This function does not change anything of the stack, it only has the
purpose of returning the top value of the stack to the user.


Figure 9: peek() of stack

Do peek(), the top element (6) will be return , nothing was changed in stack S.
Source code:

Figure 10: method peek in class stack

Code test:

Figure 11: test peek method

Running

Figure 12: After peek


1.2 ADT usages
1.2.1 Application of Stack in memory (P2)
Definition:
Memory stack is a system memory space that is used to hold variables and functions when running the
program. When the function terminates, the memory cell that records that function is deleted and freed.
Push (insert a variable or function into stack memory) and pop (remove position to free memory) are the
two most common memory stack operations.

Figure 13: example for application of stack in memory


Applications in storing memory cells for recursive functions, for example, calculating the results of the
recursive factorial-function below, the variable result will be stored on the stack, the factorial-function(4)

will also be stored on the stack to wait for the results, after calculate. Each recursive call will have an extra
memory cell to store the factorial-function, when factorial (1) will have a return value of 1, then calculate
the value of factorial (2), factorial (3), factorial (4) and free the stack memory. Finally return the value for
the variable result at Main.

Figure 14: : example for application of stack in memory(1)

Figure 15: : example for application of stack in memory(2)


1.2.2 Application of an ADT (P3)
Scenario:
The customer X is the owner of a drug store and they are looking for a software to manage pharmacists.
They need software that can track the pharmacist's basic information such as name, age, and phone
number.
Requirement:
- Adding a pharmacist
- Editing a pharmacist's information
- Deleting a pharmacist's information
- Search for a pharmacist.
- Show all pharmacist.
For this scenario I will use Java language with ArrayList to do it.
Before making this program, I need to write an Array List ADT so that I can use it in my program. In this
program, I only used two main function is add and delete objects, so I write two methods to do that
function.
ArrayList Source code:

Figure 16: ArrayList source code(1)



Figure 17: ArrayList source code(2)

Figure 18: ArrayList source code(3)

Figure 19: ArrayList source code(4)


-

-

To make this program, I decided to use ADT which is ArrayList, because Array List is a dynamic
array that can grow or shrink as needed. That will make this program is easy for managing for
users.
My program will have two class to manage pharmacist. One is Pharmacist and another is
PharmacistController

The class Pharmacist
Here is the Pharmacist code with field, constructor, getter setter and method Display().

Figure 20: Class Pharmacist


The class Pharmacist Controller
Before using ArrayList, we must import the ArrayList library we already write above:

Figure 21: Import ArrayList

Here is the PharmacistController code with method AddNew(), ViewAll(), Delete().
⎯ Insert new Pharmacist (Add a new information of a Pharmacist to system)

⎯ View list of Pharmacist (View all information of Pharmacistin system)
⎯ Delete Pharmacist (Delete delete a specific Pharmacist with the Id passed in via console of a
Pharmacistto system)

Figure 22: Pharmacist menu using switch case


Figure 23: Pharmacist Controller methods


Running program
Firstly, Let’s see how the Pharmacist system look then I will add something new into the system

Figure 24: Pharmacist system Create method


Secondly, check that have the new pharmacist I just add above is appear in system yet?

Figure 25: Pharmacist system Read method


Thirdly, Delete method

Figure 26: Pharmacist system Delete method



×