Docsity

Data Structures and Algorithms - Assignment 2, Papers of Data Structures and Algorithms

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
14 May 2022
Date Received 1st
submission
Re-submission Date
Date Received 2nd
submission
Student Name
Hồ Ngọc Khánh
Student ID
GCS200074
Class
GCD0901
Assessor name
Phạm Thanh Sơ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
Khanh
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
pf1a
pf1b
pf1c
pf1d
pf1e

Partial preview of the text

Download Data Structures and Algorithms - Assignment 2 and more Papers Data Structures and Algorithms 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 14 May 2022 Date Received 1st submission Re-submission Date Date Received 2nd submission Student Name Hồ Ngọc Khánh Student ID GCS Class GCD0901 Assessor name Phạm Thanh Sơ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 Khanh Grading grid P 4 P 5 P 6 P 7 M 4 M 5 D3 D

 Summative Feedback:  Resubmission Feedback:

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

I. IMPLEMENT ADT & ALGORITHMS

1. Description of your application

One aspect of the provider interface for the middleware that is presently being developed is how messages may be transported and processed via layers. Typically, a buffer of queued messages is used for transit, and a stack of messages is used for processing. I need to create these kind of collections for the system. We should create ADT / algorithms for these two structures and develop a sample version in which the message is a string with a maximum length of 250 characters. The demo will show some of these structures' most significant operations. Even if it's only a demo, failures should be handled properly through exceptions, and some tests should be run to ensure that algorithms and operations are valid.

Figure 2 : FLOWCHART FOR STACKPROCESS FUNCTION AND STACKSTART FUNCTION EXPLANATION OF THE SYSTEM 1st. Declare Queue and Stack based on the rebuilt Queue and Stack, also declare strings array 2nd. The first function that the system encounters is the EnterMessage() function. This function just simply reads the line from the console and store it inside a temporary variable 3rd. Declare the variable startTime to start count the time of processing data 4th. The next function that the system encounters is TransferMessage(). This function will do the job of making sure that the message inputted is totally fine. First up, the function will call for a loop to split all the sentences in that message to elements so it will be easier to handle. At this time there will be four circumstances to happen

  • The length of the message goes over 250 characters: a new exception will be thrown which is called Index-Out-Of-Bounds-Exception. The system will ask the user to type the message again
  • There is nothing in the message: a new exception will be thrown and it is called IO- Exception. The system will also ask the user to type the message again
  • The message that doesn’t meet any above mistakes: that message will be enqueued to the queue and the system will notify that the message has been successfully sent
  • The last exception where we can’t cover: Illegal-State-Exception 5th. The next function that the system encounters is StackProcess(). The function calls for a while loop to check that the queue is empty or not. If it is empty, then this function will finish. On the other hand, when the queue is not empty, the stack will start to push the message that is dequeued from the queue 6th. The last function is StackStart(). This function basically prints out the message. Firstly, it will print a small notification of telling users that there will be a message. Next up, it will loop to check the stack whether it is empty or not. If the stack is empty then there is no message at all. However, if the stack is not empty, the stack will pop the message and print it out 7th. At this time, I declare a stopTime to stop counting the time the system processing. Print the stopTime out and that’s the end

3. ADT (P4, P5)

i. Implement

IMPLEMENTATION OF THE DATA STRUCTURE

First up, I have designed a class called “Buffer” to contain all the messages in the system

Figure 5 : CLASS MYSTACK AND CONSTRUCTOR These three functions take on different tasks. isEmpty would take the job of checking the emptiness of the stack. The push function will make sure that the element adding in won’t overflow the stack. Lastly, the toString will turn the message in the stack into string so the system can easily handle. Figure 6 : ISEMPTY, TOSTRING AND PEEK FUNCTION

For the Push function, this is the class that will insert the new message on top of the stack and also this functionality runs on O(1). Figure 7 : PUSH FUNCTION OF MYSTACK CLASS For the Pop function, this is the class where the program will remove the top or the most recent message out of the stack and it also runs on O(1). Figure 8 : POP FUNCTION IN MYSTACK CLASS Now, let’s move on to rebuilding a Queue of my own. Below are declaration of some needed variables and the constructor.

Figure 11 : ENQUEUE FUNCTION IN MYQUEUE CLASS DeQueue function allows the program to remove the message form the front of the Queue and it also runs on O(1). Figure 12 : DEQUEUE FUNCTION IN MYQUEUE CLASS The toString will turn the message in the stack into string so the system can easily handle. Figure 13 : TOSTRING FUNCTION IN MYQUEUE CLASS

MAIN CLASS

Figure 14 : IMPORTED LIBRARIES AND VARIABLES DECLARATION Figure 15 : MAIN FUNCTION Figure 16 : ENTERMESSAGE FUNCTION

