63
www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Structured Problem Solving 2009-2010 Week 10: Java: Strings Stewart Blakeway FML 213 [email protected]

Structured Problem Solving 2009-2010

  • Upload
    holland

  • View
    31

  • Download
    0

Embed Size (px)

DESCRIPTION

Structured Problem Solving 2009-2010. Week 10: Java: Strings Stewart Blakeway FML 213 [email protected]. 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 - PowerPoint PPT Presentation

Citation preview

Page 1: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

Structured Problem Solving2009-2010

Week 10: Java: Strings

Stewart Blakeway

FML 213

[email protected]

Page 2: Structured Problem Solving 2009-2010

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

Page 3: Structured Problem Solving 2009-2010

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

Page 4: Structured Problem Solving 2009-2010

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

Page 5: Structured Problem Solving 2009-2010

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

Page 6: Structured Problem Solving 2009-2010

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

Page 7: Structured Problem Solving 2009-2010

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

Page 8: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

Strings in Java

• Alphabet Σ = {‘A’..’Z’, ‘a’..’z’, ‘0’..’9’, …..}

8

Page 9: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

Java Strings

String s;

s = "Fred";

System.out.println(s);

9

Page 10: Structured Problem Solving 2009-2010

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

Page 11: Structured Problem Solving 2009-2010

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

Page 12: Structured Problem Solving 2009-2010

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

Page 13: Structured Problem Solving 2009-2010

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

Page 14: Structured Problem Solving 2009-2010

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

Page 15: Structured Problem Solving 2009-2010

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

Page 16: Structured Problem Solving 2009-2010

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

Page 17: Structured Problem Solving 2009-2010

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

Page 18: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

Example 1

18

Page 19: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

Write out a word in reverse: e.g. “Fred” -> “derF”

19

Page 20: Structured Problem Solving 2009-2010

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

Page 21: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

Reversing a name of any length

21

Page 22: Structured Problem Solving 2009-2010

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

Page 23: Structured Problem Solving 2009-2010

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

Page 24: Structured Problem Solving 2009-2010

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

Page 25: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

Initial statement

• Count the number of words in a sentence

25

Page 26: Structured Problem Solving 2009-2010

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

Page 27: Structured Problem Solving 2009-2010

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

Page 28: Structured Problem Solving 2009-2010

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

Page 29: Structured Problem Solving 2009-2010

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

Page 30: Structured Problem Solving 2009-2010

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

Page 31: Structured Problem Solving 2009-2010

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

Page 32: Structured Problem Solving 2009-2010

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

Page 33: Structured Problem Solving 2009-2010

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

Page 34: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

Example 3

34

Page 35: Structured Problem Solving 2009-2010

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

Page 36: Structured Problem Solving 2009-2010

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

Page 37: Structured Problem Solving 2009-2010

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

Page 38: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

Analysing a URL

• http://www.hope.ac.uk

38

Country

Page 39: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

Analysing a URL

• http://www.hope.ac.uk

39

Classification

Page 40: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

Analysing a URL

• http://www.hope.ac.uk

40

Organisation

Page 41: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

Analysing a URL

• http://www.hope.ac.uk

41

Service

Page 42: Structured Problem Solving 2009-2010

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

Page 43: Structured Problem Solving 2009-2010

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

Page 44: Structured Problem Solving 2009-2010

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

Page 45: Structured Problem Solving 2009-2010

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)

Page 46: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

Analysing a URL – .COM is different

• http://www.microsoft.com

46

Organisation

Page 47: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

Analysing a URL – .COM is different

• http://www.microsoft.com

47

Service

Page 48: Structured Problem Solving 2009-2010

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

Page 49: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

First we design a solution

49

Page 50: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

Initial statement

Analyse a URL

50

Page 51: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

First refinement

Get URL

Analyse the URL

Report the results

51

Page 52: Structured Problem Solving 2009-2010

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

Page 53: Structured Problem Solving 2009-2010

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

Page 54: Structured Problem Solving 2009-2010

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

Page 55: Structured Problem Solving 2009-2010

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

Page 56: Structured Problem Solving 2009-2010

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

Page 57: Structured Problem Solving 2009-2010

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

Page 58: Structured Problem Solving 2009-2010

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

Page 59: Structured Problem Solving 2009-2010

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

Page 60: Structured Problem Solving 2009-2010

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

Page 61: Structured Problem Solving 2009-2010

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

Page 62: Structured Problem Solving 2009-2010

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

Page 63: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

Any Questions?