Scanning Scanning image for text Scanning frequencies for

  • Slides: 87
Download presentation
Scanning • • Scanning image for text. Scanning frequencies for radio stations. Finding words

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

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

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

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

char Constants 16 bits char {letters, digits, operators. . . } ‘a’ ‘ 1’

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

Useful Escape Sequences

Useful Escape Sequences

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

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

Converting between Characters and their Ordinal Numbers (int) ‘a’ ordinal number of ’a’ (char)

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

A Useful Character Operation Character. is. Letter(c) true if c is a letter Character.

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

String constants variable size Object Type String {sequences of characters} “hello” “hello 123” ‘a’

String constants variable size Object Type String {sequences of characters} “hello” “hello 123” ‘a’ “ 123” “a” “” “hellonn 123” “\”

Accessing String Components index String s = “hello world”; s. get. First. Char() s.

Accessing String Components index String s = “hello world”; s. get. First. Char() s. get. Second. Char(). . . s. char. At(0) s. char. At(1) ‘h’ ‘e’ s. char. At(-1) s. char. At(11) s. length() String. Index. Bounds “ ”. length() exceptiom “”. length() 11 1 0

Accessing Sub. String public String substring (int begin. Index, int end. Index) s. substring(begin.

Accessing Sub. String public String substring (int begin. Index, int end. Index) s. substring(begin. Index, end. Index) s. char. At(begin. Index). . s. char. At(end. Index-1) “hello world”. substring(4, 7) “o w” “hello world”. substring(4, 4) “” “hello world”. substring(7, 4) String. Index. Bounds exceptiom

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

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

Useful String Operations s. to. Lower. Case() copy of s with letters converted to

Useful String Operations s. to. Lower. Case() copy of s with letters converted to lower case s. to. Upper. Case() copy of s with letters converted to upper case “Hello World”. to. Lower. Case() “hello world” “Hello World”. to. Upper. Case() “HELLO WORLD”

String Processing int i = 0; while (i < s. length()) { System. out.

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

Dissecting a Loop loop condition int i = 0; while (i < s. length())

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

Finer-grained Dissection initalizing loop variables loop condition int i = 0; while (i <

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

Meaning of For Loop for (S 1; E; S 2) S 3 for (;

Meaning of For Loop for (S 1; E; S 2) S 3 for (; E; ) S 3 for (; ; ) S 3 S 1; while ( E) { S 3; S 2; } while ( E) { S 3; } while ( true) S 3;

Scanning Problem

Scanning Problem

Scanning Input stream token Line 1 J token o h n Token Stream token

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

Solution • Monolithic Solution • Class Decomposition

Solution • Monolithic Solution • Class Decomposition

Algorithm String input. Line

Algorithm String input. Line

Algorithm String input. Line J marker o h 0 n F . K e

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

Algorithm String input. Line J marker o h 1 n F . K e

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

Algorithm String input. Line J marker o h 2 n F . K e

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

Algorithm String input. Line J marker o h 5 n F . K e

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

Algorithm String input. Line J marker o h 6 n F . K e

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

Algorithm String input. Line J marker o h 8 n F . K e

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

Algorithm String input. Line J marker o h 9 n F . K e

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

Algorithm String input. Line J o h marker 14 n F . K e

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

Monolithic Solution public class Upper. Case. Printer { public static void main (String args[])

Monolithic Solution public class Upper. Case. Printer { public static void main (String args[]) { String input = get. Input(); int index = 0; System. out. println("Upper Case Letters : "); while (index < input. length()) { char next. Char = input. char. At(index); if (Character. is. Upper. Case(next. Char)) System. out. print(next. Char); // token processing index++; } } public static String get. Input() { System. out. println("Please enter a string"); return Keyboard. read. Line(); } }

Storing instead of printing tokens String input. Line J o h marker 14 n

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

No reuse in Monolithic Solutions String s = ""; // token processing int index

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

Class Decomposition? Main Class

Class Decomposition? Main Class

Division of Labor in Radio Scanning

Division of Labor in Radio Scanning

Division of Labor in Radio Scanning

Division of Labor in Radio Scanning

Division of Labor in Radio Scanning

Division of Labor in Radio Scanning

Division of Labor in Radio Scanning

Division of Labor in Radio Scanning

Division of Labor in Radio Scanning

Division of Labor in Radio Scanning

Division of Labor in Radio Scanning

Division of Labor in Radio Scanning

Division of Labor in Radio Scanning

Division of Labor in Radio Scanning

Division of Labor in Radio Scanning ?

Division of Labor in Radio Scanning ?

Division of Labor in Radio Scanning

Division of Labor in Radio Scanning

Class Decomposition Scanner Class instantiate Scanner Object calls Scanner User Main Class (Input &

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

Class Decomposition instantiate Data. Input. Stream Instance read. Line() Scanner User Main Class (Input

Class Decomposition instantiate Data. Input. Stream Instance read. Line() Scanner User Main Class (Input & Output)

Scanner User-Scanner Object Interaction Data. Input. Stream data. In = new Data. Input. Stream

Scanner User-Scanner Object Interaction Data. Input. Stream data. In = new Data. Input. Stream (System. in); int product = 1; while (true) { int num = Integer. parse. Int (data. In. read. Line()); if (num < 0) break; product = product*num; } System. out. println (product);

Data. Input. Stream Operations Line 1 Line 2 data. In. read. Line() Line 1

Data. Input. Stream Operations Line 1 Line 2 data. In. read. Line() Line 1 data. In. read. Line() Line 2 data. In. read. Line() IOException Multi-line input stream Line stream

Scanner Interface? Input stream token 1 token 2 scanner. next. Element() token 1 scanner.

Scanner Interface? Input stream token 1 token 2 scanner. next. Element() token 1 scanner. next. Element() token 2 scanner. next. Element() Scanner. Exception Token Stream

Scanner Interface? Input stream token 1 token 2 scanner. next. Element() token 1 scanner.

Scanner Interface? Input stream token 1 token 2 scanner. next. Element() token 1 scanner. next. Element() token 2 scanner. has. More. Elements() scanner. next. Element() false ? ? ? Token Stream

Uppercase Scanner Interface? J o token 1 h n F . token 2 K

Uppercase Scanner Interface? J o token 1 h n F . token 2 K token 3 scanner. next. Element() ‘J’ scanner. next. Element() ‘F’ scanner. next. Element() ‘K’ scanner. has. More. Elements() scanner. next. Element() e false ? ? ? n n e d y

Enumeration Interfaces public interface Char. Enumeration { public char next. Element(); public boolean has.

Enumeration Interfaces public interface Char. Enumeration { public char next. Element(); public boolean has. More. Elements(); } public interface String. Enumeration { public String next. Element(); public boolean has. More. Elements(); } public interface <Type>Enumeration { public <Type> next. Element(); public boolean has. More. Elements(); }

Using an Enumeration Interface J o token 1 h n F . token 2

Using an Enumeration Interface J o token 1 h n F . token 2 K e n n e d y token 3 public static void print. Chars (Char. Enumeration char. Enumeration) { while (char. Enumeration. has. More. Elements()) System. out. print(char. Enumeration. next. Element()); }

Using an Enumeration Interface public class Upper. Case. Printer { public static void main

Using an Enumeration Interface public class Upper. Case. Printer { public static void main (String args[]) { String input = get. Input(); print. Upper. Case(input); } public static String get. Input() { System. out. println("Please enter a string"); return Keyboard. read. Line(); } public static void print. Upper. Case(String s) { System. out. println("Upper Case Letters: "); print. Chars (new An. Upper. Case. Enumeration(s)); } public static void print. Chars (Char. Enumeration char. Enumeration) { while (char. Enumeration. has. More. Elements()) System. out. print(char. Enumeration. next. Element()); } }

Implementing Scanner public class An. Upper. Case. Enumeration implements Char. Enumeration { … public

Implementing Scanner public class An. Upper. Case. Enumeration implements Char. Enumeration { … public An. Upper. Case. Enumeration(String the. String) {. . . } public boolean has. More. Elements() {. . . } public char next. Element() {. . . ; } … }

Data Structure: Scanned String public class An. Upper. Case. Enumeration implements Char. Enumeration {

Data Structure: Scanned String public class An. Upper. Case. Enumeration implements Char. Enumeration { String string; … public An. Upper. Case. Enumeration(String the. String) { string = the. String; . . . } public boolean has. More. Elements() {. . . } public char next. Element() {. . . ; } … }

Data Structure: marker string J o h n F . scanned part next. Element.

Data Structure: marker string J o h n F . scanned part next. Element. Pos K e n Unscanned part n e d y

has. More. Elements() string J o h n F . next. Element. Pos public

has. More. Elements() string J o h n F . next. Element. Pos public boolean has. More. Elements() {. . . } true K e n n e d y

has. More. Elements() string J o h n F . K e n n

has. More. Elements() string J o h n F . K e n n e d y next. Element. Pos public boolean has. More. Elements() {. . . } true

has. More. Elements() string J o h n F . K e n n

has. More. Elements() string J o h n F . K e n n e d y next. Element. Pos public boolean has. More. Elements() {. . . } false

has. More. Elements() string J o h n F . K e n n

has. More. Elements() string J o h n F . K e n n e d y next. Element. Pos //return true if next. Element. Pos is beyond end of string; false otherwise public boolean has. More. Elements() {. . . }

has. More. Elements() (edited) string J o h n F . K e n

has. More. Elements() (edited) string J o h n F . K e n n e d y next. Element. Pos //return false if next. Element. Pos is beyond end of string; true otherwise public boolean has. More. Elements() { return string. length() > next. Element. Pos; }

has. More. Elements() string J o h n F . K e n n

has. More. Elements() string J o h n F . K e n n e d y next. Element. Pos //return false if next. Element. Pos is beyond end of string; true otherwise public boolean has. More. Elements() { return next. Element. Pos < string. length(); }

next. Element() string J o h n F next. Element. Pos public char next.

next. Element() string J o h n F next. Element. Pos public char next. Element() { } . K e n n e d y

next. Element() string J o h n F . K e n n e

next. Element() string J o h n F . K e n n e d next. Element. Pos //Assume next. Elem. Pos is at start of next token or end of string when //method is called. //Method makes sure this is also true before returning. //returns next element if one exists public char next. Elements() { } y

next. Element() string J o h n F . K e n n e

next. Element() string J o h n F . K e n n e d Unexecuted Loop next. Element. Pos //Assume next. Elem. Pos is at start of next token or end of string when //method is called. //Method makes sure this is also true before returning. //returns next element if one exists public char next. Elements() { char ret. Val = string. char. At(next. Elem. Pos) while (!Character. is. Upper. Case(string. char. At(next. Element. Pos))) } next. Elem. Pos++; return ret. Val; y

next. Element() string J o h n F . K e n n e

next. Element() string J o h n F . K e n n e d next. Element. Pos //Assume next. Elem. Pos is at start of next token or end of string when //method is called. //Method makes sure this is also true before returning. //returns next element if one exists public char next. Elements() { char ret. Val = extract. Token(); move. Past. Current. Token(); skip. Non. Token. Characters(); return ret. Val; } y

next. Element() string J o h n F . K e n n e

next. Element() string J o h n F . K e n n e d next. Element. Pos //Assume next. Elem. Pos is at start of next token or end of string when //method is called. //returns next token if one exists public char extract. Token() { return string. char. At(next. Element. Pos); } y

move. Past. Current. Token() string J o h n F . K e n

move. Past. Current. Token() string J o h n F . K e n n e d next. Element. Pos //Assume next. Elem. Pos is at start of next token or end of string when //method is called. //Moves past current token. public void move. Past. Current. Token() { next. Elem. Pos++; } y

skip. Non. Token. Characters() string J o h n F . K e n

skip. Non. Token. Characters() string J o h n F . K e n n e d y next. Element. Pos String. Index. Out. Of. Bounds // keep advancing next. Element. Pos until we hit the next upper case public void skip. Non. Token. Characters() { while (!Character. is. Upper. Case(string. char. At(next. Element. Pos))) next. Elem. Pos++; }

skip. Non. Token. Characters() string J o h n F . K e n

skip. Non. Token. Characters() string J o h n F . K e n n e d y next. Element. Pos short-circuit String. Index. Out. Of. Bounds? // keep advancing next. Element. Pos until we hit the next upper case or go // beyond the end of the string. public void skip. Non. Token. Characters() { while (next. Element. Pos < string. length() && !Character. is. Upper. Case(string. char. At(next. Element Pos))) next. Elem. Pos++; }

Initialization string j o h n F . K e n next. Element. Pos

Initialization string j o h n F . K e n next. Element. Pos String string; int next. Element. Pos = 0; public An. Upper. Case. Enumeration(String the. String) { string = the. String; skip. Non. Token. Characters(); } n e d y

Complete Scanner public class An. Upper. Case. Enumeration implements Char. Enumeration { String string;

Complete Scanner public class An. Upper. Case. Enumeration implements Char. Enumeration { String string; int next. Element. Pos = 0; public An. Upper. Case. Enumeration(String the. String) { string = the. String; skip. Non. Token. Characters(); } public boolean has. More. Elements() { return next. Element. Pos < string. length(); } public char next. Element() { char ret. Val = extract. Token(); move. Past. Current. Token(); skip. Non. Token. Characters(); return ret. Val; } void move. Past. Current. Token() {next. Element. Pos++; } void skip. Non. Token. Characters() { while (next. Element. Pos < string. length() && !Character. is. Upper. Case(string. char. At(next. Element. Pos++; } char extract. Token() { return string. char. At(next. Element. Pos); } }

Scanner Pattern public class <Scanner Name> implements <T>Enumeration { String string; int next. Element.

Scanner Pattern public class <Scanner Name> implements <T>Enumeration { String string; int next. Element. Start = 0; int next. Element. End = ? ? ? ; public An. Upper. Case. Enumeration(String the. String) { string = the. String; skip. Non. Token. Characters(); } public boolean has. More. Elements() { return next. Element. Start < string. length(); } public <T> next. Element() { <T> ret. Val = extract. Token(token. Length); move. Past. Current. Token(token. Length); skip. Non. Token. Characters(); return ret. Val; } void move. Past. Current. Token() {…}; void skip. Non. Token. Characters() {…}; char extract. Token() { …} }

Scanner User Again public class Upper. Case. Printer { public static void main (String

Scanner User Again public class Upper. Case. Printer { public static void main (String args[]) { String input = get. Input(); print. Upper. Case(input); } public static String get. Input() { System. out. println("Please enter a string"); return Keyboard. read. Line(); } public static void print. Upper. Case(String s) { System. out. println("Upper Case Letters: "); print. Chars (new An. Upper. Case. Enumeration(s)); } public static void print. Chars (Char. Enumeration char. Enumeration) { while (char. Enumeration. has. More. Elements()) System. out. print(char. Enumeration. next. Element()); } }

Monolithic Solution public class Upper. Case. Printer { public static void main (String args[])

Monolithic Solution public class Upper. Case. Printer { public static void main (String args[]) { String input = get. Input(); int index = 0; System. out. println("Upper Case Letters : "); while (index < input. length()) { char next. Char = input. char. At(index); if (Character. is. Upper. Case(next. Char)) System. out. print(next. Char); // token processing index++; } } public static String get. Input() { System. out. println("Please enter a string"); return Keyboard. read. Line(); } }

Class Decomposition instantiate An. Upper. Case. Enumeration instance An. Uppercase. Printer

Class Decomposition instantiate An. Upper. Case. Enumeration instance An. Uppercase. Printer

Class Decomposition instantiate An. Upper. Case. Enumeration instance An. Uppercase. Concatenator

Class Decomposition instantiate An. Upper. Case. Enumeration instance An. Uppercase. Concatenator

Reuse of Enumeration String s = ""; while (char. Enumeration. has. More. Elements()) {

Reuse of Enumeration String s = ""; while (char. Enumeration. has. More. Elements()) { s += char. Enumeration. next. Element(); } while (char. Enumeration. has. More. Elements()) System. out. print(char. Enumeration. next. Element());

Monolithic Solution String s = ""; // token processing int index = 0; while

Monolithic Solution String s = ""; // token processing int index = 0; while (index < input. length()) { char next. Char = input. char. At(index); if (Character. is. Upper. Case(next. Char)) s += next. Char; // token processing index++; } int index = 0; System. out. println("Upper Case Letters : "); //token processing while (index < input. length()) { char next. Char = input. char. At(index); if (Character. is. Upper. Case(next. Char)) System. out. print(next. Char); // token processing index++; }

Monolithic Solution to read. Line() initialize while there is more input set next line;

Monolithic Solution to read. Line() initialize while there is more input set next line; user code to process next line …. move next line markers

Enumeration Vs Scanning J o token 1 h n F . token 2 K

Enumeration Vs Scanning J o token 1 h n F . token 2 K e n n token 3 public interface Char. Enumeration { public char next. Element(); public boolean has. More. Elements(); } e d y

Enumeration without Scanning All. Uppercase. Letters. In. Order ‘A’ ‘B’. . . ‘Z’ implements

Enumeration without Scanning All. Uppercase. Letters. In. Order ‘A’ ‘B’. . . ‘Z’ implements public interface Char. Enumeration { public char next. Element(); public boolean has. More. Elements(); }

Enumerating all Uppercase Letters //instance variables ? ? ? public char next. Element() {

Enumerating all Uppercase Letters //instance variables ? ? ? public char next. Element() { ? ? ? } public boolean has. More. Elements() { ? ? }

Enumerating all Uppercase Letters (edited) //instance variables ? ? ? public char next. Element()

Enumerating all Uppercase Letters (edited) //instance variables ? ? ? public char next. Element() { ? ? ? } public boolean has. More. Elements() { ? ? }

Enumerating all Uppercase Letters public class All. Upper. Case. Letters. In. Order implements Char.

Enumerating all Uppercase Letters public class All. Upper. Case. Letters. In. Order implements Char. Enumeration { char next. Letter = 'A'; public boolean has. More. Elements() { return next. Letter <= 'Z'; } public char next. Element() { char ret. Val = next. Letter; next. Letter = (char) (next. Letter + 1); return ret. Val; } }

Comparing Two Implementations J o token 1 h n F . token 2 K

Comparing Two Implementations J o token 1 h n F . token 2 K e n n e d token 3 An. Upper. Case. Enumeration implements public interface Char. Enumeration { public char next. Element(); public boolean has. More. Elements(); } ‘J’ ‘F’‘K’ y

Radically Different Behaviors Polymorphism print (new An. Uppercase. Enumeration(s)); print (new All. Uppercase. Letters.

Radically Different Behaviors Polymorphism print (new An. Uppercase. Enumeration(s)); print (new All. Uppercase. Letters. In. Order()); All. Uppercase. Letters. In. Order ‘A’ ‘B’. . . ‘Z’ implements public interface Char. Enumeration { Syntactic public char next. Element(); Specification! public boolean has. More. Elements(); }