Download Assignments 1 1651-advance programing and more Assignments Information Technology in PDF only on Docsity!
ASSIGNMENT 1 FRONT SHEET
Qualification BTEC Level 5 HND Diploma in Computing Unit number and title Unit 19: Data Structures and Algorithms Submission date 02/11/2023 Date Received 1st submission 02/11/ Re-submission Date Date Received 2nd submission Student Name Nguyễn Văn Trường Student ID GCH Class GCH1107 Assessor name Đỗ Hồng Quân 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 Trường Grading grid P1 P2 P3 M1 M2 M3 D1 D
Summative Feedback: Resubmission Feedback: Grade: Assessor Signature: Date: Internal Verifier’s Comments: IV Signature:
1. ADT
An abstract data type (ADT) is a class of objects characterized by a specific set of values and operations. It is a data type that defines its behavior through properties and functions, focusing on what operations it can perform rather than the specific implementation details. ADTs don't dictate the internal memory organization or algorithms used to execute these operations. Typically, ADTs are encapsulated within classes or structs, allowing us to create objects of the class to work with the abstract data type. (geeksforgeeks, 2023). Figure 1: Adtract data type (geeksforgeeks, 2023). Abstract data types within data structures encompass examples like lists, stacks, and queues, among others.
- Operations in ADT: (geeksforgeeks, 2023). Lists:
- Creators: Methods for creating new lists, which may take parameters but not objects of the same type.
- Producers: Operations that create new lists or elements from existing ones, like concatenating strings or joining lists.
- Observers: Functions that provide information about the list, such as its size, which returns an integer.
- Mutators: Methods that modify the list, like adding an element to the end. Stacks:
- Creators: Functions for initializing new stacks.
- Producers: Operations that create new stacks or elements based on existing ones.
- Observers: Functions to inspect the top element or check if the stack is empty.
- Mutators: Methods for pushing elements onto the stack (add) or popping elements from the stack (remove). Queues:
- Creators: Methods for creating new queues.
- Producers: Operations for creating new queues or elements based on existing ones.
- Observers: Functions to access the front and rear elements or check if the queue is empty.
- Mutators: Methods to enqueue (add) and dequeue (remove) elements from the queue. List ADT: (geeksforgeeks, 2023).
- get(int index) - Retrieves the element located at a specific index in the list.
- add(E e) - Appends the provided element to the end of the list.
- remove(Object o) - Deletes the first instance of the specified element from the list.
- remove(int index) - Eliminates the element at the given index in the list.
- size() - Reports the number of elements in the list.
- isEmpty() - Indicates whether the list is empty, returning true if it is, and false if it's not. Abstract data types (ADTs) offer several advantages compared to concrete data types: (geeksforgeeks, 2023). + Representation Independence: One of the primary benefits is that abstract data types allow most of the program to operate independently of how the ADT's data is represented. This means you can improve the data's internal representation without having to overhaul the entire program. + Modularity : Thanks to representation independence, different parts of a program become less reliant on each other and are less affected by how those other parts are implemented. This modularity enhances the maintainability and flexibility of the program. + Interchangeability of Parts: Different implementations of an ADT can have varying performance characteristics. Abstract data types make it easier for different parts of a program to use the specific ADT implementation that best suits their efficiency requirements. This interchangeability improves overall program efficiency and adaptability.
- Stack operations and working mechanism
- public E push(E item ) adds a new item to the top of the stack. Parameters: a new element with value is item. Returns: the item argument.
- public E pop() r emoves the object at the top of this stack Returns: The object at the top of this stack
- public E peek() Looks at the object at the top of this stack without removing it from the stack. Returns: the object at the top of this stack
- public boolean empty() Checks if this stack is empty. Returns: Return "true" if and only if this stack contains no items Return "false" if it contains items.
- Public Boolean isFull() check if the stack is full or not. Return: 'true' if the stack is full. 'false' if the stack is not full.
- public int search(Object o) searches for a specific element in the stack. Parameter: The parameter is the element to search for in the stack, it is an object of type Object, because the stack can contain different types of data.
Return: the 1 - based position from the top of the stack where the object is located; the return value - 1 indicates that the object is not on the stack.
- Examples of Stack: Figure 2: Example Stack Output Stack Push(7) 7 (7) Push(14) 14 (14) Push(21) 21 (21) Pop(21) 7, 14 (7, 14) Pop(14) 7 (7) isEmpty() False () Size() 1 Table 1: Example of the stack
Example of function call: Figure 3: Soure code When the program is executed, it commences by calling the main() function, resulting in the creation of an activation record that is appended to the top of the call stack. Subsequently, main() invokes introduceYourself(), leading to the addition of an activation record for introduceYourself() at the stack's top. Following that, greet() is triggered, which places its activation record on the stack. After greet() completes, its activation record is removed from the stack. Then, introduceYourself() concludes, and its activation record is eliminated. Ultimately, when main() returns, its activation record is discarded, effectively clearing the stack. In a visual representation, the state of the stack evolves as described in Figure 4, with activation records being pushed onto the stack when functions are called and popped off when functions return.
Figure 4 A stack that stores activation records
4. Queue ADT
A queue is a fundamental abstract data structure that operates on a "first-come, first-served" basis, adhering to the principle of "first in, first out" (FIFO). It's somewhat similar to stacks, but queues are open at both ends. One end is exclusively used for adding data (enqueuing), while the other end is dedicated to removing data (dequeuing). In essence, a queue follows the methodology where the item that enters the queue first is the one that will be accessed and processed first. (Rajinikanth 2023) There are several methods to implement the Queue architecture, including actions to a new element and to two key elements: Enqueue: is the operation of adding an element to the end of the expected queue. Dequeue: is the operation to remove elements at the beginning of the queue.
- Queue operations and working mechanism
- Public boolean enQueue(E value): adds a new element to the end of the queue. Parameter: value is the element added to the end of the queue. This parameter contains information about the element placed into the queue. Return: Returns "true" when the element has been successfully added to the end of the queue. Returns "false" if the item cannot be added to the queue due to capacity restrictions or for some other reason.
5. Explanation on how to specify an abstract data type using the
example of software stack
5.1 Introduction to formal specification, types of formal specification languages.
Formal specification is a method employed to enhance the precision of a system's requirements specification by employing mathematical techniques. It aids in generating a clear and unambiguous portrayal of how a system should behave. These formal specification techniques are invaluable for identifying issues in system requirements and can be utilized to establish precise interface and behavioral specifications. (Sommerville, 2009). Formal specifications are used in a variety of software engineering tasks, including: (Sommerville, 2009).
- Requirements analysis: Capture the requirements of a system in a precise and certain way. This can help to avoid misunderstandings between the customer and the developer.
- Design: Verify the design meets the requirements and to identify potential problems.
- Verification: Prove the implementation satisfies the specification.
- Validation: Check the implementation produces the correct results for a given set of inputs. There are two fundamental approaches to formal specification (Sommerville, 2009):
- An algebraic approach where the system is described in terms of operations and their relationships.
- A model-based approach where a model of the system is built using mathematical constructs such as sets and sequences, and the system operations are defined by how they modify the system state.
5.2 Describe what are pre-condition, post-condition, error-condition.
- The pre-condition of a method represents a condition that needs to be satisfied when the method is called. Pre-conditions encompass the method's input parameters and any additional requirements the method has regarding the current state of the program. If a user doesn't meet these preconditions, the method's outcome becomes uncertain; it could lead to various outcomes like throwing an exception, returning incorrect results, or attempting error recovery. Nevertheless, it's important not to expect or depend on consistent behavior when the preconditions are not met. (Matthew, 2012)
- A method's post-condition signifies a condition that becomes valid once the procedure has successfully completed. This includes the method's returned value and any possible consequences it may have. When you use a method, you can safely assume that all its post-conditions hold true. If you're creating your own method, it's essential to define these post-conditions clearly to inform others about the expected outcomes when they utilize the method. (Matthew, 2012)
- An error-condition refers to a subclass of Throwable that identifies serious issues which a well-designed program should not attempt to handle. Most of these errors arise from exceptional and uncommon situations. For instance, the ThreadDeath error is classified as an Error subclass because in most cases, applications should not make an effort to handle or catch it, even though it might be considered a "regular" occurrence. (Matthew, 2012)
5.3 Specify Stack’s operations using this formal specification language.
The stack will be called Stack. Variables and function definition:
- s = Stack.Size(): Retrieves the current number of elements in the Stack.
- Stack.Max_Capacity: Represents the maximum number of elements that the Stack can store.
- Peek(): Returns the topmost element in the Stack.
- item: A parameter used in the Push() function, with a type E defined by the user.
- Pop(): Removes the top element from the Stack.
- isEmpty(): Returns true or false depending on whether the Stack has any elements.
- Top: Represents the index of the top element in the Stack.
- t = Stack.Peek(): Stores the value of the current top element of the Stack. Push(E item)
- Pre-condition: o s < Max_Capacity o item != null
- Post-condition: o Size() = s + 1 o Peek() == item
- Error condition: o s >= Max_Capacity => print “Stack Overflow” o item = null => Throw exception “Invalid data” Pop() : item i
- Pre-condition: o s > 0 o Max_Capacity > 0
- Post-condition: o Size() = s - 1 o Peek() != t
- Error condition: o s <= 0 => print “Stack is Empty”
References
GeeksforGeeks (2023) Abstract data types. Available at: https://www.geeksforgeeks.org/abstract-data- types/ (Accessed: 17 October 2023). GeeksforGeeks (2023) Stack Data Structure. Available at: https://www.geeksforgeeks.org/stack-data- structure/ (Accessed: 17 October 2023). Matthew (2012) Homeback to start , DevFright. Available at: https://www.devfright.com/what-are-pre- conditions-and-post-conditions-in- programming/?fbclid=IwAR1DAKVrWl9Gp86r7YYFrEXqY3BzaEVS1fn92ncROPwvQQ_GgVY4FQV Nai0 (Accessed: 31 October 2023). Matthew Martin and Martin, M. (2023) Stack vs heap memory – difference between them , Guru. Available at: https://www.guru99.com/stack-vs-heap.html (Accessed: 02 November 2023). Rajinikanth (2023) Data Structures, Data Structures Tutorials - Queue ADT. Available at: http://www.btechsmartclass.com/data_structures/queue-adt.html (Accessed: 27 October 2023). Sommerville (2009) 27 formal specification - university of st andrews. Available at: https://ifs.host.cs.st- andrews.ac.uk/Books/SE9/WebChapters/PDF/Ch_27_Formal_spec.pdf (Accessed: 31 October 2023). What is LIFO (last in, first out)? (2022) Computer Hope. Available at: https://www.computerhope.com/jargon/l/lifo.htm (Accessed: 17 October 2023).