Docsity

Assignment 2 for Data Structures and Algorithms - 1649, Assignments 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
Date Received 1st
submission
Re-submission Date
Date Received 2nd
submission
Student Name
Phan Minh Tri
Student ID
GCD201632
Class
GCD0904
Assessor name
Pham 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
Grading grid
P4
P5
P6
P7
M4
M5
D3
D4
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16

Partial preview of the text

Download Assignment 2 for Data Structures and Algorithms - 1649 and more Assignments 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 Date Received 1st submission Re-submission Date Date Received 2nd submission Student Name Phan Minh Tri Student ID GCD Class GCD0904 Assessor name Pham 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 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:

Table of Contents

❖ Flowchart of enterMessages() Function

  • Firstly, the system prints the notification for entering the messages
  • Next, entering the input data at the console
  • Finally, the system divides it into many different messages by comma ❖ Flowchart of transfer() Function
  • Firstly, the system will run the loop based on the number of messages entered the system
  • Next, for each input message, the system will check against predefined errors
  • Next, if the message passes all errors, it will continue to be loaded into the queue. If the message has any errors, the system will report where the error is and force the user to re-enter the message in the correct format.
  • Finally, continuing loop execution with reading next message in input until the end and stop loop after that Figure 2 : Flowchart of enterMessages() Function Figure 3 : Flowchart of transfer() Function

❖ Flowchart of process() Function

  • Firstly, the system will check if there is data in the queue for processing
  • Next, if so, the system will take that data out of the queue, if not, it will stop there
  • Finally, after taking the data out of the queue, the system will push that data to the stack ❖ Flowchart of result() Function
  • Firstly, the system will check if there is data in the stack for displaying
  • Next, if so, the system will take that data out of the stack, if not, it will stop there
  • Finally, after taking the data out of the stack, the system will print it to screen Figure 4 : Flowchart of process() Function Figure 5 : Flowchart of result() Function
  • Pop() Function and ensureNonEmpty() Function Figure 8 : pop() Function
  • isEmpty() Function Figure 9 : isEmpty() Function ❖ Queue ADT
  • Queue's constructors Figure 10 : Queue's Constructors
  • Offer() Function Figure 11 : offer() Function
  • Poll() Function Figure 12 : poll() Function
  • isEmpty() Function Figure 13 : isEmpty() Function
  • process() Function is used to process messages. Start with getting messages off the queue, then keep pushing those messages onto stack Figure 16 : process() Function
  • result() Function is used to print the messages to the screen after being removed from the stack Figure 17 : result() Function
  • Main Function is used to perform the main functions of the program Figure 18 : Main Function

1.2. The explanation of handling error

❖ An exception is traditionally defined as an occurrence that happens during program execution and breaks the usual flow of instructions. Java exceptions are specific events that signify that something terrible has occurred in the application and that it must either recover or quit. ❖ Java exception handling is vital because it helps to preserve the program's usual, desired flow even when unexpected occurrences occur. Programs may crash or requests may fail if Java exceptions are not handled. It is critical to manage Java exceptions to avoid the program from abruptly failing and losing data. A sudden system crash can be caused by a variety of factors, including inaccurate or unexpected data entry. ❖ The try – catch technique is the easiest way to handle exceptions. Put your code in the try block, and any Java exceptions thrown by the code are captured by one or more catch blocks. This function will capture any form of thrown Java exception. This is the most basic exception handling method. Figure 19 : Example of try – catch technique ❖ When implementing my program, there are some errors, exception like IndexOutOfBoundException (it is an unchecked exception which is thrown to indicate that an index of some sort is out of range), IOException, … Figure 20 : Handling error in my program

  • In transfer() method, I used catch block to catch and handle the “IndexOutOfBoundException” when index is out of 250 characters and “IOException” when user doesn’t type anything

