1 / 63

Structured Problem Solving 2009-2010

Structured Problem Solving 2009-2010. Week 10: Java: Strings Stewart Blakeway FML 213 blakews@hope.ac.uk. 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

holland
Télécharger la présentation

Structured Problem Solving 2009-2010

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Structured Problem Solving2009-2010 Week 10: Java: Strings Stewart Blakeway FML 213 blakews@hope.ac.uk

  2. 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

  3. 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

  4. 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)

  5. 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)”

  6. Example • Suppose Σ = { W, $, Þ,Ŷ, Ǿ}. • Which of the following are valid strings over this alphabet: • ǾW$$ • WORD • Þ • $Ŷw Yes No – ‘O’, ‘R’, ‘D’ not in given alphabet Yes No – ‘w’ not in given alphabet

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

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

  9. Java Strings String s; s = "Fred"; System.out.println(s);

  10. 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

  11. Concatenating strings with + String a; String b; String c; b = "Fred"; c = "Bloggs"; a = b + c; System.out.println(a);

  12. Accessing characters in Strings String s; s = "Fred"; s[0]is'F' s[1]is'r' s[2]is'e' s[3]is'd'

  13. Accessing characters in Strings String s; s = "Fred"; System.out.println(s[0]);

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

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

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

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

  18. Example 1

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

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

  21. Reversing a name of any length

  22. 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]); }

  23. 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);

  24. 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]); }

  25. Initial statement • Count the number of words in a sentence

  26. Initial statement • Count the number of words in a sentence by counting spaces then adding 1

  27. 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

  28. First refinement Get sentence Initialise count Count the spaces Report the final count + 1

  29. Second refinement Read Sentence Count := 0 While (not reached end of sentence) begin deal with next character end Display Count + 1

  30. 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

  31. Second refinement Read Sentence Count := 0 For each character in the sentence begin deal with the character end Display Count + 1

  32. Third refinement Read Sentence Count := 0 For each character in the sentence begin if (character = '') begin Count = Count + 1 end end Display Count + 1

  33. Counting the number of words in a sentence: in Java String sentence; int count; inti; 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.");

  34. Example 3

  35. 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.

  36. 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. 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 Host name

  38. Analysing a URL • http://www.hope.ac.uk Country

  39. Analysing a URL • http://www.hope.ac.uk Classification

  40. Analysing a URL • http://www.hope.ac.uk Organisation

  41. Analysing a URL • http://www.hope.ac.uk Service

  42. 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)

  43. Analysing a URL • http://moodle.hope.ac.uk • Country = uk (United Kingdom) • Classification = ac (Academic) • Organisation = hope (Liverpool Hope) • Service = moodle (Hope’s VLE)

  44. Analysing a URL • Country • uk (United Kingdom) • de (Germany) • fr (France) • Classification • ac (academic) • gov (government) • org (organisation)

  45. Analysing a URL – .COM is different • http://www.microsoft.com USA (or an organisation based anywhere)

  46. Analysing a URL – .COM is different • http://www.microsoft.com Organisation

  47. Analysing a URL – .COM is different • http://www.microsoft.com Service

  48. Analysing a URL • http://www.microsoft.com • Organisation based anywhere • Organisation = microsoft • Service = world wide web

  49. First we design a solution

  50. Initial statement Analyse a URL

More Related