Presentation is loading. Please wait.

Presentation is loading. Please wait.

Faculty of Sciences and Social Sciences HOPE Structured Problem Solving 2009-2010 Week 10: Java: Strings Stewart Blakeway FML 213

Similar presentations


Presentation on theme: "Faculty of Sciences and Social Sciences HOPE Structured Problem Solving 2009-2010 Week 10: Java: Strings Stewart Blakeway FML 213"— Presentation transcript:

1 Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 10: Java: Strings Stewart Blakeway FML 213

2 Faculty of Sciences and Social Sciences HOPE 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

3 Faculty of Sciences and Social Sciences HOPE 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

4 Faculty of Sciences and Social Sciences HOPE 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. 4

5 Faculty of Sciences and Social Sciences HOPE 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: “ 5

6 Faculty of Sciences and Social Sciences HOPE 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

7 Faculty of Sciences and Social Sciences HOPE 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

8 Faculty of Sciences and Social Sciences HOPE Strings in Java Alphabet Σ = {‘A’..’Z’, ‘a’..’z’, ‘0’..’9’, …..} 8

9 Faculty of Sciences and Social Sciences HOPE Java Strings String s; s = "Fred"; System.out.println(s); 9

10 Faculty of Sciences and Social Sciences HOPE 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

11 Faculty of Sciences and Social Sciences HOPE Concatenating strings with + String a; String b; String c; b = "Fred"; c = "Bloggs"; a = b + c; System.out.println(a); 11

12 Faculty of Sciences and Social Sciences HOPE 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

13 Faculty of Sciences and Social Sciences HOPE Accessing characters in Strings String s; s = "Fred"; System.out.println(s[0]); 13

14 Faculty of Sciences and Social Sciences HOPE Accessing characters in Strings String s; int i; s = "Fred"; for (i=0; i<4; i++) { System.out.println(s[i]); } 14

15 Faculty of Sciences and Social Sciences HOPE Accessing characters in Strings String s; int i; s = "Fred"; for (i=0; i<4; i++) { System.out.println(s[i]); } 15

16 Faculty of Sciences and Social Sciences HOPE Accessing characters in Strings String s; int i; s = "Fred"; for (i=0; i<4; i++) { System.out.print(s[i]); } 16

17 Faculty of Sciences and Social Sciences HOPE Accessing characters in Strings String s; int i; s = "Fred"; for (i=0; i<4; i++) { System.out.print(s[i]); } 17

18 Faculty of Sciences and Social Sciences HOPE Example 1 18

19 Faculty of Sciences and Social Sciences HOPE Write out a word in reverse: e.g. “Fred” -> “derF” 19

20 Faculty of Sciences and Social Sciences HOPE Write out a word in reverse String s; int i; s = "Fred"; for (i=3; i>=0; i--) { System.out.print(s[i]); } 20

21 Faculty of Sciences and Social Sciences HOPE Reversing a name of any length 21

22 Faculty of Sciences and Social Sciences HOPE 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

23 Faculty of Sciences and Social Sciences HOPE 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

24 Faculty of Sciences and Social Sciences HOPE 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

25 Faculty of Sciences and Social Sciences HOPE Initial statement Count the number of words in a sentence 25

26 Faculty of Sciences and Social Sciences HOPE Initial statement Count the number of words in a sentence by counting spaces then adding 1 26

27 Faculty of Sciences and Social Sciences HOPE 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

28 Faculty of Sciences and Social Sciences HOPE First refinement Get sentence Initialise count Count the spaces Report the final count

29 Faculty of Sciences and Social Sciences HOPE Second refinement Read Sentence Count := 0 While (not reached end of sentence) begin deal with next character end Display Count

30 Faculty of Sciences and Social Sciences HOPE 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

31 Faculty of Sciences and Social Sciences HOPE Second refinement Read Sentence Count := 0 For each character in the sentence begin deal with the character end Display Count

32 Faculty of Sciences and Social Sciences HOPE Third refinement Read Sentence Count := 0 For each character in the sentence begin if (character = ' ') begin Count = Count + 1 end Display Count

