Assignment 2 - Data Structure & Algorithms - PASS, Assignments of Computer Science

Assignment 2 - Data Structure & Algorithms - PASS

Typology: Assignments

2021/2022
On special offer
30 Points
Discount

Limited-time offer


Uploaded on 12/26/2022

minh-huy-huynh
minh-huy-huynh 🇻🇳

4.7

(58)

39 documents

1 / 36

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Huynh Minh Huy
UNIVERSITY OF GREENWICH | GCD1001
Assignment 2
DATA STRUCTURE & ALGORITHMS
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
Discount

On special offer

Partial preview of the text

Download Assignment 2 - Data Structure & Algorithms - PASS and more Assignments Computer Science in PDF only on Docsity!

Huynh Minh Huy

UNIVERSITY OF GREENWICH | GCD

Assignment 2

DATA STRUCTURE & ALGORITHMS

ASSIGNMENT 2 FRONT SHEET Qualification BTEC Level 5 HND Diploma in Computing Unit number and title Unit 19: Data Structures and Algorithms Submission date 25/12/2022 Date Received 1st submission Re-submission Date Date Received 2nd submission Student Name Huynh Minh Huy Student ID GCD Class GCD1001 Assessor name Ho Van Phi 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 (^) Huy Grading grid P 4 P 5 P 6 P 7 M 4 M 5 D3 D

ACKNOWLEDGEMENT First of all, I would like to thank the curators of the University of Greenwich, who make these courses available to students. I would like to thank all the authors and researchers who have researched about this data. In addition, I would like thank speaker Ho Van Phi for his very good and professional lectures and tutorials. Finally, I would like to thank the teachers and responsible staff of our university for providing a good learning environment.

Table of Contents INSTRODUCTION .................................................................................................................................................... 6 CHAPTER 1: IMPLEMENT COMPLEX DATA STRUCTURES AND ALGORITHMS. ................................. 7 I. Implement a complex ADT and algorithm in an executable programming language to solve a well defined problem. (P4).............................................................................................................................................. 7

  1. Program’s Requirement. ..................................................................................................................................... 7
  2. Implement Program in Java Programming Language. ............................................................................ 7 II. Implement error handling and report test results. (P5) .......................................................................... 15
  3. Error Handling. .................................................................................................................................................... 15
  4. Test Plan and Test Log. ..................................................................................................................................... 17 CHAPTER 2: ASSESS THE EFFECTIVENESS OF DATA STRUCTURES AND ALGORITHMS. ............ 25 I. Discuss how asymptotic analysis can be used to assess the effectiveness of an algorithm. (P6) 25
  5. How asymptotic analysis can be used to assess the effectiveness of an algorithm. ................. 25 II. Determine two ways in which the efficiency of an algorithm can be measured, illustrating your answer with an example. (P7) .......................................................................................................................... 28
  6. Time Complexity. ................................................................................................................................................ 28
  7. Space Complexity. ............................................................................................................................................... 30 CONCLUSION ......................................................................................................................................................... 33 References ............................................................................................................................................................. 34

INSTRODUCTION Data Structures are the programmatic method of storing data in order for it to be used efficiently. Almost every corporate program makes use of numerous sorts of data structures in some fashion. This lesson will provide you with a solid grasp of Data Structures, which is required to comprehend the complexity of enterprise level programs and the requirement for algorithms and data structures. An algorithm is a step-by-step technique that outlines a collection of instructions that must be followed in a certain order to get the intended result. Algorithms are often developed independently of underlying languages, which means that an algorithm may be implemented in more than one programming language. Following the presentation of ADT from the previous report assignment 1. In this report assignment 2, I utilized ADT such as Queue or Stack to create a Send Message program with certain requirements, and I analyzed the program's performance.

