Upload
maddison-turrill
View
213
Download
0
Embed Size (px)
Citation preview
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Structured Problem Solving2009-2010
Week 10: Java: Strings
Stewart Blakeway
FML 213
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
What we have done already• Seen what an algorithm is– a set of instructions that, if carried out, will lead
to a successful conclusion
• Learned how to represent algorithms in– Structured English– Flow charts
• Used variables to remember
2
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
What we have done already• Applied the top down, stepwise refinement
approach to creating algorithms• Looked at problems more oriented towards
being solved on a computer – stacks, queues, functions, procedures
• Seen how high level languages like Java are used to create programs with the aide of compilers
• The problem solving constructs in Java• Nested for loops
3
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
What we shall do today
• Strings• 3 examples– Reversing the order of characters in a word– Counting the number of words in a sentence– Analysing a URL (e.g. www.hope.ac.uk)
4
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Strings in computing
• Let Σ be an alphabet, a non-empty finite set.
• Elements of Σ are called symbols or characters.
• A string (or word) over Σ is any finite
sequence of characters from Σ.– Reference: “http://en.wikipedia.org/wiki/String_(computer_science)”
5
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Example
• Suppose Σ = { W, $, Þ,Ŷ, Ǿ}.
• Which of the following are valid strings over this alphabet: – ǾW$$– WORD– Þ– $Ŷw
6
Yes
No – ‘O’, ‘R’, ‘D’ not in given alphabet
Yes
No – ‘w’ not in given alphabet
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Strings in Java
• Alphabet includes all ASCII characters– ‘A’..’Z’– ‘a’..’z’– ‘0’..’9’– Punctuation marks– Certain “unprintable” characters like TAB,
NEWLINE are represented by ‘\t’, ‘\n’• Works next term in BlueJ – not here in Java Trainer
7
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Strings in Java
• Alphabet Σ = {‘A’..’Z’, ‘a’..’z’, ‘0’..’9’, …..}
8
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Java Strings
String s;
s = "Fred";
System.out.println(s);
9
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Concatenating strings
Concatenate:Etymology: Middle English, from Late
Latin concatenatus, past participle of concatenare to link together, from Latin com- + catena chain
Date: 15th century
10
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Concatenating strings with +
String a;String b; String c;
b = "Fred";c = "Bloggs";a = b + c;System.out.println(a);
11
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Accessing characters in Strings
String s;
s = "Fred";
s[0] is 'F's[1] is 'r's[2] is 'e's[3] is 'd'
12
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Accessing characters in Strings
String s;
s = "Fred";
System.out.println(s[0]);
13
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Accessing characters in Strings
String s;int i;s = "Fred";for (i=0; i<4; i++) { System.out.println(s[i]); }
14
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Accessing characters in Strings
String s;int i;s = "Fred";for (i=0; i<4; i++) { System.out.println(s[i]); }
15
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Accessing characters in Strings
String s;int i;s = "Fred";for (i=0; i<4; i++) { System.out.print(s[i]); }
16
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Accessing characters in Strings
String s;int i;s = "Fred";for (i=0; i<4; i++) { System.out.print(s[i]); }
17
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Example 1
18
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Write out a word in reverse: e.g. “Fred” -> “derF”
19
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Write out a word in reverse
String s;
int i;
s = "Fred";
for (i=3; i>=0; i--)
{
System.out.print(s[i]);
}
20
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Reversing a name of any length
21
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Reversing a word of any length
String s;
int i;
System.in.read(s);
for (i=3; i>=0; i--)
{
System.out.print(s[i]);
}
22
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
We need to be able to find the length of the string
• Java Trainer has a FUNCTION that returns the length of a String
• It is called length
String s;System.in.read(s);int n;n = length(s);
23
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Reversing a name of any length
String s;
int i;
int n;
System.in.read(s);
n = length(s);
for (i=n-1; i>=0; i--)
{
System.out.print(s[i]);
}
24
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Initial statement
• Count the number of words in a sentence
25
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Initial statement
• Count the number of words in a sentence by counting spaces then adding 1
26
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Initial statement
• Count the number of words in a sentence by counting spaces then adding 1– Assume single space between words– Assume no spaces at beginning or end of
sentence
27
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
First refinement
Get sentence
Initialise count
Count the spaces
Report the final count + 1
28
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Second refinement
Read Sentence
Count := 0
While (not reached end of sentence)
begin
deal with next character
end
Display Count + 1
29
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Second refinement
• Do we know before we start how many times we shall go round the loop?
• Yes – since we can use the length function to get the number of characters.
• So, we can use a for loop
30
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Second refinement
Read Sentence
Count := 0
For each character in the sentence
begin
deal with the character
end
Display Count + 1
31
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Third refinement
Read Sentence Count := 0For each character in the sentence begin if (character = ' ') begin Count = Count + 1 end endDisplay Count + 1
32
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Counting the number of words in a sentence: in Java
String sentence;
int count;
int i;
count = 0;
System.in.read(sentence);
for (i=0; i<length(sentence); i++)
{
if (sentence[i] == ' ')
{
count = count + 1;
}
}
System.out.println("There are " + (count+1) + " words.");
33
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Example 3
34
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Analysing a URL• URL -> Universal Resource Locator• URL is the basis for communicating locations
of resources (data) on the web. – E.g. http://www.hope.ac.uk
• A URL consists of:– a protocol identifier (e.g. HTTP (hypertext transfer
protocol), FTP (file transfer protocol)– and a protocol-specific syntax further defining the
location.
35
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Analysing a URL
• In this example we shall analyse a HTTP protocol URL to extract information about the location of the host. – E.g. http://www.hope.ac.uk
36
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Analysing a URL
• In this example we shall analyse a HTTP protocol URL to extract information about the location of the host. – E.g. http://www.hope.ac.uk
37
Host name
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Analysing a URL
• http://www.hope.ac.uk
38
Country
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Analysing a URL
• http://www.hope.ac.uk
39
Classification
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Analysing a URL
• http://www.hope.ac.uk
40
Organisation
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Analysing a URL
• http://www.hope.ac.uk
41
Service
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Analysing a URL
• http://www.hope.ac.uk
• Country = uk (United Kingdom)
• Classification = ac (Academic)• Organisation = hope (Liverpool
Hope)• Service = World Wide Web (Hope’s web
server)
42
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Analysing a URL
• http://moodle.hope.ac.uk
• Country = uk (United Kingdom)
• Classification = ac (Academic)• Organisation = hope (Liverpool Hope)• Service = moodle (Hope’s
VLE)
43
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Analysing a URL
• Country– uk (United Kingdom)– de (Germany)– fr (France)
• Classification– ac (academic)– gov (government)– org (organisation)
44
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Analysing a URL – .COM is different
• http://www.microsoft.com
45
USA (or an organisation based
anywhere)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Analysing a URL – .COM is different
• http://www.microsoft.com
46
Organisation
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Analysing a URL – .COM is different
• http://www.microsoft.com
47
Service
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Analysing a URL
• http://www.microsoft.com
• Organisation based anywhere• Organisation = microsoft• Service = world wide web
48
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
First we design a solution
49
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Initial statement
Analyse a URL
50
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
First refinement
Get URL
Analyse the URL
Report the results
51
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Getting the URL
Get URL
Analyse the URL
Report the results
52
We can store the URL in a variable called URL.
No further refinement necessary
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Second refinement – what output do we expect
Get URLAnalyse the URLif (URL has .com) then begin display .com report endElse begin display country report end
53
Knowing what the final output should be is a key guide to determining what the analysis should be finding out
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Third refinement – what output do we expect
display .com report
54
display “Organisation based anywhere”
display “Organisation: “, Organisation
display “Service: “, Service
We need the analysis to record Organisation and Service
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Third refinement – what output do we expect
display country report
55
display “Country: ”, Country
display “Organisation: “, Organisation
display “Service: “, Service
We need the analysis to record Country, Organisation and Service
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Second refinement – analyse the URL
• We keep two examples in front of us:– http://moodle.hope.ac.uk case 1– http://www.microsoft.com case 2
• We need to extract the separate parts of the URLs
56
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Second refinement – analyse the URL
• We keep two examples in front of us:– http://moodle.hope.ac.uk case 1– http://www.microsoft.com case 2
• We identify the words separated by full stops: ‘.’• Start from the back and stop at the first ‘/’– In case 1: uk, ac, hope, moodle (4 words)– In case 2: com, microsoft, www (3 words)
57
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
We now know what the analysis needs to do
Identify the parts of the URL as separate wordsif (last word is “com”) then begin copy the other words into the variables Organisation and
Service endelse begin Copy the words into the variables Country, Classification, Organisation and Service end
58
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Nearly there
• We have got most of the algorithm into a level of detail that we can translate directly into Java code
• The only part remaining is how to split the URL into separate words.
59
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Identify the parts of the URL as separate words – in Java
i = length(url) - 1;
word1 = "";
while (url[i] != '.')
{
word1 = url[i] + word1;
i--;
}
60
First word
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Identify the parts of the URL as separate words – in Java
i--;
word2 = "";
while (url[i] != '.')
{
word2 = url[i] + word2;
i--;
}
61
Second word
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
The whole program in 6pt. Copy and paste the two parts into Java Trainer
String word1;
String word2;
String word3;
String word4;
String url;
String country;
String classification;
String organisation;
String service;
int i;
url = "http://www.hope.ac.uk";
i = length(url) - 1;
word1 = "";
while (url[i] != '.')
{
word1 = url[i] + word1;
i--;
}
i--;
word2 = "";
while (url[i] != '.')
{
word2 = url[i] + word2;
i--;
}
i--;
word3 = "";
while ((url[i] != '.') && (url[i] != '/'))
{
word3 = url[i] + word3;
i--;
}
i--;
word4 = "";
while ((url[i] != '.') && (url[i] != '/'))
{
word4 = url[i] + word4;
i--;
}
if (word1 == "com")
{
organisation = word2;
service = word3;
}
else
{
country = word1;
classification = word2;
organisation = word3;
service = word4;
}
if (word1 == "com")
{
System.out.println("Organisation based anywhere");
System.out.println("Organisation: " + organisation);
System.out.println("Service: " + service);
}
else
{
System.out.println("Country: " + country);
System.out.println("Classification: " + classification);
System.out.println("Organisation: " + organisation);
System.out.println("Service: " + service);
}
62
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Any Questions?