3. Result

  • CHAPTER 1 – IMPLEMENT ADT & ALGORITHMS...........................................................................................................
    • I. Description of my application
        1. Scenario
        1. Flowchart and explanations
    • II. ADT (P4, P5)
        1. Implement.....................................................................................................................................................
        • 1.1. The explanation of Queue and Stack implementation
        • 1.2. The explanation of handling error
        1. Test (P5)
        1. Result
  • CHAPTER 2 – ANALYSIS
    • I. Big O (P6)
        1. Definition
        1. Examples
    • II. Analyze performances (P7)
        1. Analyze Queue ADT
        1. Analyze Stack ADT
        1. Process() function
        1. Analyze all program
  • Figure 1: Flowchart of the whole program Table of Figures
  • Figure 2: Flowchart of enterMessages() Function
  • Figure 3: Flowchart of transfer() Function
  • Figure 4: Flowchart of process() Function
  • Figure 5: Flowchart of result() Function
  • Figure 6: Class Node......................................................................................................................................................
  • Figure 7: push() Function
  • Figure 8: pop() Function..............................................................................................................................................
  • Figure 9: isEmpty() Function
  • Figure 10: Queue's Constructors
  • Figure 11: offer() Function
  • Figure 12: poll() Function
  • Figure 13: isEmpty() Function
  • Figure 14: enterMessages() Function
  • Figure 15: transfer() Function
  • Figure 16: process() Function
  • Figure 17: result() Function.........................................................................................................................................
  • Figure 18: Main Function
  • Figure 19: Example of try – catch technique
  • Figure 20: Handling error in my program
  • Figure 21: TC.1
  • Figure 22: TC.2
  • Figure 23: TC.3
  • Figure 24: TC.4
  • Figure 25: Example of O(n)
  • Figure 26: Example of O(1)
  • Figure 27: O(n^2)
  • Figure 28: O(logn)
  • Figure 29: Big O Notation Cheat Sheet
  • Table 1: Test Case
  • Table 2: Analyze offer() function
  • Table 3: Analyze poll function.....................................................................................................................................
  • Table 4: Analyze push() function
  • Table 5: Analyze process() function
  • Table 6: Analyze all program.......................................................................................................................................
  • Figure 21 : TC.
  • Figure 22 : TC.
  • Figure 23 : TC.
  • Figure 24 : TC.

CHAPTER 2 – ANALYSIS

I. Big O (P6)

1. Definition

❖ Big O, often known as "algorithm complexity," is a phrase used to describe the amount of time it takes to perform an algorithm. Big O is frequently used by programmers to assess the effectiveness of alternative methods to the same problem. ❖ Big O may be summed up in one sentence: The running duration, is determined by the input (input value), which increases as the program runs.

  • The running time duration – The exact running time of an algorithm is difficult to calculate. Because each computer with a different configuration will produce a different result based on the machine's CPU and RAM. So, rather than asking, "What precisely is the time consuming?" we could utilize Big O to ask, "How quickly does time run?"
  • Determined by the input (input value) – When measuring how long an algorithm takes to execute, you can use precise time units like as seconds or milliseconds, however with Big O we will utilize input units referred to as "n." As a result, programmers frequently use the unit based on the difficulty of the algorithm with input "n" to compute the program's running time.
  • Increases as the program runs – People frequently believe that writing a sophisticated algorithm to handle small input values will be arduous at first, however as the algorithm progresses, the originally small number grows smaller and smaller. So, it's bigger, everything is different, and no one cares about the algorithm's complexity any longer. The programmer only concerns about other things that are consumed (time, storage) when the input value becomes greater with Big O analysis.

2. Examples

❖ O(n) : Linear complexity. Consider the following function, which computes the sum of numbers from 1 to n: Figure 25 : Example of O(n)

  • The above loop iterates n times for each value of n. As a result, the above program's execution time is dependent on the input value n. The preceding program has an O(n) complexity (really O(n+2).

❖ O(logn) : Figure 28 : O(logn)

  • An algorithm for searching for an element in the array arr with the value x has been sorted in ascending order. To discover that value, we will divide the array in half, and if that element is greater than the value to be found, we will look at the left half of that element, and vice versa. In the worst-case scenario, we'll have to partition until the partitioned array has just one element (if that's not what we're after, return - 1). As a result, the given algorithm has an O complexity (logn). ❖ Big O Notation Cheat Sheet Figure 29 : Big O Notation Cheat Sheet

II. Analyze performances (P7)

1. Analyze Queue ADT

❖ Offer() function is used to perform transfer() function Type of message Data input Result Time Short message Hello Long message Fifteen years ago Steve Jobs introduced the first iPhone. He described it as three devices in one: a widescreen iPod with touch controls a revolutionary mobile phone and a breakthrough internet communications device. iPhone is still making headlines Table 2 : Analyze offer() function ❖ Poll() operation Type of message Data input Result Time Short message Hello Long message Fifteen years ago Steve Jobs introduced the first iPhone. He described it as three devices in one: a widescreen iPod with touch controls a revolutionary mobile phone and a breakthrough internet communications device. iPhone is still making headlines Table 3 : Analyze poll function