1 / 87

Scanning

Scanning. Scanning image for text. Scanning frequencies for radio stations. Finding words in a sentence Finding identifiers, operators, in a program char String Variable-size type Indexing Enumeration. Primitive Types. int, double, boolean, long, short, float, byte char.

walda
Télécharger la présentation

Scanning

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. Scanning • Scanning image for text. • Scanning frequencies for radio stations. • Finding words in a sentence • Finding identifiers, operators, in a program • char • String • Variable-size type • Indexing • Enumeration

  2. Primitive Types • int, double, boolean, long, short, float, byte • char

  3. Primitive Types • Constants (Literals & Named Constants) • Operations with Invocation Syntax

  4. 16 bits char Escape sequence newline char Constants {letters, digits, operators ...} ‘\n’ ‘a’ ‘1’ ‘’’ ‘\’ ‘ ‘ ‘’ ‘\’’ ‘A‘ ‘< ‘ ‘ ‘\\’

  5. Useful Escape Sequences

  6. ordinal number (integer code) Ordering Characters ‘’ …. ‘a’ …. position in ordered character list

  7. ‘’ …. ‘a’ ‘b’ ‘c’ …. ‘z’ …. ‘’ ‘’ …. …. ‘0’ ‘A’ ‘B’ ‘1’ ‘2’ ‘C’ …. …. ‘Z’ ‘3’ …. …. Ordering Characters ‘a’ > ‘b’  false ‘a’ > ‘A’  ??? ‘a’ > ‘0’  ??? ‘B’ > ‘A’  true ‘4’ > ‘0’  true ‘0’ > ‘’  true

  8. Implicit cast to wider type Converting between Characters and their Ordinal Numbers (int) ‘a’  ordinal number of ’a’ (char) 55  character whose ordinal number is 55 (int) ‘c’ - (int) ‘a’  2 (int) ‘’  0 ‘c’ - ‘a’  2 (char) 0  ‘’ (int) ‘d’  ??? (char) (‘c’ - 2)  ‘a’ (char) 1  ??? (char) (‘A’ + 2)  ‘C’ (char) -1 (char) (‘C’ - ‘A’ + ‘a’)  ‘c’

  9. A Useful Character Operation Character.isLetter(c)  true if c is a letter Character.isLetter(‘a’)  true Character.isLetter(‘A’)  true Character.isLetter(‘1’)  false Character.isLetter(‘ ’)  false

  10. variable size Object Type String constants String {sequences of characters} “hello\n\n123” “hello” “hello 123” ‘a’ “” “123” “a” “\” “\\”

  11. index StringIndexBounds exceptiom Accessing String Components String s = “hello world”; s.getFirstChar() s.charAt(0)  ‘h’ s.charAt(1)  ‘e’ s.getSecondChar() s.charAt(-1) ... s.charAt(11) s.length()  11  1 “ ”.length() “”.length()  0

  12. StringIndexBounds exceptiom Accessing SubString public String substring (int beginIndex, int endIndex) s.substring(beginIndex, endIndex)  s.charAt(beginIndex) .. s.charAt(endIndex-1) “hello world”.substring(4,7)  “o w” “hello world”.substring(4,4)  “” “hello world”.substring(7,4)

  13. three different instances Changing Strings? Strings are read-only (immutable) “hello” + “world”  “hello world”

  14. Useful String Operations s.toLowerCase()  copy of s with letters converted to lower case s.toUpperCase()  copy of s with letters converted to upper case “Hello World”.toLowerCase()  “hello world” “Hello World”.toUpperCase()  “HELLO WORLD”

  15. String Processing • int i = 0; • while (i < s.length()) { • System.out.println (s.charAt(i)); • i++; • } prints each character on separate line

  16. loop condition loop body Dissecting a Loop • int i = 0; • while (i < s.length()) { • System.out.println (s.charAt(i)); • i++; • }

  17. initalizing loop variables loop condition real body Resetting loop variable Finer-grained Dissection • int i = 0; • while (i < s.length()) { • System.out.println (s.charAt(i)); • i++; • } • for (int i=0; i<s.length(); i++) • System.out.println(s.charAt(i));

  18. Meaning of For Loop for (S1; E; S2) S3 • for (; E; S2) • S3 • for (; E; ) • S3 • for (; ; ) • S3 S1; while ( E) { S3; S2; } while ( E) { S3; S2; } while ( E) { S3; } while ( true) S3;

  19. Scanning Problem

  20. token token Multi-line input stream Line stream Line 1 Line 2 J o h n F . K e n n e d y token token token Scanning Input stream Token Stream

  21. Solution • Monolithic Solution • Class Decomposition

  22. Algorithm String inputLine

  23. marker 0 Algorithm String inputLine J o h n F . K e n n e d y Output: J

  24. Algorithm String inputLine J o h n F . K e n n e d y marker 1 Output: J

  25. Algorithm String inputLine J o h n F . K e n n e d y marker 2 Output: J

  26. Algorithm String inputLine J o h n F . K e n n e d y marker 5 Output: JF

  27. Algorithm String inputLine J o h n F . K e n n e d y marker 6 Output: JF

  28. Algorithm String inputLine J o h n F . K e n n e d y marker 8 Output: JFK

  29. Algorithm String inputLine J o h n F . K e n n e d y marker 9 Output: JFK

  30. Algorithm String inputLine J o h n F . K e n n e d y marker 14 Output: JFK

  31. public class UpperCasePrinter { • publicstaticvoid main (String args[]) { • String input = getInput(); • int index = 0; • System.out.println("Upper Case Letters :"); • while (index < input.length()) { • char nextChar = input.charAt(index); • if (Character.isUpperCase(nextChar)) • System.out.print(nextChar); // token processing • index++; • } • } • publicstatic String getInput() { • System.out.println("Please enter a string"); • return Keyboard.readLine(); • } • } Monolithic Solution

  32. Storing instead of printing tokens String inputLine J o h n F . K e n n e d y marker 14 String s = “JFK”;

  33. No reuse in Monolithic Solutions String s = ""; // token processing int index = 0; while (index < input.length()) { char nextChar = input.charAt(index); if (Character.isUpperCase(nextChar)) s += nextChar; // token processing index++; } int index = 0; System.out.println("Upper Case Letters :");//token processing while (index < input.length()) { char nextChar = input.charAt(index); if (Character.isUpperCase(nextChar)) System.out.print(nextChar); // token processing index++; }

  34. Class Decomposition? Main Class

  35. Division of Labor in Radio Scanning

  36. Division of Labor in Radio Scanning

  37. Division of Labor in Radio Scanning

  38. Division of Labor in Radio Scanning

  39. Division of Labor in Radio Scanning

  40. Division of Labor in Radio Scanning

  41. Division of Labor in Radio Scanning

  42. Division of Labor in Radio Scanning ?

  43. Division of Labor in Radio Scanning

  44. Class Decomposition instantiate Scanner Class Scanner Object calls Scanner User Main Class (Input & Output)

  45. Class Decomposition instantiate DataInputStream Instance DataInputStream readLine() Scanner User Main Class (Input & Output)

  46. Scanner User-Scanner Object Interaction • DataInputStream dataIn = new DataInputStream (System.in); • int product = 1; • while (true) { • int num = Integer.parseInt (dataIn.readLine()); • if (num < 0) break; • product = product*num; • } • System.out.println (product);

  47. DataInputStream Operations Multi-line input stream Line stream Line 1 Line 2 dataIn.readLine() Line 1 dataIn.readLine() Line 2 dataIn.readLine() IOException

  48. Scanner Interface? Input stream Token Stream token 1 token 2 scanner.nextElement() token 1 scanner.nextElement() token 2 scanner.nextElement() ScannerException

  49. Scanner Interface? Input stream Token Stream token 1 token 2 scanner.nextElement() token 1 scanner.nextElement() token 2 scanner.hasMoreElements() false scanner.nextElement() ???

  50. J o h n F . K e n n e d y token 1 token 2 token 3 Uppercase Scanner Interface? scanner.nextElement() ‘J’ scanner.nextElement() ‘F’ scanner.nextElement() ‘K’ scanner.hasMoreElements() false scanner.nextElement() ???

More Related