CHAPTER 1: IMPLEMENT COMPLEX DATA STRUCTURES AND ALGORITHMS. I. Implement a complex ADT and algorithm in an executable programming language to solve a well defined problem. (P4)

  1. Program’s Requirement.  Message transfer program: The message transmission program system has a queue message buffer, and processing requires a message stack. They must develop ADTs and algorithms for these two structures, as well as build a sample version containing a message of no more than 250 characters. The example should highlight some of the most important operations of these structures. Despite the fact that this is a demo, errors should be handled appropriately with exceptions, and specific tests should be run to demonstrate the correctness of algorithms and operations.  This programming include: - Making use of an ADT (Stack / Queue) as a buffer during data transmission and reception. - Error handling: utilize the Exception catcher - the "try... catch" block - to catch and manage any errors that occur during program execution. - Run the program and report the results (Test case, test log).
  2. Implement Program in Java Programming Language. Class Queue: public class Queue { int MAX; char[] Q; int front = - 1 ; int rear = - 1 ; // init QUEUE public Queue(int M ){ Q = new char[ M ]; this.MAX = M ; } boolean isEmpty(){ return (front == - 1 ); }

Class Messenger: public class Messenger { int bufferLimit = 150 ; int messageLimit = 250 ; String S2= ""; Runtime rt = Runtime. getRuntime (); // long beforeUsedMem=Runtime.getRuntime().totalMemory()- Runtime.getRuntime().freeMemory(); // long afterUsedMem=Runtime.getRuntime().totalMemory()- Runtime.getRuntime().freeMemory(); // long actualMemUsed=afterUsedMem-beforeUsedMem; long beforeUsedMem; long afterUsedMem; public Messenger() { } public void sendingMessage(String messageSource ){ rt.gc(); beforeUsedMem = rt.totalMemory() - rt.freeMemory(); Queue myQueue = new Queue(bufferLimit); char[] S1 = messageSource .toCharArray(); int n = 0 ; try { while(n < S1.length && n < messageLimit) { while(myQueue.count() < bufferLimit && n < messageLimit) { myQueue.enQueue(S1[n]); n++; if (myQueue.isFull()){ afterUsedMem = (rt.totalMemory()-rt.freeMemory()); } } // Measure if (afterUsedMem == 0 ){ afterUsedMem = (rt.totalMemory()-rt.freeMemory()); } // When buffer full push

while(myQueue.count()!= 0 ){ S2 = S2 + myQueue.deQueue(); } } } catch (Exception e ){ while(myQueue.count() != 0 ){ S2 = S2 + myQueue.deQueue(); } } } public void printMessage(){ System.out.println("(S2) This is your message:"); System.out.println(S2); System.out.println(); } // public void printMemoryUsage(){ // System.out.println("Amount of used memory: " + used_mem); // } } Figure 2 : Class Messenger. Class TimeExcution: public class TimeExecution { double start; double stop; public TimeExecution(){ start = stop = 1 ; } public double Elapse(){ return stop - start; } public void Start(){ start = System. currentTimeMillis (); }

// Show message messenger.printMessage(); // Show successfully sent message System.out.println("Successfully Sent " + messenger.S2.length() + " Characters!"); System.out.println(); // Show buffer size System.out.println("Buffer Size: " + messenger.bufferLimit); System.out.println(); // Show time execution System.out.println("Executed time: " + timeExecution.Elapse() + " millisecond"); System.out.println(); // Show memory System.out.println("Memory Usage before send message: " + (messenger.beforeUsedMem) / 1024 + " KB"); System.out.println(); System.out.println("Memory Usage after send message: " + messenger.afterUsedMem / 1024 + " KB"); System.out.println(); System.out.println("Memory used: " + (messenger.afterUsedMem- messenger.beforeUsedMem)/ 1024 + " KB"); System.out.println(); } else { // Show error System.out.println("Your input too long, up to " + sourceMessage.length()

  • " characters, " + "some part of message might not get send," + " because we only send messages with a maximum of " + messenger.messageLimit + " characters."); System.out.println(); // Time the function timeExecution.Start();

// Send message messenger.sendingMessage(sourceMessage); timeExecution.Stop(); // Show message messenger.printMessage(); // Show successfully sent message System.out.println("Successfully Sent " + messenger.S2.length() + " Characters!"); System.out.println(); // Show buffer size System.out.println("Buffer Size: " + messenger.bufferLimit); System.out.println(); // Show time execution System.out.println("Executed time: " + timeExecution.Elapse() + " millisecond"); System.out.println(); // Show memory System.out.println("Memory Usage before send message: " + (messenger.beforeUsedMem) / 1024 + " KB"); System.out.println(); System.out.println("Memory Usage after send message: " + messenger.afterUsedMem / 1024 + " KB"); System.out.println(); System.out.println("Memory used: " + (messenger.afterUsedMem- messenger.beforeUsedMem)/ 1024 + " KB"); System.out.println(); } //Show memory // System.out.println("Memory Usage before send message: " + messenger.); // System.out.println(); // System.out.println("Memory Usage after send message: " + messenger.afterUsedMem); // System.out.println(); // messenger.printMemoryUsage();

