Docsity

Implementing a Queue and Stack System for Processing User Input - Prof. Fpt, Summaries of Information Technology

Toggle sidebar
bg1
ASSIGNMENT 2 FRONT SHEET
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 Manhntgcd201535
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 D4
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

Download Implementing a Queue and Stack System for Processing User Input - Prof. Fpt and more Summaries Information Technology in PDF only on Docsity!

ASSIGNMENT 2 FRONT SHEET

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

 Summative Feedback:  Resubmission Feedback:

Grade: Assessor Signature: Date: Internal Verifier’s Comments: IV Signature:

Table of Contents

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

A. Description of my application

  1. I created and implemented abstract data types for assignment 1. Therefore, we will implement and complete them in accordance with the user's desire in this task 2. Building a project to process and transfer user input from the keyboard and print it on the screen is the task we perform in this project. Our development team is currently in charge of the functionality for which the application we design is the middleware provider interface. Typically, the system needs a message stack for processing and a buffer of queued messages is produced for conveyance. Working as a back-end developer at a software business that specialized in networking solutions. I have to make these kinds of collections for the system. For these two structures, they must develop an ADT/algorithm, which they must then show using a message string of no more than 250 characters. Some of the most important operations of the structures will also be demonstrated in the demo. In addition, failures shall be treated cautiously, with some exceptions, and tests should be done to verify the accuracy of the algorithms/operations. This section will cover the two data structures' implementation as well as how to assess the effectiveness of the algorithms used in their creation and development, including the complexity issue.
  2. Stack An example of a linear data structure that follows the "last-in, first-out" (LIFO) rule is a stack. This implies that the first item to be eliminated comes after the last piece is put to the stack. Stacks are frequently used to store information that must be processed in a particular order, such as the history of calculations made on a calculator or the list of websites viewed in a web browser. They are also employed in computer systems to assist recursive algorithms and store temporary data.
 - 4. Result 
  • Chapter II – Analysis..........................................................................................................................
    • A. Big O (P6)...............................................................................................................................................
        1. Definition of Asymptotic analysis
        1. Asymptotic Notation and how it can be used to assess the effectiveness of an algorithm
    • B. Analyze performances (P7)....................................................................................................................
        1. Time Complexity
        1. Time Complexity In My Application
  • Figure 1: Source code Stack 1......................................................................................................................................
  • Figure 2: Source Code stack 2......................................................................................................................................
  • Figure 3: Source Code Queue1
  • Figure 4: Source Code Queue 2...................................................................................................................................
  • Figure 5:Flowchart of the application........................................................................................................................
  • Figure 6: Source Code 1.............................................................................................................................................
  • Figure 7: Source Code 2.............................................................................................................................................
  • Figure 8: Source Code 3.............................................................................................................................................
  • Figure 9: Source Code 4.............................................................................................................................................
  • Figure 10: Source Code 5...........................................................................................................................................
  • Figure 11: Source Code 5...........................................................................................................................................
  • Figure 12: Exceptions thrown in send message.........................................................................................................
  • Figure 13: Exceptions thrown in send message.........................................................................................................
  • Figure 14: Result 1.....................................................................................................................................................
  • Figure 15: Result 2.....................................................................................................................................................
  • Figure 16: Result 3.....................................................................................................................................................
  • Figure 17: Result 4.....................................................................................................................................................
  • Figure 18: Big O Notation..........................................................................................................................................
  • Figure 19: Example O(1)............................................................................................................................................
  • Figure 20: Example O(log n).......................................................................................................................................
  • Figure 21: Example O(n)............................................................................................................................................
  • Figure 22: Example O(n2)..........................................................................................................................................
  • Figure 23: Example O(2n)..........................................................................................................................................
  • Figure 24: Code of Time complexity..........................................................................................................................
  • Figure 25: Result of case 1........................................................................................................................................
  • Figure 26: Result of case 2........................................................................................................................................
  • Figure 27: Result of case 3........................................................................................................................................
  • Figure 1 : Source code Stack

Figure 3 : Source Code Queue Figure 4 : Source Code Queue 2

B. ADT (P4, P5)

1. Implementation (P4)

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

2. Errors handling by exception (P5)

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

4. Result

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