

















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 Nguyen Thai Manh Student ID Manhntgcd Class GCD0905 Assessor name Phan Thanh Son 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 Manh Grading grid P4 P5 P6 P7 M4 M5 D3 D
Grade: Assessor Signature: Date: Internal Verifier’s Comments: IV Signature:
Chapter I – Implement ADT & algorithms............................................................................................ 3 A. Description of my application.................................................................................................................. 3 B. ADT (P4, P5)............................................................................................................................................ 3
1. Implementation (P4) .................................................................................................................................................... 3 2. Errors handling by exception (P5) ............................................................................................................................. 8 3. Testing (P5) ................................................................................................................................................................ 10
Chapter I – Implement ADT & algorithms
- 4. Result Figure 3 : Source Code Queue Figure 4 : Source Code Queue 2
a. Explanation about the application
the Queue through and push them to the Stack via push() and delete the data in the Queue via dequeue(). Step 4: Print out the character from Stack and end program. c. Source code of the application I will defines a Message class that has two member variables: a QueueStructure object and a StackStructure object. The QueueStructure object is used to store a message. It is likely that this object has methods for adding elements to the queue and removing elements from the front of the queue. The StackStructure object is used to process the message. It is likely that this object has methods for pushing elements onto the stack and popping elements from the top of the stack. The Message class uses the QueueStructure object to store the message and the StackStructure object to process the message. However, it is not clear from the provided code how these objects are used or what methods they have. Figure 6 : Source Code 1 The enterMessage method is a member function of the Message class. It prompts the user for a message and reads the user input using the Scanner object. The Scanner object reads input from a standard input stream (e.g. keyboard input). The method then stores the entered message as a string in the enterMess variable and returns it. In the code, I will implement the method that prints the string "Please enter message: " to the console using the System.out.print method. This prompts the user to enter a message. The method creates a Scanner object named sc and passes the standard input stream as an argument to the Scanner constructor. This method calls the nextLine method on the Scanner object to read a line of text from the standard input stream. The method stores the entered message as a string in the enterMess variable. Finally, the method returns the enterMess variable.
Figure 7 : Source Code 2 This function has the main task of taking a string as input and processing it. First, the method splits the input string into an array of strings with the delimiter ".". Then it checks if the length of the input string is less than or equal to zero or greater than 250. If either of these conditions is true, the method will raise an IllegalArgumentException. If the length of the input string is within the allowed range, the method will add each element in the array to the queue using the Enqueue method of the QueueStructure object. It then calls the StackProcess method to process the message. Finally, the method prints out the length of the message in characters and the message after it is processed by the system.
time variable, I will print the transit and message processing times. Additionally, I'll use the entire code I just defined above in an effort to catch the IllegalArgumentException whenever a user types a message incorrectly. Figure 11 : Source Code 5
An exception is an unanticipated or unanticipated event that happens during the execution of a program, also known as run time, and disrupts the regular flow of instructions in the program. Programs are capable of catching and handling exceptions. When an exception occurs, a procedure creates an object. This object has the name exception object assigned to it. It includes information about the exception, including its name, description, and the state of the program at the time the exception happened. The five keywords try, catch, throw, throws, and finally are used by Java to manage exceptions. Simply said, try blocks are comprised of program statements that we think might result in an exception. The try block throws an exception if one arises. Our code can catch this exception and handle it logically (using a catch block). System-generated exceptions are thrown automatically by the Java run-time system. To manually raise an exception, use the keyword throw. A throws clause must be used to explicitly specify any exception that a method throws. Any code that must be executed immediately after a try block has finished running is contained in finally blocks.
Figure 12 : Exceptions thrown in send message The source code shown above is utilized when a user enters an excessive number of characters or none at all. If one of these exceptions occurs, I will print a message stating that the message is blank or contains more than 250 characters. Figure 13 : Exceptions thrown in send message
2 Verify to see if the messa ge excee ds 250 charac ters. Enter a message string that is more than 250 characters. The message has empty or is more than 250 characte rs and stops the program . Pas s 3 Verify messa ge lines that have 123 charac ters. Enter the following message line "The most popular asymptotic notations for determining an algorithm's running time complexity include the ones listed below." The softwar e ends after processi ng the message 's 123 characte rs and message line. Pas s 4 Verify specia l charac ter messa ges. Enter the following message line "***&&% %%%$" The softwar e ends after processi ng the message 's 10 char acters and message line. Pas s
The outcomes when I tested the program's functionality without entering the string are shown below. The application will report the user input as an exception because no message was entered. Figure 14 : Result 1 When I enter more than 250 characters for the message to be sent, the pass is displayed in the result below. When entering more than 250 characters or nothing at all, exceptions are printed. Figure 15 : Result 2 The outcome of sending a message line with 123 characters is seen below. Results from printing indicate that the message was received and that it has been printed with a length of 123 characters, including the length of the message that was entered and the message that was printed to the screen. Figure 16 : Result 3 When I insert special characters for the message to send, the outcome is seen below. The results provided indicate that the message was sent successfully, has 10 characters.
The asymptotic notation of an algorithm is a mathematical representation that expresses how long the algorithm will run when the input tends to a particular value, body, possible, or limit value. The most popular asymptotic notations for determining an algorithm's running time complexity include the ones listed below. Big Theta. Big-Ω Notation. Big-O Notation. a. Big-Ο Notation Asymptotic analysis is the study of how the algorithm's performance changes when the order of the input size changes. We employ big-notation to asymptotically confine the expansion of a running time to within constant factors above and below. The amount of time, storage, and other resources required to perform an algorithm determine its efficiency. Asymptotic notations are used to determine the efficiency. For different types of inputs, an algorithm's performance may vary. The performance will fluctuate as the input size grows larger. Figure 18 : Big O Notation When the input tends towards a certain value or a limiting value, asymptotic notations are used to represent how long an algorithm takes to execute. When the input array is already sorted, for example, the time spent by the method is linear, which is the best scenario.
However, when the input array is in reverse order, the method takes the longest (quadratic) time to sort the items, which is the worst-case scenario. It takes average time when the input array is not sorted or in reverse order. Asymptotic notations are used to represent these durations. Big O notation classifies functions based on their growth rates: several functions with the same growth rate can be written using the same O notation. The symbol O is utilized since a function's development rate is also known as the order of the function. A large O notation description of a function generally only offers an upper constraint on the function's development rate. It would be convenient to have a form of asymptotic notation that means "the running time grows at most this much, but it could grow more slowly." We use "big-O" notation for just such occasions (Javatpoint, 2022). b. Constant Time Algorithms – O(1) Whatever n is, it doesn't matter because this function completes in O(1) time. Whether the input array has one entry or 1,000, this function simply requires one step. This code's execution time is constant. There is no effect on n's size. The example that follows also pertains to continuous time. Even if the input, n, is three times longer, the runtime is unaffected. The following names are given to constant time algorithms: O(1). O(1000), O(1000), and O(2) are all equivalent. Figure 19 : Example O(1) c. Logarithmic Time Algorithms – O(log n) The constant time algorithm is the quickest asymptotically. In terms of speed, logarithmic time comes in second. They are unfortunately a little more challenging to picture. A good illustration of a logarithmic time algorithm is the binary search technique. It's important that the runtime increases in accordance with the input logarithm (in this case, log to base