Assignment 1 Frontsheet
Assignment 1 Frontsheet
1>
Qualification
Assignment due
16-08-2016
Learners name
Assignment submitted
27-09-2016
Assessor name
Learner declaration:
I certify that the work submitted for this assignment is my own and research sources are fully acknowledged.
Learner signature
Date
27-09-2016
Grading grid
P1.
1
P1.
2
P1.
3
P2.
1
P2.
2
P2.
3
M1 M2
M3
D1
D2
D3
Assignment title
In this assignment, you will have opportunities to provide evidence against the following criteria.
Indicate the page numbers where the evidence can be found.
Assessment criteria
Expected evidence
Task
no.
Assessors Feedback
1.1
1.2
1.3
2.1
2.2 implement
opportunities for error
handling and reporting
Explain bout
where/why/how did you
apply error handling and
reporting in your work
Test cases
Test log and evidence
Assessment criteria
Expected Evidence
2.2
2.3
Feedback
(note on Merit/Distinction if applicable)
Assessors
Signature
Date
1.1 produce design specification for data structures explaining the valid
operations that can be carried out on the structures
Data structures used in this case are Singly Linked List, Linked implementation
of a Stack, Linked implementation of a Queue
Singly Linked List: A singly linked list is a list whose node includes two
datafields: info and next. The info field is used to store information, and this is
important to the user. The next field is used to link to its successor in this
sequence. The following image depicts a simple integer linked list.
12
Head
15
10
Tail
1.1: Singly linked list chart
Stack:
o A stackis a linear data structure that can be accessed only at one of its
ends for storing and retrieving data
o A stackis a Last In, First Out (LIFO) data structure
o Anything added to the stack goes on the top of the stack
o Anything removed from the stack is taken from the top of the stack
o Things are removed in the reverse order from that in which they were
inserted
Queue:
o A queueis a waiting line that grows by adding elements to its end and
shrinks by taking elements from its front
o A queue is a structure in which both ends are used: One for adding
new elements One for removing them
o A queue is an FIFOstructure: First In/First Out
In SMS, I use these data structures because its simple and suitable for
SMSs data. Singly linked list has so many advanatages:
DFD: This DFD shows how the system works, when customers order product, the
manager will check that product is available or not, if available the manager will
deliver that product to the customers and the customers also checkout that bill at
the same time to system. On the orther hand, the admin will have to maintain this
system regularly
ORDER
PRODDUCT
MANAGER
CUSTOMERS
DELIVER
PRODDUCT
AVAILABLE
PRODDUCT
CHECKOUT
CHECK
PRODDUCT
SMS
MAINTAIN
SYSTEM
Customers:
Order product
Checkout
to
the
system
Get product after
order
ADMIN
Admin:
Update system
Maintain
when
having errors
START
FALSE
VALIDATE INPUT
TRUE
FALSE
END
10
START
SELECT PRODUCT
FALSE
CHECK IF PRODUCT EXISTED IN ORDER
LIST
TRUE
FALSE
DELETE PRODUCT
END
11
FOUND
NOT
FOUND
SHOW PRODUCT
END
13
While(next!=null)
FALSE
Swap (current,next)
change = true
next= current.next
TRUE
BEGIN
FALSE
While(change)
do
15
16
TRUE
FALSE
INSERT CUSTOMER
END
17
START
SELECT CUSTOMER
FALSE
CHECK IF CUSTOMER EXISTED INT ORDER LIST
TRUE
FALSE
DELETE
CUSTOMER
18
END
START
INTPUT CUSTOMER,
PRODUCT, QUANTITY
ORDER
20
END
21
START
ADD TO LIST
ORDERDETAIL
END
22
23
Pseudo code:
END
26
27
BEGIN
them and then sorts the less widely spaced elements. This spacing is
termed
as interval. This interval is calculated based on Knuth's formula
procedure
shellSort()
as h =h*3+1 (h is interval with initial value 1)
A : array of items
o This algorithm is quite efficient for medium sized data sets as its
while
intervaland
< A.length
/3 do:complexity are of O(n) where n are no. of items.
average
worst case
Pseudo
code:
interval
= interval * 3 + 1
end while
while interval > 0 do:
for outer = interval; outer < A.length; outer ++ do:
valueToInsert = A[outer]
inner = outer;
while inner > interval -1 && A[inner - interval] >= valueToInsert do:
A[inner] = A[inner - interval]
inner = inner - interval
end while
A[inner] = valueToInsert
28
end for
interval = (interval -1) /3;
end while
end procedure
END
Quick sort
o Quick sort is a highly efficient sorting algorithm and is based on
partitioning of array of data into smaller arrays. A large array is
partitioned into two arrays one of which holds values smaller than
specified value say pivot based on which the partition is made and
another array holds values greater than pivot value.
29
o The quick sort partitions an array and then calls itself recursively twice
to sort the resulting two subarray. This algorithm is quite efficient for
large sized data sets as its average and worst case complexity are of
O(nlogn) where n are no. of items.
BEGIN
function partitionFunc(left, right, pivot)
leftPointer = left -1
rightPointer = right
while True do
while A[++leftPointer] < pivot do
//do-nothing
Pseudo code:
end while
while rightPointer > 0 && A[--rightPointer] > pivot do
//do-nothing
end while
if leftPointer >= rightPointer
break
else
swap leftPointer,rightPointer
end if
end while
swap leftPointer,right
return leftPointer
end function
END
30
BEGIN
procedure quickSort(left, right)
if right-left <= 0
return
else
pivot = A[right]
partition = partitionFunc(left, right, pivot)
quickSort(left,partition-1)
quickSort(partition+1,right)
end if
end procedure
END
1.27: Quick sort pseudo code
31
Merge sort
o Merge sort is a sorting technique based on divide and conquer
technique. With worst-case time complexity being (n log n), it is one of
the most respected algorithms.
o Merge sort first divides the array into equal halves and then combines
them in a sorted manner.
return merge( l1, l2 )
Pseudo code:
end procedure
end procedure
32
BEGIN
Heapsort(A as array)
BuildHeap(A)
for i = n to 1
swap(A[1], A[i])
Heap sort
The heapsort
involves preparing the list by first turning it into a max
n =algorithm
n-1
heap. The algorithm then repeatedly swaps the first value of the list with the
last value, decreasing
Heapify(A, 1)the range of values considered in the heap operation by
one, and sifting the new first value into its position in the heap. This repeats
BuildHeap(A
as array)
until the
range of considered
values is one value in length.
Pseudo code:
n = elements_in(A)
for i = floor(n/2) to 1
Heapify(A,i)
Heapify(A as array, i as int)
left = 2i
right = 2i+1
if (left <= n) and (A[left] > A[i])
else
max = left
max = i
swap(A[i], A[max])
max = right
Heapify(A, max)
33
BEGIN
o Radix sort iteratively orders all the strings by their n-th character in the
// The array to put the partially sorted array into
first iteration, the strings are ordered by their last character. In the second
int[]
tmp
= neware
int[old.length];
run,
the
strings
ordered in respect to their penultimate character. And
because the sort is stable, the strings, which have the same penultimate
// The number of 0s
character, are still sorted in accordance to their last characters. After n-th
intthe
j =strings
0;
run
are sorted in respect to all character positions.
// Move the
0s to the new array, and the 1s to the old one
Pseudo
code:
for (int i = 0; i < old.length; i++) {
// If there is a 1 in the bit we are testing, the number will be negative
boolean move = old[i] << shift >= 0;
// If this is the last bit, negative numbers are actually lower
if (shift == 0 ? !move : move) {
tmp[j] = old[i];
j++;
34
} else {
// It's a 1, so stick it in the old array for now
old[i - j] = old[i];
}
}
// Copy over the 1s from the old array
for (int i = j; i < tmp.length; i++) {
tmp[i] = old[i - j];
}
// And now the tmp array gets switched for another round of sorting
old = tmp;
}
return old;
}
END
35
36
1.31: Tail
recursion
example
tail(i-1);
}}
Non tail recursion:
The recursive call is not at the very end of a method implementation
public static void reverse() throws Exception {
char ch = (char)
System.in.read();
if(ch != '\n') {
reverse();
System.out.print(ch);
}
}
37
38
} order loadDataFromFile();
order displayData();
void saveDataToFile();
void insertNewOrder(String
String ccode,
double total);
Interface
Order: containsorderid,
some abstract
methods
void sortBycusCode();
}
39
And in order to add more than one product to order list, I also create
an Interface OrderDetail: contains some method such as
loadDataFromFile, displayData, savaDataToFile, insertNew
public interface IOrderDetail {
orderdetail loadDataFromFile();
orderdetail displayData();
void saveDataToFile();
orderdetail insertNew(String orderid, String pcode, String pname, int
quantity,double price, double total);
40
@Override
@Override
public product searchByCode(String code) {
@Override
public void deleteByCode(String code) {
@Override
public void sortByCode() {
@Override
public void deleteAfterxCode(String code) { }
public product search(product c, String code) {
private product delete(product n, String x) { }
41
@Override
public product loadDataFromFile() {
try (//file is store in this directory
Method loadDataFromFile():
is used to load data
ObjectInputStreamthis
ois =method
new ObjectInputStream(fis);)
{ from file
which is saved in the local folder directory
try {
42
Method displayData (): this method is used to show data from file after
loaded
@Override
@Override
public void saveDataToFile() {
public product displayData() {
try (//save data to file in this directory
return head;
FileOutputStream fos = new FileOutputStream("D:/DSA_product.txt");
}
ObjectOutputStream os = new ObjectOutputStream(fos);) {
os.writeObject(head);//write object to file
os.close();
Method saveDataToFile (): this method is used to save data to file in the
fos.close();
specific directory
} catch (IOException ex) {
ex.printStackTrace();
}
}
43
public product search(product c, String code) {// two parameters are passed to
this method
if (c == null || c.pcode.equals(code)) {//check if c is null or c has pcode equal
to code, then return c
return c;
} else {
return search(c.next, code);//call this method recursively
}
44
In this assignment I apply bubble sort to the data structure used because
it is simple, easy
to set up and also adequated to
the
data ifstructure,
I used
if (current.pcode.compareTo(next.pcode)
> 0)
{//check
current code
>
it to sort
product
list
and
order
list.
next code
Here an example
of sorting
change
= true;product by product code with bubble sort:
if (previous != null) {//then swap the nodes
product temp = next.next;
previous.next = next;
next.next = current;
current.next = temp;
45
} else {
product temp = next.next;
head = next;
next.next = current;
current.next = temp;
}
previous = next;
next = current.next;//move to next node
} else {//move to the next node
previous = current;
current = next;
next = next.next;
}
}
} while (change);
}
46
} else {
48
Customer_Controller:
implements Interfaces Customer and contains
@Override
some necessary methods
public void saveDataToFile() { }
@Override
public void insertNewCustomer(String code, String name, String phone) {}
@Override
public customer searchByCode(String code) {
@Override
public void deleteByCode(String code) {
49
@Override
50
Orderpublic
classclass
(Singly
List):
represent an order with its properties
orderLinked
implements
Serializable{
public String orderid;
public String ccode;
public double total;
public order next;
public order() {
51
@Override
public order displayData() {}
@Override
public void saveDataToFile() {}
@Override
public void insertNewOrder(String orderid, String ccode, double total) {}
public boolean isEmpty() { }
@Override
public void sortBycusCode() {}
}
52
}
}
Method displayData (): this method is used to show all order in order list
order o = head;//head will be assigned to an order object
head = head.next;//move to the next node
return o;//return object order
}
53
@Override
public void saveDataToFile() {}
@Override
public orderdetail insertNew(String orderid, String pcode,String pname, int
in your work
55
If data structure is null, to prevent error there will be an inform the user click on display da
56
.........
57
6. When user wants to delete a product after the last one, this is an
error, an error message will be displayed to the user
private void
btnDeleteAfterCodeProductActionPerformed(java.awt.event.ActionEvent
evt) {
String code = txtDeleteAfterCode.getText();
if (pc.searchByCode(code).next != null) { .. }
else {JOptionPane.showMessageDialog(rootPane,
areinform:
at the
There will "You
be an
58
59
What
is
being
tested
Product
code
Product
name
Product
quantit
y
Product
price
Custom
er code
Custom
er
name
Expected
outcome
Actual
outcome
String
Pxxx
(P002)
String
xxxx
(jeans)
Int(>=1,<
=200)
(120)
String
(P002)
Pa
Evidence
ss
or
fail
Pas Successful
s
String
(jeans)
Pas
s
Int (300)
Fail
Double(>=
1,<=3000)
(2000)
String
Cxxx
(C002)
Double
(2000)
Pas
s
String
(C00134)
Fail
String
xxxx
(Jack)
String
(Jack)
Pas
s
Successful
Successful
Successful
60
Custom
er
phone
Orderid
Successful
Total
price
String
(201609090
92400)
Double
(200.0)
Pas
s
String
(20160909
092400)
Double
(200.0)
Pas
s
Successful
10
loadDat
aFromFi
le()
Successful
display
Data()
Read from
file in
directory
(D:/DSA_pro
duct.txt)
Null
Pas
s
11
Read from
file in
directory
(D:/DSA_pr
oduct.txt)
Product list
12
saveDa
taToFile
()
File is
saved in
directory
(D:/DSA_pr
oduct.txt)
insertN Product
ewProd (P001,jea
uct()
ns200,200
,0)
searchB Product
yCode() (P001,jea
ns200,200
,0)
File is saved
in directory
(D:/DSA_pro
duct.txt)
Pas
s
Successful
Product
(P001,jean
s200,200,0)
Pas
s
Successful
Null
Fail
12
13
Fail
61
14
deleteB
yCode()
15
sortByC
ode()
deleteA
fterxCo
de()
16
Delete
product
(P001,jea
ns200,200
,0)
Sorted list
Delete
product
(P001,jean
s200,200,0)
Pas
s
Successful
Sorted list
Successful
Delete
product
(P001,jea
ns200,200
,0)
Null
Pas
s
Fail
17
search(
)
Product
(P001,jea
ns200,200
,0)
Null
Fail
18
loadDat
aFromFi
le()
Successful
display
Data()
saveDa
taToFile
()
Read from
file in
directory
(D:/DSA_ord
er.txt)
Order list
Pas
s
19
Read from
file in
directory
(D:/DSA_or
der.txt)
Order list
successful
File is
saved in
directory
(D:/DSA_or
der.txt)
Order
(20140915
093020,C
001,2000)
File is saved
in directory
(D:/DSA_ord
er.txt)
Pas
s
Pas
s
Order
(201409150
93020,C00
1,2000)
Pas
s
Successful
Sorted
Sorted order
Pas
Successful
20
21
insertN
ewOrde
r()
22
sortByc
Successful
62
order list
list
Read from
file in
directory
(D:/DSA_cu
stomer.txt)
Customer
list
Read from
Pas
file in
s
directory
(D:/DSA_cust
omer.txt)
Null
Fail
Successful
File is
saved in
directory
(D:/DSA_cu
stomer.txt)
insertN Customer
ewCust (C001,Ng
omer()
uyen,0903
020293)
searchB Customer
yCode() (C001,Ng
uyen,0903
020293)
Successful
Customer
(C001,Ngu
yen,090302
0293)
Null
Pas
s
Successful
28
deleteB
yCode()
Pas
s
Successful
29
loadDat
aFromFi
le()
Delete
customer
(C001,Ngu
yen,090302
0293)
Read from
file in
directory
(D:/DSA_ord
Pas
s
Successful
23
usCode
()
loadDat
aFromFi
le()
24
display
Data()
25
saveDa
taToFile
()
26
27
Delete
customer
(C001,Ng
uyen,0903
020293)
Read from
file in
directory
(D:/DSA_or
Fail
63
30
display
Data()
saveDa
taToFile
()
31
32
insertN
ew()
de4rdetail.t
xt)
Orderdetail
list
File is
saved in
directory
(D:/DSA_or
derdetail.tx
t)
Orderdetail
(20140915
093020,P
001,
Jeans,100
,2,200)
erdetail.txt)
Orderdetail
list
File is saved
in directory
(D:/DSA_ord
erdetail.txt)
Pas
s
Pas
s
Successful
Orderdetail
(201409150
93020,P00
1,
Jeans,100,2
,200)
Pas
s
Successful
Successful
System Requirements
Windows XP or later,Linux, MacOS
RAM: 128 MB; 64 MB for Windows XP (32-bit)
Disk space: 124 MB
JDK 6 or later
Compiling and running program
Compile the program from within the directory:
javac DSA_A1.java
After compiling the program successfully, run it from the directory above:
java DSA_A1
64
7
5
8
2
4
6
12
1
3
9
1
0
14
1
1
65
66
1
2
67
5
6
8
9
1
0
1
1
68
1
2
3
4
69
3
7
1
4
2
6
8
9
1
0
70
6: Click on this button will sort customers in the data structure and will
show in the table
7: Click on this button will open an insert order window
8: Click on this button will display all data in the table
9: Click on this button will show order detail which is selected in the table
10: Table is used to display all orders in the data structure
Input new order
71
8
1
0
72
6: Click on this button will add product to order list and will show in the
table
7: This table is used to show products in order list
8: This will show total price of the order
9: Click on this button will make ordering
10: Click on this button will close this window
Order detail
1: This will show orderid
5
6
START
LOAD DATA TO
FILE
73
PRODUCT
INSERT
CUSTOMER
DELETE
SORT
ORDER
SEARCH
DISPLAY
SAVE DATA TO
FILE
END
Positive: This programme is very simple, easy to handle, admin can fix any
error anytime
Negative:
Dont handle the amount of products saled per day and also the amount of
customers
74
75