ASM2-Java Programming
ASM2-Java Programming
Class: GCS0903B
ID: GCS200345
Unit number and title Unit 19: Data Structures and Algorithms
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.
Grading grid
P4 P5 P6 P7 M4 M5 D3 D4
2
Summative Feedback: Resubmission Feedback:
IV Signature:
1
Assignment Brief 2 (RQF)
Higher National Certificate/Diploma in Business
Submission Format:
Format:
● The submission is in the form of an individual written report and a presentation. This should be
written in a concise, formal business style using single spacing and font size 12. You are required
to make use of headings, paragraphs and subsections as appropriate, and all work must be
supported with research and referenced using the Harvard referencing system. Please also provide
a bibliography using the Harvard referencing system.
Submission
● Students are compulsory to submit the assignment in due date and in a way requested by the
Tutor.
● The form of submission will be a soft copy posted on http://cms.greenwich.edu.vn/.
● Remember to convert the word file into PDF file before the submission on CMS.
Note:
2
● The individual Assignment must be your own work, and not copied by or from another student.
● If you use ideas, quotes or data (such as diagrams) from books, journals or other sources, you
must reference your sources, using the Harvard style.
● Make sure that you understand and follow the guidelines to avoid plagiarism. Failure to comply
this requirement will result in a failed assignment.
Assignment scenario
3
Learning Outcomes and Assessment Criteria (Assignment 2)
Contents
Assignment Brief 2 (RQF) ......................................................................................................2
Higher National Certificate/Diploma in Business ...............................................................2
P4 Implement a complex ADT and algorithm in an executable programming
language to solve a well-defined problem. ...............................................................5
P5 Implement error handling and report test results. .............................................7
P6 Discuss how asymptotic analysis can be used to assess the effectiveness of
an algorithm ................................................................................................................9
P7 Determine two ways in which the efficiency of an algorithm can be
measured, illustrating your answer with an example. ...........................................10
References: ...............................................................................................................11
4
P4 Implement a complex ADT and algorithm in an executable
programming language to solve a well-defined problem.
To develop data structure and algorithms I have chosen ADT array and write in the Java
language. Here is my code to implement an algorithm:
This is the App file which is take responsible to run the algorithm and give us back the
result
import java.util.Random;
public class App{
public static void main(String[] args){
Stack myStack = new Stack();
Queue myQueue = new Queue();
for (int i = 0; i < 25;i++){
String temp = "Hello ";
Random rand = new Random();
temp += rand.nextInt(100);
myQueue.enqueue(temp);
System.out.println("Messages sent from user 1: " + temp);
}
while (!myQueue.isEmpty()){
String temp = myQueue.dequeue();
myStack.push(temp);
}
System.out.println();
while (!myStack.isEmpty()){
System.out.println("Messages received from user 2: " + myStack.pop());
}
}
}
In this main function, I simulate a chatbox of user 1 and user 2. Whenever user 1 sends a
message, the message enqueue to the queue. Then user 2 saw that the program will
dequeue all of the messages in the stack to see the message.
5
This is queue, the data structure which have enqueue and dequeue function to put in and
take out the data.
public class Queue{
private int total = 0;
private String[] queue;
private int rear;
private int front;
public Queue(){
queue = new String[25];
rear = 0;
front = -1;
}
public void enqueue(String s){
if (total < 25){
total++;
front++;
queue[front] = s;
}
}
public String dequeue(){
if (total > 0){
total--;
String temp = queue[rear++];
if (rear == queue.length){
rear = 0;
front = -1;
}
6
return temp;
}
return "";
}
public boolean isEmpty(){
return total == 0;
}
public boolean isFull(){
return total == queue.length;
}
}
This is the stack, the data structure and its function have a mission that checks out the
data and stops it when it reaches the limit.
public class Stack{
private String[] stack;
private int index;
private int total = 0;
public Stack(){
stack = new String[25];
index = -1;
}
public void push(String s){
if (index < 24){
total++;
stack[++index] = s;
}
}
public String pop(){
if (index >= 0){
total--;
return stack[index--];
}
return "";
}
public boolean isEmpty(){
return total == 0;
}
public boolean isFull(){
return total == 25;
}
}
8
P6 Discuss how asymptotic analysis can be used to assess the
effectiveness of an algorithm
An algorithm's effectiveness can be determined using Big O Notation. As the input
increases, it measures the amount of time it takes to run your function. Alternatively, how
well does the function scale?
Time complexity and spatial complexity are the two components that makeup efficiency
measurement. The length of the function's computation-step execution time is known as its
time complexity. The function's utilization of memory has an impact on space complexity.
With the help of two search algorithms, this blog will demonstrate time complexity.
There are two such methods: time complexity and spatial complexity.
• Time Complexity: An algorithm's time complexity measures how long it takes to
complete a task in relation to the size of the input. It should be noted that the time
required to complete the procedure depends on the length of the input rather than
the machine's real processing speed.
The total operations for an input length on N are calculated in order to determine
the time complexity of an algorithm. It is assumed that each operation takes a fixed
amount of time, c. To comprehend the computation process, consider the following
example: Let's say that the task at hand is to determine whether a pair (X, Y) exists
in an array (A) with N elements and a sum of Z. The easiest approach is to take into
account each pair and determine whether or not it fits the requirement.
10
• Space Complexity: The amount of space an algorithm uses to operate as a function
of input length is measured by its space complexity. Here's an illustration: Imagine
you are trying to determine the frequency of an array's elements.
In addition to spatial complexity, there is auxiliary space. The primary distinction is
that although auxiliary space quantifies additional space utilized in the method
beyond the input given, space complexity quantifies the total amount of space
needed by the algorithm.
References:
Medium. 2022. Introduction to Big O Notation. [online] Available at:
<https://towardsdatascience.com/introduction-to-big-o-notation-820d2e25d3fd> [Accessed 26 June
2022].
2022. [online] Available at: <https://codenza.app/data-structures/> [Accessed 26 June 2022].
Stack, A., 2022. Array Implementation of Stack | Java Stack Implementation using Array. [online] EDUCBA.
Available at: <https://www.educba.com/array-implementation-of-stack/> [Accessed 26 June 2022].
11