33 Faculty of Sciences and Social Sciences HOPE 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

34 Faculty of Sciences and Social Sciences HOPE Example 3 34

35 Faculty of Sciences and Social Sciences HOPE Analysing a URL URL -> Universal Resource Locator URL is the basis for communicating locations of resources (data) on the web. – E.g. 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

36 Faculty of Sciences and Social Sciences HOPE Analysing a URL In this example we shall analyse a HTTP protocol URL to extract information about the location of the host. – E.g. 36

37 Faculty of Sciences and Social Sciences HOPE Analysing a URL In this example we shall analyse a HTTP protocol URL to extract information about the location of the host. – E.g. 37 Host name

38 Faculty of Sciences and Social Sciences HOPE Analysing a URL 38 Country

39 Faculty of Sciences and Social Sciences HOPE Analysing a URL 39 Classification

40 Faculty of Sciences and Social Sciences HOPE Analysing a URL 40 Organisation

41 Faculty of Sciences and Social Sciences HOPE Analysing a URL 41 Service

42 Faculty of Sciences and Social Sciences HOPE Analysing a URL Country = uk (United Kingdom) Classification = ac (Academic) Organisation = hope (Liverpool Hope) Service = World Wide Web ( Hope’s web server ) 42

43 Faculty of Sciences and Social Sciences HOPE Analysing a URL Country = uk (United Kingdom) Classification = ac (Academic) Organisation = hope (Liverpool Hope) Service = moodle(Hope’s VLE) 43

44 Faculty of Sciences and Social Sciences HOPE Analysing a URL Country – uk(United Kingdom) – de(Germany) – fr (France) Classification – ac (academic) – gov (government) – org (organisation) 44

45 Faculty of Sciences and Social Sciences HOPE Analysing a URL –.COM is different 45 USA (or an organisation based anywhere)

46 Faculty of Sciences and Social Sciences HOPE Analysing a URL –.COM is different 46 Organisation

47 Faculty of Sciences and Social Sciences HOPE Analysing a URL –.COM is different 47 Service

48 Faculty of Sciences and Social Sciences HOPE Analysing a URL Organisation based anywhere Organisation = microsoft Service = world wide web 48

49 Faculty of Sciences and Social Sciences HOPE First we design a solution 49

50 Faculty of Sciences and Social Sciences HOPE Initial statement Analyse a URL 50

51 Faculty of Sciences and Social Sciences HOPE First refinement Get URL Analyse the URL Report the results 51

52 Faculty of Sciences and Social Sciences HOPE 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

53 Faculty of Sciences and Social Sciences HOPE Second refinement – what output do we expect Get URL Analyse the URL if (URL has.com) then begin display.com report end Else 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

54 Faculty of Sciences and Social Sciences HOPE 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

55 Faculty of Sciences and Social Sciences HOPE 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

56 Faculty of Sciences and Social Sciences HOPE Second refinement – analyse the URL We keep two examples in front of us: – 1 – 2 We need to extract the separate parts of the URLs 56

57 Faculty of Sciences and Social Sciences HOPE Second refinement – analyse the URL We keep two examples in front of us: – 1 – 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

58 Faculty of Sciences and Social Sciences HOPE We now know what the analysis needs to do Identify the parts of the URL as separate words if (last word is “com”) then begin copy the other words into the variables Organisation and Service end else begin Copy the words into the variables Country, Classification, Organisation and Service end 58

59 Faculty of Sciences and Social Sciences HOPE 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

60 Faculty of Sciences and Social Sciences HOPE 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

61 Faculty of Sciences and Social Sciences HOPE Identify the parts of the URL as separate words – in Java i--; word2 = ""; while (url[i] != '.') { word2 = url[i] + word2; i--; } 61 Second word

62 Faculty of Sciences and Social Sciences HOPE 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

63 Faculty of Sciences and Social Sciences HOPE Any Questions?


Download ppt "Faculty of Sciences and Social Sciences HOPE Structured Problem Solving 2009-2010 Week 10: Java: Strings Stewart Blakeway FML 213"

Similar presentations


Ads by Google