
































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

































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 Do Thi Dieu Student ID BHAF Class Assessor name Nguyen Thai Cuong 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 P4 P5 P6 P7 M4 M5 D3 D
Grade: Assessor Signature: Date: Internal Verifier’s Comments: IV Signature:
Table of figure Figure 1. Main program chart......................................................................................................................... 9 Figure 2. The “Message” class..................................................................................................................... 10 Figure 3. The “MyStack” class..................................................................................................................... 12 Figure 4. The “Queue” class......................................................................................................................... 15 Figure 5. The “Main “class........................................................................................................................... 16 Figure 6. Syntax try-catch............................................................................................................................. 17 Figure 7. IllegalStateException: Underflow Exception................................................................................ 18 Figure 8. Error handling in stack.................................................................................................................. 18 Figure 9. Handling errors when entering characters and IndexOutOfBoundsException in the queue......... 19 Figure 10. Test case results one.................................................................................................................... 19 Figure 11 Test case results two..................................................................................................................... 20 Figure 12 Test case results three................................................................................................................... 20 Figure 13. Test case results three.................................................................................................................. 21 Figure 14. Test case...................................................................................................................................... 21 Figure 15. Two views of a graph illustrating the growth rates for six equations. The horizontal axis represents input size. The vertical axis can represent time, space, or any other measure of cost................ 24 Figure 16. Cots for growth rates representative of most computer algorithm.............................................. 25 Figure 17. Illustrating the “big-Oh” natation. The function f(n) is O(g(n)), since f(n) ≤ c g(n) when n ≥ n 0 ...................................................................................................................................................................... 26 Figure 18. Illustrating the Omega-Ω natation............................................................................................... 27 Figure 19. Illustrating the Theta-Θ notation................................................................................................. 28 Figure 20. Loops........................................................................................................................................... 29 Figure 21. Nested loops................................................................................................................................ 29 Figure 22. Consecutive statement................................................................................................................. 30 Figure 23. Example for if/else...................................................................................................................... 30 Figure 24. Time Complexity Table.............................................................................................................. 32 Figure 25. A graph showing the response of different time complexities as the number of element increase. Coefficients were choose so that the worst time complexities were the largest by the time n = 1000 .............................................................................................................................................................. 33 Figure 26. Space Complexity table............................................................................................................... 34 Figure 27. Space complexity........................................................................................................................ 35 Figure 28. Stack Implementation.................................................................................................................. 37 Figure 29. Performance of a stack realized by an array. The space usage is O(N), where N is the size of the array, determined at the time the stack is instantiated, and independent from the number n ≤ N of messages that are actually in the stack......................................................................................................... 38 Figure 30. Implantation Queue..................................................................................................................... 38 Figure 31. The performance of a queue is performed by an array. The use of space is O (N), where N is the size of the array, determined at the time the queue is created and independent of the number n <N of the messages actually in the queue..................................................................................................................... 39
I. Introduce The same problem can frequently be solved with algorithms that differ in inefficiency. Once an algorithm is given for a problem and decided (somehow) to be correct, an important step is to determine how much in the way of resources, such as time or space, the algorithm will require. In this assignment, my report includes the following sections: First, I will implement a complex ADT and algorithm in a programming language (Java) to solve a well-defined problem. Second, I will implement error handling and report the test results. Third, I will explain how asymptotic analysis can be used to evaluate the effectiveness of an algorithm. Finally, I will give two possible ways of measuring the effectiveness of an algorithm with specific examples. II. Implement ADT The given scenario is: “For the middleware that is currently developing, one part of the provision interface is how message can be transferred and processed through layers. For transport, normally a buffer of queue messages is implemented and for processing, the systems require a stack of messages. The team now has to develop these kinds of collections for the system. They should design ADT / algorithms for these 2 structures and implement a demo version with message is a string of maximum 250 characters. The demo should demonstrate some important operations of these structures. Even it’s a demo, errors should be handled carefully by exceptions and some tests should be executed to prove the correctness of algorithms / operations.” Based on the given scenario, we can determine that we need some suitable ADTs to solve this problem. They are stack and queue, which are defined in Assignment 1. Stack is used to store the process messages. Queue is used to store the transported messages.
A stack is a linear data structure that can only be accessed at one of its ends for storage and retrieve data. Such a stack is like a stack of trays in the cafeteria: the new trays are put on the top of the stack and take out top. The last tray put on the stack is the first tray removed from the stack. Hence, it is called the Last in First out (LIFO) or First in Last out (FILO) list. Thus, Stack is used to store the process messages.
A queue is simply a queue that grows by adding element at the end and shrinking by talking elements from it front. Unlike a stack, a queue is a structure in which both ends are used: one to add new elements and one for removing them. So, the last element has to wait until all elements preceding it on the queue are deleted. Hence, it is called First in First out (FIFO) or Last on Last out (LILO) list. Thus, Queue is used to store the transported messages. The message will be added to the queue, if the queue is full then the message will not be added to the queue. 1.1 Flowchart
Figure 2. The “Message” class Next, I will design the stack ADT for the system.
Figure 5. The “Main “class
2.1 Error handling No program or segment can be considered complete until the appropriate error handling has been added. Unwanted program errors are a catastrophe - at the best, they cause disappointment because program users have to repeat for minutes or hours of work, but in life-critical applications, even the most trivial program error, if not processed correctly, has the potential to kill someone. If an error is fatal, in the sense that a program cannot reasonably continue, the program must be able to " die gracefully ". This means that it must informs its user (s) why it died and save as many program states as possible.
Error handling is the process of responding to and recovering from error conditions in your program’s. When executing Java code, different errors can occur: coding errors made by the programmer, errors due to wrong input, or other unforeseeable things. When an error occurs, normally Java will stop and generate an error message. The technical term for this is: Java will throw an exception (throw an error). An Exception is an unusual event, often an error, that requires special processing. Exceptions can be caused by using a reference that is not yet initialized, dividing by zero, going beyond the length of an array, or even JVM not being able to assign objects on the heap. The try-catch block is used to execute the catch and throw pattern of exception handling. The try statement allows you to define a block of code to be tested for errors while it is being executed. The catch statement allows you to define a block of code to be executed, if an error occurs in the try block. The catch block in Java is used to handle the Exception by declaring the exception type in the parameter. The declared exception must be an exception of the parent class exception (i.e, Exception) or the generated exception type. However, the good approach is to declare the generated type of exception. The catch block must be used after the try block only. You can use multiple catch blocks with a single try block. The try and catch keywords come in pairs: Figure 6. Syntax try-catch When implementing the system, we will encounter some errors, exceptions like NullPointerException (The NullPointerException is thrown when the program tries to refer to an object but it has no place in memory, i.e. has a null value), IndexOutOfBoundsException, ...: For example:
Figure 9. Handling errors when entering characters and IndexOutOfBoundsException in the queue 2.2 Test result Results of running program tests:
Figure 11 Test case results two