Figure 19 : STACKSTART FUNCTION HANDLE ERRORS BY EXCEPTION No program or segment may be regarded finished until the necessary error handling has been implemented. Unwanted program failures are a disaster - at most, they bring disappointment since program users must redo minutes or hours of effort, but in life-critical programs, even the most minor program fault has the potential to kill someone if not treated appropriately. If a mistake is fatal in the sense that a program cannot reasonably continue, it must be able to "die gracefully." This implies that it must

  • Inform its user (s) of the reason for its death
  • Save as many program states as feasible. The process of responding to and recovering from erroneous circumstances in your software is known as error handling. When running Java code, several mistakes might occur, including coding errors produced by the programmer, faults caused by incorrect input, and other unexpected factors. When an error occurs, Java will generally stop and display an error message. This is referred to as throwing an exception in Java (throw an error). An Exception is a one-of-a-kind occurrence, generally a mistake, that necessitates specific handling. Using an uninitialized reference, dividing by zero, exceeding the length of an array, or the JVM not being able to allocate objects on the heap can all result in an exception. The try-catch block is used to implement the catch and throw exception handling pattern. The try statement lets you to design a block of code that will be checked for errors while it runs. If an error occurs in the try block, the catch statement allows you to specify a block of code that will be executed.

In Java, the catch block is used to manage Exceptions by defining the exception type in the argument. The stated exception must be either a parent class exception or a generated exception type. The best way, however, is to define the created kind of exception. The catch block should only be used after the try block. With a single try block, you may utilize many catch blocks. Figure 20 : TRY CATCH SENTENCE IN POP FUNCTION Figure 21 : THROW ILLEGAL STATE EXCEPTION AT ENSURENONEMPTY FUNCTION

instead of years You can complete these flexible and affordable online learning programs on your own schedule and you will benefit from interactive the system informs to retype the message message went out of bounds and retyping required (Screenshot no.2) 3 Type in messages separated with periods Earn a certificate from a world class university. Build job ready skills to grow your career in months instead of years. You can complete these flexible and affordable online learning programs. Messages will be separated and saved to Queue one by one, the messages will be sent one by one The message couldn’t separate itself and the message was sent fully (Screenshot no.3) Passed 4 Type in message that doesn’t contain anything Firstly, the system will inform that the message goes over 250 characters then the system informs to retype the message The message is sent unsuccessfully with the notification of the message went numb and retyping required (Screenshot no.4) Passed Table 1 : TEST CASES

iii. Result

  • I. IMPLEMENT ADT & ALGORITHMS
      1. Description of your application
      1. Flowchart and explanations
      1. ADT (P4, P5)
      • i. Implement.....................................................................................................................................................
      • ii. Test (P5)
      • iii. Result
  • II. ANALYSIS
      1. Big O (P6)
      1. Analyze performances (P7)
  • III. References
  • Figure 1: FLOWCHART OF MAIN FUNCTION, ENTERMESSAGE FUNCTION AND TRANSFERMESSAGE FUNCTION FIGURES AND TABLES
  • Figure 2: FLOWCHART FOR STACKPROCESS FUNCTION AND STACKSTART FUNCTION
  • Figure 3: BUFFER CONSTRUCTOR
  • Figure 4: SOME FUNCTIONS FOR BUFFER CLASS
  • Figure 5: CLASS MYSTACK AND CONSTRUCTOR
  • Figure 6: ISEMPTY, TOSTRING AND PEEK FUNCTION
  • Figure 7: PUSH FUNCTION OF MYSTACK CLASS
  • Figure 8: POP FUNCTION IN MYSTACK CLASS
  • Figure 9: CLASS MYQUEUE AND CONSTRUCTOR
  • Figure 10: PEEK, ENSURENONEMPTY AND TOSTRING FUNCTION
  • Figure 11: ENQUEUE FUNCTION IN MYQUEUE CLASS
  • Figure 12: DEQUEUE FUNCTION IN MYQUEUE CLASS
  • Figure 13: TOSTRING FUNCTION IN MYQUEUE CLASS
  • Figure 14: IMPORTED LIBRARIES AND VARIABLES DECLARATION
  • Figure 15: MAIN FUNCTION
  • Figure 16: ENTERMESSAGE FUNCTION
  • Figure 17: TRANSFERMESSAGE FUNCTION.................................................................................................................
  • Figure 18: STACKPROCESS FUNCTION
  • Figure 19: STACKSTART FUNCTION
  • Figure 20: TRY CATCH SENTENCE IN POP FUNCTION
  • Figure 21: THROW ILLEGAL STATE EXCEPTION AT ENSURENONEMPTY FUNCTION
  • Figure 22: THROW OVERFLOW EXCEPTION AT ENQUEUE FUNCTION
  • Figure 23: THROW NEW ILLEGAL STATE EXCEPTION AT PUSH FUNCTION
  • Figure 24: SCREENSHOT NO.1
  • Figure 25: SCREENSHOT NO.2
  • Figure 26: SCREENSHOT NO.3
  • Figure 27: SCREENSHOT NO.4
  • Figure 28: EXAMPLE FOR O(1)
  • Figure 29: EXAMPLE FOR O(n)
  • Figure 30: EXAMPLE FOR O(n^2 )
  • Figure 31: EXAMPLE FOR O(log n)...............................................................................................................................
  • Figure 32: BIG O CHEAT SHEET
  • Table 1: TEST CASES
  • Table 2: EXECUTED TIME OF SOME EXAMPLES
  • Table 3: EXECUTED TIME OF SOME EXAMPLES
  • Figure 24 : SCREENSHOT NO.
  • Figure 25 : SCREENSHOT NO.
  • Figure 26 : SCREENSHOT NO.
  • Figure 27 : SCREENSHOT NO.