Você está na página 1de 10

ASSIGNMENT FRONT SHEET <No.

2>
Qualification

Edexcel BTEC Level 5 HND Diploma in Computing and Systems


Development

Unit number and


title

Unit 34: Data Structures and Algorithms

Assignment due

16-08-2016

Learners name

Assignment submitted

27-09-2016

Assessor name

TRAN PHUOC SINH

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
P3.
1

P3.
2

M1

M2

M3

D1

D2

D3

Assignment title

Investigating and Applying String Manipulating Techniques

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

LO3. Understand how strings are structured and processed

3.1 explain common


string operations and
their practical
applications
3.2 demonstrate the
outcome of string
operations in specified
algorithms.

Explain and give examples


about String operations like
concatenation, find
character, length, lowercase,
substring, trim, etc.

1.1

Write algorithms or programs


to demonstrate String
Operations

1.2

Assessment criteria

Expected Evidence

Feedback
(note on Merit/Distinction if applicable)

Merit descriptor No. (M1)


Merit descriptor No. (M2)
Merit descriptor No. (M3)
Distinction descriptor No.
(D1)
Distinction descriptor No.
(D2)
Distinction descriptor No.
(D3)

Summative feedback

Assessors
Signature

Date

3.1 Explain and give examples about String operations like concatenation, find
character, length, lowercase, substring, trim, etc.

String Concatenation in Java


In java, string concatenation forms a new string that is the combination of multiple
strings. There are two ways to concat string in java:
1. By + (string concatenation) operator
2. By concat() method
1) String Concatenation by + (string concatenation) operator
Java string concatenation operator (+) is used to add strings. For example:
class StringConcatenation{
public static void main(String args[]){
String s="DSA"+" BTECH";
System.out.println(s);//DSA BTECH
}
}

2) String Concatenation by concat() method


The String concat() method concatenates the specified string to the end of current
string. For example:
class StringConcatenation{
public static void main(String args[]){
String s1="DSA "; String s2="BTECH";
String s3=s1.concat(s2);
System.out.println(s3);//DSA BTECH
} }

Java String charAt


The java string charAt() method returns a char value at the given index number. The
index number starts from 0. For example:
public class CharAtExample{
public static void main(String args[]){
String name="javastring";
char ch=name.charAt(4);//returns the char value at the 4th index
System.out.println(ch); //s
}}

Java String length


The java string length() method length of the string. It returns count of total number of
characters. The length of java string is same as the unicode code units of the string. For
example:
public class LengthExample{
public static void main(String args[]){
String s1="javastring";
String s2="java";
System.out.println("string length is: "+s1.length());//10 is the length of
javastring string
System.out.println("string length is: "+s2.length());//4 is the length of
java string
}
}

Java String toLowerCase()


The java string toLowerCase() method returns the string in lowercase letter. In other
words, it converts all characters of the string into lower case letter. For example:
public class StringLowerExample{
public static void main(String args[]){
String s1="JAVA HELLO stRIng";
String s1lower=s1.toLowerCase();
System.out.println(s1lower); // returns java hello string
}
}

Java String substring


The java string substring() method returns a part of the string. We pass begin index and
end index number position in the java substring method where start index is inclusive and
end index is exclusive. In other words, start index starts from 0 whereas end index starts
from 1. For example:
public class SubstringExample{
public static void main(String args[]){
String s1="javastring";
System.out.println(s1.substring(2,4));//returns va
System.out.println(s1.substring(2));//returns vastring
}
}

Java String trim


The java string trim() method eliminates leading and trailing spaces. The unicode value of
space character is '\u0020'. The trim() method in java string checks this unicode value
before and after the string, if it exists then removes the spaces and returns the omitted
string. The string trim() method doesn't omits middle spaces. For example
public class StringTrimExample{
public static void main(String args[]){
String s1=" hello string ";
System.out.println(s1+"java");//without trim() returns
java
System.out.println(s1.trim()+"java");//with trim() returns
stringjava

hello string
hello

}
3.2 Write algorithms or programs to demonstrate String Operations

In this case, I use LSD algorithm to arrange the following numbers (in string
representations) in ascending order: 158, 124, 238, 707, 608, 250,
888. The algorithm is
public class LSD_Sort {
public static void sort(String[] a, int w) {
int n = a.length;
int R = 256; // extend ASCII alphabet size
String[] aux = new String[n];
for (int d = w-1; d >= 0; d--) {
// sort by key-indexed counting on dth character

// compute frequency counts


int[] count = new int[R+1];
for (int i = 0; i < n; i++)
count[a[i].charAt(d) + 1]++;
// compute cumulates
for (int r = 0; r < R; r++)
count[r+1] += count[r];
// move data
for (int i = 0; i < n; i++)
aux[count[a[i].charAt(d)]++] = a[i];
// copy back
for (int i = 0; i < n; i++)
a[i] = aux[i];
}
}

Main method is used to test the result:

public static void main(String[] args) {


String[] s= {"158","124","238","707","608","250","888"};
LSD_Sort.sort(s, 3);
for (String str:s) {
System.out.println(str);
}}
Here is the result after sorting:
9

Here is the trace of this algorithm:

Input
(w=3)
158
124
238
707
608
250
888

d=2
250
124
707
158
238
608
888

d=1

d=0

Output

608
707
124
238
158
250
888

124
158
238
250
608
707
888

124
158
238
250
608
707
888

10

Você também pode gostar