II. Implement error handling and report test results. (P5)

  1. Error Handling. An exception in Java is a circumstance that prevents the program's regular flow. It is an item that is hurled during playback. Run-time error handling is a technique for dealing with these annoyances. Five keywords are available in Java that are used to manage exceptions. Each is described in the following table. Keyword Description try The "try" keyword is used to designate a block in which an exception code should be placed. That means try block cannot be used by itself. Catch or finally must come after the try block. catch The "catch" block is used to handle the exception. It must be preceded by try block which means we can't use catch block alone. It can be followed by finally block later. finally The "finally" section is used to run the program's essential code. It is run whether or not an exception is handled. throw The "throw" keyword is used to throw an exception. throws The "throws" keyword is used to declare exceptions. It specifies that there may occur an exception in the method. It doesn't throw an exception. It is always used with method signature. Table 1 : Error Handling. To solve the problem in this application, we use "Try - Catch" exception handling, which has been applied to the Transfer Function to handle the user entered string, avoiding the inputted string message containing a huge number of values, such as this error: " Index was outside the bounds of the array." Figure 6 : Result when doesn't use Try-Catch. Instead of stopping the program, we handle the exception using try-catch by sending all of the remaining characters in the queue buffer to the message destination, as seen in the following code.

try { while(n < S1.length && n < messageLimit) { while(myQueue.count() < bufferLimit && n < messageLimit) { myQueue.enQueue(S1[n]); n++; if (myQueue.isFull()) { afterUsedMem = (rt.totalMemory()-rt.freeMemory()); } } // Measure if (afterUsedMem == 0 ) { afterUsedMem = (rt.totalMemory()-rt.freeMemory()); } // When buffer full push while(myQueue.count()!= 0 ) { S2 = S2 + myQueue.deQueue(); } } } catch (Exception e ){ while(myQueue.count() != 0 ){ S2 = S2 + myQueue.deQueue(); } } Figure 7 : Try-Catch Implementation. After using “Try – Catch” we could know where is bug in our program then fixed it to make the system work well.

Test No Function being tested Testing method Test Data Expected result 1 Test the system if input a NULL string message. Input nothing in the input phase and press Enter NULL Console display: “Your input is empty, please input again to send message!” 2 Test the system if input a string over 250 characters. ( characters) Input data in input phase and press Enter “You must produce at least 250 words for IELTS Writing Task 2. You will be given a topic to reply to, and your ability to do so will be evaluated. This will involve expressing and defending an opinion, addressing the subject, summarizing specifics, outlining issues, suggesting potential solutions, and supporting your writing with arguments.” Console display: “Your input too long, up to 341 characters, some part of message might not get send, because we only send messages with a maximum of 250 characters. (S2) This is your message: You must produce at least 250 words for IELTS Writing Task 2. You will be given a topic to reply to, and your ability to do so will be evaluated. This will involve expressing and defending an opinion, addressing the subject, summarizing specifics, ou Successfully Sent 250 Characters! Buffer Size: 150 Executed time: 18.0 millisecond Memory Usage before send message: 1857 KB Memory Usage after send message: 2103 KB Memory used: 245 KB ----< Thank you for using GreMes >---- ----< Press any key to exit >----“ 3 Test the system if input a string in range 0 – 250 characters. ( characters) Input data in input phase and press Enter “The Cup of Coffee has established itself as the local coffee business with the quickest growth. If you've already seen one on your neighborhood corner, look across the street to see another.” Console display: “The Cup of Coffee has established itself as the local coffee business with the quickest growth. If you've already seen one on your neighborhood corner, look across the street to see another. (S2) This is your message: The Cup of Coffee has established itself as the local coffee business with the quickest growth. If you've already seen one on your neighborhood corner, look across the street to see another. Successfully Sent 190 Characters!

Buffer Size: 150 Executed time: 22.0 millisecond Memory Usage before send message: 1845 KB Memory Usage after send message: 2090 KB Memory used: 245 KB ----< Thank you for using GreMes >---- ----< Press any key to exit >----“ 4 Test program when input message that have special characters Input data in input phase and press Enter “Data Structure & Algorithms is important” Console display: “Data Structure & Algorithms is important. (S2) This is your message: Data structure & algorithms are crucial. Successfully Sent 40 Characters! Buffer Size: 150 Executed time: 18.0 millisecond Memory Usage before send message: 1845 KB Memory Usage after send message: 0 KB Memory used: - 1845 KB ----< Thank you for using GreMes >---- ----< Press any key to exit >----“ 5 Test time and space methods Input data in input phase and press Enter “The Highlands Coffee has established itself as the local coffee business with the quickest growth. If you've already seen one on your neighborhood corner, look across the street to see another.” Console display: “Executed time: 20.0 millisecond Memory Usage before send message: 1845 KB Memory Usage after send message: 2090 KB Memory used: 245 KB ----< Thank you for using GreMes >---- ----< Press any key to exit >----“ Table 2 : Test Case.